通过Java API Drools的计时规则执行

问题描述 投票:0回答:2

我想创建一个被每5分钟触发基于时间的规则,而Drools文档指出:

相反,当发动机的Drools在被动模式下运行(即:使用而不是fireUntilHalt fireAllRules)默认情况下,除非fireAllRules不会再次调用它不火的定时规则的后果。然而,它是可以通过用TimedRuleExecutionOption配置KieSession改变这种默认行为如图中下面的示例

KieSessionConfiguration ksconf = KieServices.Factory.get().newKieSessionConfiguration();
ksconf.setOption( TimedRuleExecutionOption.YES );
KSession ksession = kbase.newKieSession(ksconf, null);

不过,我不是直接访问,因为我使用的是Java的REST API将请求发送到部署KieExecution服务器上像这样(直接从Drools的文件所采取的示例)一个Drools项目的KieSession对象:

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传递给session?

我已经通过发送FireAllRules周期性的命令,但我想知道如果我能配置此session选项,这样我就不用加定期触发每一个定时事件,我想创建找到了解决办法。

另外,我一直在使用,而不是FireAllRules FireUntilHalt试了,但我的理解是命令块在服务器上执行线程,我必须在某个时间点发送HaltCommand,所有这些我想避免,因为我有一个多线程客户端发送事件到服务器。

java drools kie kie-server
2个回答
0
投票

通过“-Ddrools.timedRuleExecution =真”,而开始在那里乃纪伊,将server.war部署服务器实例。


0
投票

您可以使用滴料的cron功能。它作为一个计时器,并调用基于cron的expresion规则。实施例执行的规则,每5分钟:

rule "Send SMS every 5 minutes"
    timer (cron:* 0/5 * * * ?)
when
    $a : Event( )
then

end

你可以找到解释here

© www.soinside.com 2019 - 2024. All rights reserved.