Chapter 19. Requirements for HA CEP client and server code
When developing client and server code for high-availability CEP, follow certain additional requirements.
kie-remote API
The client code must use the kie-remote
API and not the kie
API. The kie-remote
API is specified in the org.kie:kie-remote
Maven artifact. You can find the source code in the kie-remote
Maven module.
Explicit timestamps
The decision engine needs to determine the sequence in which events happen. For this reason, every event must have an associated timestamp assigned to it. In a high-availability environment, make this timestamp a property of the JavaBean that models the event. Annotate the event class with the @Timestamp
annotation, where the name of the timestamp attribute itself is the parameter, as in the following example:
@Role(Role.Type.EVENT) @Timestamp("myTime") public class StockTickEvent implements Serializable { private String company; private double price; private long myTime; }
If you do not provide a timestamp attribute, Drools assigns a timestamp to every event based on the time when the event is inserted by the client into a remote session. However, this mechanism depends on the clocks on the client machines. If clocks between different clients diverge, inconsistencies can occur between events inserted by these hosts.
Lambda expressions for non-memory actions
Working memory actions (actions to insert, modify, or delete information in the working memory of the decision engine) must be processed on every node of the cluster. Actions that are not memory actions must be executed only on the leader.
For example, the code might include the following rule:
rule FindAdult when $p : Person(age >= 18) then modify($p) { setAdult(true) }; // working memory action sendEmailTo($p); // side effect end
When this rule is triggered, the person must be marked as an adult on every node. However, only the leader must send the email, so that only one copy of the email is sent.
Therefore, in your code, wrap the email action (called a side effect) in a lambda expression, as shown in the following example:
rule FindAdult when $p : Person(age >= 18) then modify($p) { setAdult(true) }; DroolsExecutor.getInstance().execute( () -> sendEmailTo($p) ); end