To set asynchronous ruleset execution, your code must create
a stateless session and an execution request, specify the canonical
ruleset path, pass input parameters, and create an observer.
About this task
You can set stateless sessions to execute rulesets asynchronously
depending on execution events. When you set execution as asynchronous,
the rest of your code does not have to wait for the result of the
execution. This is useful, for example, when you work in a graphical
user interface (GUI): you want to be able to execute actions in the
GUI while an execution is taking place. To achieve this, you do not
have to code multithreading manually: you set the execution as asynchronous
and the API takes care of it for you.
See the ilog.rules.res.session.async package for
details of the API.
Procedure
To set asynchronous execution:
- Use a factory to create a stateless session.
IlrStatelessSession statelessSession = getSessionFactory().createStatelessSession();
- Use a factory to create an execution request.
IlrSessionRequest request = sessionFactory.createRequest();
- Specify the ruleset path.
request.setRulesetPath(IlrPath.parsePath("/bigsequential/1.0/bigsequentialruleset/1.0"));
- Pass the input parameters.
request.setInputParameter("param1", "ruoa")
- Create an observer.
The observer is called
when the execution fails or ends.
IlrAsyncExecutionObserver myObserver = new IlrAsyncExecutionObserver() {
public void update(IlrAsyncExecutionEvent event) {
if (event instanceof IlrAsyncExecutionEndedEvent) {
IlrAsyncExecutionEndedEvent endedEvent = (IlrAsyncExecutionEndedEvent)event;
IlrSessionResponse response = endedEvent.getResponse();
assertNotNull("ended exec event references null response", response);
assertNotNull("expected param not found in response", response.getOutputParameters().get("param1"));
hasExecuted = true;
} else if (event instanceof IlrAsyncExecutionFailedEvent) {
IlrAsyncExecutionFailedEvent failedEvent = (IlrAsyncExecutionFailedEvent)event;
String msg = "observer got notified of an error, e=" + failedEvent.getException().getMessage();
fail(msg);
}
}
};
- Start the execution as asynchronous with the observer.
statelessSession.executeAsynchronous(request, myObserver, -1);
Results
The execution of the ruleset does not block your code.