[jboss-user] [jBPM] - Re: Async service task questions

Calvin Chu do-not-reply at jboss.com
Wed Oct 24 09:37:57 EDT 2012

Calvin Chu [https://community.jboss.org/people/calvinchu101] created the discussion

"Re: Async service task questions"

To view the discussion, visit: https://community.jboss.org/message/772085#772085

Here's my quick and dirty changes for my demo.

First, I implement a WorkItemHandler to send JMS message to a request queue,
public class JmsRequestWorkItemHandler implements WorkItemHandler {
public void executeWorkItem(WorkItem workItem, WorkItemManager workItemMgr) {
          MessageProducer producer ;
//Some initialization of your JMS MessageProducer
            MapMessage mapMsg = session.createMapMessage() ;
            // Put the task ID in JMS message
            mapMsg.setString("taskid",String.valueOf(workItem.getId())) ;
            // Send the JMS message to queue
           //DON'T complete the workitem here
           //workItemMgr.completeWorkItem(workItem.getId(), map) ;

Then register the handler in src/main/resources/META-INF/CustomWorkItemHandlers.conf of the jpm-gwt-console-server project,
  "Log": new org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler(),
    "JmsRequest": new my.process.workitem.jms.JmsRequestWorkItemHandler(),

And then create a MDB in the jpm-gwt-console-server project,
@MessageDriven(name = "ExternalResponseMDB", activationConfig = {
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/response"),
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
public class ExternalResponseMDB implements MessageListener {
    private final static Logger log = Logger.getLogger(ExternalResponseMDB.class.getName());
    public void onMessage(Message rcvMessage) {
        MapMessage msg = null;
        log.info("Message received by ExternalResponseMDB") ;
            if (rcvMessage instanceof MapMessage) {
                msg = (MapMessage) rcvMessage;
                // retrieve sessionId from message
               String id = msg.getString("taskid") ;                
                if (id != null && id.length()>0) {
                    try {
                         // Get knowledge session by StatefulKnowledgeSessionUtil which provided in jbpm-gwt-core-5.3.0.Final.jar
                         // As your MDB is deployed within the jbpm-gwt-console-server.war, gwt-core jar already in lib
                        StatefulKnowledgeSession session = StatefulKnowledgeSessionUtil.getStatefulKnowledgeSession() ;
                        // With the session, you can complete the work item here
                        session.getWorkItemManager().completeWorkItem(Long.parseLong(id), null) ;
                    } catch (Throwable t) {
                        log.severe("completeWorkItem:"+t.getMessage()) ;
            } else {
                log.warning("Message of wrong type: "
                        + rcvMessage.getClass().getName());

Maven clean install your jpm-gwt-console-server project and deploy it. Then you can use the JmsRequest service task in you workflow to send request to external application. The external application should keep the "taskid" and reply back to the response queue such that the MDB can complete the process. Please be noted that the StatefulKnowledgeSessionUtil would try to find and reuse a serialized session ID, not sure if there are any issue if the session created by jbpm-console and MDB shares the same session ID.

As I mention above, it is my quick and dirty approach. I think a more appropiate way to do this is to wait for a signal and let the JmsRequestWorkItemHandler to complete the work item, when response received the MDB then signal the workflow to continue. i.e. calling session.signalEvent(arg0, arg1) in MDB instead of session.getWorkItemManager().completeWorkItem(arg0, arg1)

Like this, unfortunately I don't have any working sample yet.
 https://community.jboss.org/servlet/JiveServlet/showImage/2-772085-19772/HelloWorkflow-image.jpg  https://community.jboss.org/servlet/JiveServlet/downloadImage/2-772085-19772/450-386/HelloWorkflow-image.jpg

Reply to this message by going to Community

Start a new discussion in jBPM at Community

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20121024/33ed9c58/attachment-0001.html 

More information about the jboss-user mailing list