我正在使用带有弹簧的流口水XLS文件。我使用外部位置在XLS文件中实施了业务规则,然后在Kie服务的帮助下,我正在执行规则。以下是我在引擎中加载规则的代码段。在春季初始化的开始时,我正在调用init((方法请参阅下面的春季配置。
<bean id="droolsService" class="com.example.drools.DroolsServiceImpl" init-method="init">
Java代码
public void init() {
LOG.info("inside init");
KieSession kieSession;
for (RequestType type : droolsMap.keySet()) {
try {
kieSession = getKieSession(this.getDroolsMap().get(type));
droolsRules.put(type, kieSession);
} catch (Exception e) {
LOG.error("Failed to load kiesession:", e);
throw new RuntimeException(e);
}
}
}
private KieSession getKieSession(final String file) throws DroolsParserException, IOException, BiffException {
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();
InputStream stream = null;
String drl = null;
String RULE_PATH = "src/main/resources/";
SpreadsheetCompiler converter = new SpreadsheetCompiler();
//Workbook workbook = Workbook.getWorkbook(DroolsServiceImpl.class.getResourceAsStream(file));
Workbook workbook = Workbook.getWorkbook(new FileInputStream(file));
LOG.info("Loading rule file " + file);
for (Sheet sheet : workbook.getSheets()) {
LOG.info("Loading Sheet " + sheet.getName());
stream = new FileInputStream(file);
drl = converter.compile(stream, sheet.getName());
//StringReader reader = new StringReader(drl);
String DRL_FILE = RULE_PATH + sheet.getName() + ".drl";
System.out.println("Drool file added ::: " + DRL_FILE);
kfs.write(DRL_FILE, ResourceFactory.newReaderResource(new StringReader(drl)));
stream.close();
}
KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
KieSessionConfiguration conf = SessionConfiguration.newInstance();
KieSession ksession = kieContainer.newKieSession(conf);
if (kieBuilder.getResults().hasMessages(Message.Level.ERROR)) {
List<Message> errors = kieBuilder.getResults().getMessages(Message.Level.ERROR);
StringBuilder sb = new StringBuilder("Errors:");
for (Message msg : errors) {
sb.append("n " + msg);
}
try {
throw new Exception(sb.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null)
stream.close();
if (workbook != null)
workbook.close();
}
}
return ksession;
}
一切正常的一切都完美,但问题是我无法扫描文件更改。如果文件已修改,则我必须重新启动服务器才能同步更改。
我尝试过侦听器加载特定的init((方法,XLS dtable具有任何更改,但它不起作用,相同的旧结果也会到来。
我尝试过Kiescanner,但我无法获得这个概念。Kiescanner正在加载Maven Kjar,所以我该如何创建Kjar。
我只想在流口水文件中进行任何更改,然后尝试重新加载Kiecontainer的整个更改而不重新启动服务器。
自己找到答案,因为它将帮助需要的人。
我所做的,我使用了Apache VFS文件监视器 -
DefaultFileMonitor fm = new DefaultFileMonitor(new CustomFileListener());
当文件修改,创建或删除时,它将调用customFileListener。
以下是实现CustomFileListener。
import org.apache.commons.vfs2.FileChangeEvent;
import org.apache.commons.vfs2.FileListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.support.XmlWebApplicationContext;
public class CustomFileListener implements FileListener {
private static final Logger LOG = LoggerFactory.getLogger(CustomFileListener.class);
@Override
public void fileCreated(FileChangeEvent fileChangeEvent) throws Exception {
}
@Override
public void fileDeleted(FileChangeEvent fileChangeEvent) throws Exception {
}
@Override
public void fileChanged(FileChangeEvent fileChangeEvent) throws Exception {
LOG.debug(" Under FileChanged Method");
LOG.debug(" File has been changed hence reinitializing init method = " + fileChangeEvent.getFile().getName().getPath());
XmlWebApplicationContext xmlWebApplicationContext =
(XmlWebApplicationContext) ContextLoader.getCurrentWebApplicationContext();
DefaultListableBeanFactory defaultListableBeanFactory =
(DefaultListableBeanFactory) xmlWebApplicationContext.getBeanFactory();
DroolsServiceImpl droolsService = (DroolsServiceImpl) defaultListableBeanFactory.getBean("droolsService");
droolsService.init();
}
}
我在文件将更改时所做的事情,它将调用filechanged方法。
我从 contextLoader.getCurrentWebapplicationContext((; > 中获取了缓存的bean(droolServiceImpl(,并称为其init((方法。
这样,它将重新加载整个过程并重新定位kiemodule,kierepository。