我想创建一个基于时间的规则,每 5 分钟触发一次,Drools 文档指出:
相反,当 Drools 引擎在被动模式下运行(即:使用 fireAllRules 而不是 fireUntilHalt)时,默认情况下它不会触发定时规则的后果,除非再次调用 fireAllRules。但是,可以通过使用 TimedRuleExecutionOption 配置 KieSession 来更改此默认行为,如下例所示
KieSessionConfiguration ksconf = KieServices.Factory.get().newKieSessionConfiguration();
ksconf.setOption( TimedRuleExecutionOption.YES );
KSession ksession = kbase.newKieSession(ksconf, null);
但是,我没有直接访问 KieSession 对象,因为我使用 Java REST API 向部署在 KieExecution Server 上的 Drools 项目发送请求,如下所示(示例直接取自 Drools 文档):
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<ExecutionResults> 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());
}
}
}
所以我对如何将这个 TimedRuleExecutionOption 传递给会话有点困惑?
我已经通过定期发送 FireAllRules 命令找到了一种解决方法,但我想知道是否可以配置此会话选项,这样我就不必为要创建的每个定时事件添加定期触发。
另外,我尝试使用 FireUntilHalt 而不是 FireAllRules,但据我了解,命令会阻塞服务器上的执行线程,我必须在某个时候发送 HaltCommand,所有这些我都想避免,因为我有一个 multi-将事件发送到服务器的线程客户端。