Hi,
I also have found that you must force to complete the WorkItem after completing a human task.
A more detailed and a little bit cleaner version of Melih and Donald approach also works perfectly, even when the human task server is running on a separate thread or JVM, provided that the process engine JPA persistence is correctly configured and the original statefull knowledge session is restored upon start-up. Some boilerplate code intentionally omitted for the sake of simplicity:
// If no previous statefull knowledge session exists public void createStatefullKnowledgeSession() { //... create kbase, load process definitions, etc ... EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa"); Environment env = KnowledgeBaseFactory.newEnvironment(); env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf); env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager()); ksessionStatefull = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env); int sessionId = ksessionStatefull.getId(); // *** STORE sessionId SOMEWHERE PERSISTENTLY, to be able to // recover this session later upon services restart }
public void loadStatefullKnowledgeSession() {
//... create kbase, load process definitions, etc ...
EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");
Environment env = KnowledgeBaseFactory.newEnvironment();
env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
// *** RECOVER sessionId FROM PERSISTENT STORAGE
// and use it here to recover persisted process engine state
ksessionStatefull = JPAKnowledgeService.loadStatefulKnowledgeSession(sessionId, kbase, null, env);
}
public void complete(long taskId, String userName)
throws Exception {
// ... create the TaskClient (client), and connect it to the task server ...
// first mark the task as completed on the task server
taskOperationHandler = new BlockingTaskOperationResponseHandler();
client.complete(taskId, userName, null, taskOperationHandler);
taskOperationHandler.waitTillDone(1000);
// then retrieve the task to extract the workItemId, where the process instance is waiting
BlockingGetTaskResponseHandler responseHandler = new BlockingGetTaskResponseHandler();
client.getTask(taskId, responseHandler);
responseHandler.waitTillDone(1000);
Task task = responseHandler.getTask();
// force completition of the workItem associated with the task, on which the process instance is waiting
final Map<String, Object> results = new HashMap<String, Object>();
ksessionStatefull.getWorkItemManager().completeWorkItem(task.getTaskData().getWorkItemId(), results);
}
Hope this will be useful to anyone, best regards!