我想创建一个每5分钟触发一次的基于时间的规则,Drools文档指出:
相反,当Drools引擎以被动模式运行时(即使用FireAllRules而不是FireUntilhalt),默认情况下,它不会触发定时规则的结果,除非FireAllRules不再被调用。但是,可以通过使用TimeDruleExecutionOption配置KieSession来更改此默认行为,如下例所示
KieSessionConfiguration ksconf = KieServices.Factory.get().newKieSessionConfiguration();
ksconf.setOption( TimedRuleExecutionOption.YES );
KSession ksession = kbase.newKieSession(ksconf, null);
但是,我不直接访问KieSeEnter对象,因为我正在使用Java REST API将请求发送到部署在KiRealEngServer上的DROLS项目,像这样(直接从DROLS文档中获取):
public class MyConfigurationObject {
private static final String URL = "http://localhost:8080/kie-server/services/rest/server";
private static final String USER = "baAdmin";
private static final String PASSWORD = "password@1";
private static final MarshallingFormat FORMAT = MarshallingFormat.JSON;
private static KieServicesConfiguration conf;
private static KieServicesClient kieServicesClient;
public static void initializeKieServerClient() {
conf = KieServicesFactory.newRestConfiguration(URL, USER, PASSWORD);
conf.setMarshallingFormat(FORMAT);
kieServicesClient = KieServicesFactory.newKieServicesClient(conf);
}
public void executeCommands() {
String containerId = "hello";
System.out.println("== Sending commands to the server ==");
RuleServicesClient rulesClient = kieServicesClient.getServicesClient(RuleServicesClient.class);
KieCommands commandsFactory = KieServices.Factory.get().getCommands();
Command> insert = commandsFactory.newInsert("Some String OBJ");
Command> fireAllRules = commandsFactory.newFireAllRules();
Command> batchCommand = commandsFactory.newBatchExecution(Arrays.asList(insert, fireAllRules));
ServiceResponse executeResponse = rulesClient.executeCommandsWithResults(containerId, batchCommand);
if(executeResponse.getType() == ResponseType.SUCCESS) {
System.out.println("Commands executed with success! Response: ");
System.out.println(executeResponse.getResult());
} else {
System.out.println("Error executing rules. Message: ");
System.out.println(executeResponse.getMsg());
}
}
}
所以我有点困惑,我该如何将这个时间执行选项传递给会话?
我已经通过定期发送fireallrules命令找到了解决方法,但我想知道是否可以配置此会话选项,这样我就不必为我要创建的每个定时事件添加定期触发。
另外,我尝试使用firetuntilhalt而不是fireallrules,但据我所知,命令会阻塞服务器上的执行线程,我必须在某个时刻发送一个haltcommand,所有这些我都想避免,因为我有一个多线程的客户机将事件发送到服务器。