Information Technology Reference
In-Depth Information
don't call this method, the process will pause the execution of the process instance and
reach a safe state after executing the
executeWorkItem
method. For the process to
continue on to the next step, someone needs to invoke that method from outside using the
KieSession
exposed manager:
Ksession.getWorkItemManager().completeWorkItem(...);
The other method in
WorkItemHandler
, called
abortWorkItem
, gets invoked
when a specific work item that is waiting for someone to call the
completeWorkItem
method is suddenly stopped. It can be used to clean up any elements that might be waiting
for an external interaction.
You can find an example of creating, registering, and using
WorkItemHandlers
in the
testExternalInterations
methods of the
ExternalInterationsProcess-ExecutionTest
class:
KieSession ksession = KieServices.Factory.get().
getKieClasspathContainer().newKieSession();
//My test implementations of WorkItemHandler
TestSyncWorkItemHandler handler1 = new
TestSyncWorkItemHandler();
TestAsyncWorkItemHandler handler2 = new
TestAsyncWorkItemHandler();
Ksession.getWorkItemManager().registerWorkItemHandler("task1",
handler1);
ksession.getWorkItemManager().registerWorkItemHandler("task2",
handler2);
In the previous code block, we created a session from the default knowledge base of the
classpath container. We also created two variables,
handler1
and
handler2
, that hold
special implementations of the
WorkItemHandler
interface created exclusively for
this exercise. The first one stores the amount of invocations it gets and carries on with the
process execution, as shown in the following code:
public class TestSyncWorkItemHandler implements
WorkItemHandler {
private int invocationCount = 0;
public void executeWorkItem(WorkItem item,