JBoss Community

Re: In memory TaskClient without Mina or JMS

created by Daniele Ulrich in jBPM - View the full discussion

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 extends

WSHumanTaskHandler {

 

 

/**

* Instantiates a new local human task handler.

*

*

@param

taskService

* the task service (locally instantiated, singleton)

*/

 

public

LocalHumanTaskHandler(TaskService taskService) {

InJVMConnector inJVMConnector =

new

InJVMConnector(taskService);

TaskClient taskClient =

new

TaskClient(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-

task framework.

*

* 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:

*

*

<pre>

* ...

* &lt;persistence

-unit name="org.jbpm.task" transaction-

type="RESOURCE_LOCAL"&gt;

* &lt;non

-jta-data-source&gt;java:/org.jbpm.task&lt;/non-jta-data-

source&gt;

* &lt;mapping

-file&gt;META-INF/orm-jbpm.xml&lt;/mapping-

file&gt;

* ...

*

</pre>

*

* As the persistence layer of jbpm

-human-

task performs local transactions (explicit commits),

* the datasource has to be defined as follows:

*

*

<pre>

* ...

* &lt;no

-tx-datasource&gt;

* &lt;jndi-name&gt;org.jbpm.task&lt;/jndi-

name&gt;

* ...

*

</pre>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

*

*/

public

class InJVMConnector implements

TaskClientConnector {

 

 

/** The task client. */

 

private

TaskClient taskClient;

 

 

/** The task server handler. */

 

private

TaskServerHandler taskServerHandler;

 

 

/** The counter. */

 

private AtomicInteger counter = new

AtomicInteger();

 

 

/** The local handler. */

 

private LocalHandler localHandler = new

LocalHandler();

 

 

/**

* Instantiates a new in jvm connector.

*

*

@param

taskService

* the task service

*/

 

public

InJVMConnector(TaskService taskService) {

taskServerHandler =

new

TaskServerHandler(taskService,

SystemEventListenerFactory.getSystemEventListener());

}

 

 

/*

* (non-Javadoc)

*

* @see org.jbpm.task.service.TaskClientConnector#connect()

*/

 

@

Override

 

public boolean

connect() {

 

return true

;

}

 

 

/*

* (non-Javadoc)

*

* @see org.jbpm.task.service.TaskClientConnector#connect(java.lang.String,

* int)

*/

 

@

Override

 

public boolean connect(String address, int

port) {

 

return

connect();

}

 

 

/*

* (non-Javadoc)

*

* @see org.jbpm.task.service.TaskClientConnector#disconnect()

*/

 

@

Override

 

public void disconnect() throws

Exception {

}

 

 

/*

* (non-Javadoc)

*

* @see org.jbpm.task.service.TaskClientConnector#write(java.lang.Object)

*/

 

@

Override

 

public void

write(Object message) {

 

try

{

taskServerHandler

.messageReceived(

new

LocalSessionWriter(), message);

}

catch

(Exception e) {

 

// TODO

Auto-generated catch block

e.printStackTrace();

}

 

}

 

 

/*

* (non-Javadoc)

*

* @see org.jbpm.task.service.TaskClientConnector#getHandler()

*/

 

@

Override

 

public

BaseHandler getHandler() {

 

return

localHandler;

}

 

 

/*

* (non-Javadoc)

*

* @see org.jbpm.task.service.TaskClientConnector#getName()

*/

 

@

Override

 

public

String getName() {

 

return "localConnection"

;

}

 

 

/*

* (non-Javadoc)

*

* @see org.jbpm.task.service.TaskClientConnector#getCounter()

*/

 

@

Override

 

public

AtomicInteger getCounter() {

 

return

counter;

}

 

 

/**

* Gets the task client.

*

*

@return

the task client

*/

 

public

TaskClient getTaskClient() {

 

return

taskClient;

}

 

 

/**

* Sets the task client.

*

*

@param

taskClient

* the new task client

*/

 

public void

setTaskClient(TaskClient taskClient) {

 

this

.taskClient = taskClient;

localHandler.setClient(taskClient);

}

 

 

/**

* The Class LocalSessionWriter.

*/

 

public class LocalSessionWriter implements

SessionWriter {

 

 

/*

* (non-Javadoc)

*

* @see org.jbpm.task.service.SessionWriter#write(java.lang.Object)

*/

 

@

Override

 

public void write(Object message) throws

IOException {

 

try

{

localHandler.messageReceived(

null

, message);

}

catch

(Exception e) {

 

throw new

IOException(e);

}

}

}

 

 

/**

* The Class LocalHandler.

*/

 

public class LocalHandler implements

BaseHandler {

 

 

/** The handler. */

 

private

TaskClientHandler handler;

 

 

/** The response handlers. */

 

protected Map<Integer, ResponseHandler> responseHandlers = new

HashMap<Integer, ResponseHandler>();

 

 

/**

* Instantiates a new local handler.

*/

 

public

LocalHandler() {

handler =

new

TaskClientHandler(responseHandlers,

SystemEventListenerFactory.getSystemEventListener());

}

 

 

/*

* (non-Javadoc)

*

* @see org.jbpm.task.service.BaseHandler#addResponseHandler(int,

* org.drools.task.service.ResponseHandler)

*/

 

public void addResponseHandler(int

id, ResponseHandler responseHandler) {

responseHandlers.put(id, responseHandler);

}

 

 

/**

* Gets the client.

*

*

@return

the client

*/

 

public

TaskClient getClient() {

 

return

handler.getClient();

}

 

 

/**

* Sets the client.

*

*

@param

client

* the new client

*/

 

public void

setClient(TaskClient client) {

handler.setClient(client);

}

 

 

/**

* Exception caught.

*

*

@param

writer

* the writer

*

@param

cause

* the cause

*

@throws

Exception

* the exception

*/

 

public void

exceptionCaught(SessionWriter writer, Throwable cause)

 

throws

Exception {

handler.exceptionCaught(writer, cause);

}

 

 

/**

* Message received.

*

*

@param

writer

* the writer

*

@param

message

* the message

*

@throws

Exception

* the exception

*/

 

public void

messageReceived(SessionWriter writer, Object message)

 

throws

Exception {

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

Reply to this message by going to Community

Start a new discussion in jBPM at Community