I did it this way (although it is possible to work directly with the locally instantiated TaskService, see at the end):
This has, however, some drawbacks: all events from the task service can only be recevied by a client that registers locally (in jvm) to the task service. And I'm working on a good solution for a proper JTA integration (injected EntityManager) to support an integration within an appserver.
/*
$Id: LocalHumanTaskHandler.java 234 2011-06-20 06:24:39Z Daniele $
*/
package
ch.niceneasy.process.handler;
import
org.jbpm.process.workitem.wsht.WSHumanTaskHandler;
import
org.jbpm.task.service.TaskClient;
import
org.jbpm.task.service.TaskService;
/**
* The class LocalHumanTaskHandler allows to use Human Tasks on a local
* connection to the task service.
*/
public
class LocalHumanTaskHandler extendsWSHumanTaskHandler {
* Instantiates a new local human task handler.
*
*
@param* the task service (locally instantiated, singleton)
*/
publicLocalHumanTaskHandler(TaskService taskService) {
InJVMConnector inJVMConnector =
newInJVMConnector(taskService);
TaskClient taskClient =
newTaskClient(inJVMConnector);
inJVMConnector.setTaskClient(taskClient);
setClient(taskClient);
}
}
/*
$Id: InJVMConnector.java 234 2011-06-20 06:24:39Z Daniele $
*/
package
ch.niceneasy.process.handler;
import
java.io.IOException;
import
java.util.HashMap;
import
java.util.Map;
import
java.util.concurrent.atomic.AtomicInteger;
import
org.drools.SystemEventListenerFactory;
import
org.drools.task.service.ResponseHandler;
import
org.jbpm.task.service.BaseHandler;
import
org.jbpm.task.service.SessionWriter;
import
org.jbpm.task.service.TaskClient;
import
org.jbpm.task.service.TaskClientConnector;
import
org.jbpm.task.service.TaskClientHandler;
import
org.jbpm.task.service.TaskServerHandler;
import
org.jbpm.task.service.TaskService;
/**
* The class InJVMConnector is a replacement for the mina or
hornetq
* TaskClientConnectors. It allows an in
-JVM connection by implementing the
* TaskClientConnector interface. Although it is also possible to use
* TaskService and TaskServiceSession directly, this approach stays
stricly
* compatible to the jbpm
-human-*
* Tested on JBoss AS 6.1 with multiple datasources, I had to remove the persistence.xml and
* orm.xml from the jpb
-human-task.jar and replace it as follows:
*
*
* ...
* <persistence
-unit name="org.jbpm.task" transaction-type="RESOURCE_LOCAL">
* <non
-jta-data-source>java:/org.jbpm.task</non-jta-data-* <mapping
-file>META-INF/orm-jbpm.xml</mapping-* ...
*
*
* As the persistence layer of jbpm
-human-task performs local transactions (explicit commits),
* the datasource has to be defined as follows:
*
*
* ...
* <no
-tx-datasource>* <jndi-name>org.jbpm.task</jndi-
* ...
*
*
*/
public
class InJVMConnector implements
TaskClientConnector {
privateTaskClient taskClient;
/** The task server handler. */
privateTaskServerHandler taskServerHandler;
private AtomicInteger counter = newAtomicInteger();
/** The local handler. */
private LocalHandler localHandler = newLocalHandler();
* Instantiates a new in jvm connector.
*
*
@param* the task service
*/
publicInJVMConnector(TaskService taskService) {
taskServerHandler =
newTaskServerHandler(taskService,
SystemEventListenerFactory.getSystemEventListener());
}
* (non-Javadoc)
*
* @see org.jbpm.task.service.TaskClientConnector#connect()
*/
@Override
public booleanconnect() {
return true;
}
* (non-Javadoc)
*
* @see org.jbpm.task.service.TaskClientConnector#connect(java.lang.String,
* int)
*/
@Override
public boolean connect(String address, intport) {
returnconnect();
}
* (non-Javadoc)
*
* @see org.jbpm.task.service.TaskClientConnector#disconnect()
*/
@Override
public void disconnect() throwsException {
}
* (non-Javadoc)
*
* @see org.jbpm.task.service.TaskClientConnector#write(java.lang.Object)
*/
@Override
public voidwrite(Object message) {
try{
taskServerHandler
.messageReceived(
newLocalSessionWriter(), message);
}
catch(Exception e) {
// TODOAuto-generated catch block
e.printStackTrace();
}
}
* (non-Javadoc)
*
* @see org.jbpm.task.service.TaskClientConnector#getHandler()
*/
@Override
publicBaseHandler getHandler() {
returnlocalHandler;
}
* (non-Javadoc)
*
* @see org.jbpm.task.service.TaskClientConnector#getName()
*/
@Override
publicString getName() {
return "localConnection";
}
* (non-Javadoc)
*
* @see org.jbpm.task.service.TaskClientConnector#getCounter()
*/
@Override
publicAtomicInteger getCounter() {
returncounter;
}
* Gets the task client.
*
*
@return*/
publicTaskClient getTaskClient() {
returntaskClient;
}
* Sets the task client.
*
*
@param* the new task client
*/
public voidsetTaskClient(TaskClient taskClient) {
this.taskClient = taskClient;
localHandler.setClient(taskClient);
}
* The Class LocalSessionWriter.
*/
public class LocalSessionWriter implementsSessionWriter {
* (non-Javadoc)
*
* @see org.jbpm.task.service.SessionWriter#write(java.lang.Object)
*/
@Override
public void write(Object message) throwsIOException {
try{
localHandler.messageReceived(
null, message);
}
catch(Exception e) {
throw newIOException(e);
}
}
}
* The Class LocalHandler.
*/
public class LocalHandler implementsBaseHandler {
privateTaskClientHandler handler;
/** The response handlers. */
protected Map<Integer, ResponseHandler> responseHandlers = newHashMap<Integer, ResponseHandler>();
* Instantiates a new local handler.
*/
publicLocalHandler() {
handler =
newTaskClientHandler(responseHandlers,
SystemEventListenerFactory.getSystemEventListener());
}
* (non-Javadoc)
*
* @see org.jbpm.task.service.BaseHandler#addResponseHandler(int,
* org.drools.task.service.ResponseHandler)
*/
public void addResponseHandler(intid, ResponseHandler responseHandler) {
responseHandlers.put(id, responseHandler);
}
* Gets the client.
*
*
@return*/
publicTaskClient getClient() {
returnhandler.getClient();
}
* Sets the client.
*
*
@param* the new client
*/
public voidsetClient(TaskClient client) {
handler.setClient(client);
}
* Exception caught.
*
*
@param* the writer
*
@param* the cause
*
@throws* the exception
*/
public voidexceptionCaught(SessionWriter writer, Throwable cause)
throwsException {
handler.exceptionCaught(writer, cause);
}
* Message received.
*
*
@param* the writer
*
@param* the message
*
@throws* the exception
*/
public voidmessageReceived(SessionWriter writer, Object message)
throwsException {
handler.messageReceived(writer, message);
}
}
}
Get user task locally:
/* @Singleton @Startup KnowledgeBaseBean { @Postconstruct ....*/
KnowledgeBaseBean
knowledgeBaseBean
;
##
public List<TaskSummary> getOpenTasks() {TaskServiceSession session =
null;
try {session =
knowledgeBaseBean.getTaskServiceSession();
return session.getTasksAssignedAsPotentialOwner("mary", "en-UK");}
finally {
if (session != null) {session.dispose();
}
}
}
Cheers, Daniele
@Inject