3 Replies Latest reply on Mar 3, 2008 11:19 AM by kukeltje

    Message Driven Bean not working with jBPM

    chrisrjcox

      Hi,

      I have an issue where I send a simple test text message to a queue. Then the MDB is supposed to pick this message up and display the text. I can't see any issues with my coding, and I have tested to ensure that the message does get stored in the queue, by retrieving it straight after adding to the queue.

      Here is the action handler sending the text to the queue.

      package com.distributed.action;
      
      import org.jbpm.graph.def.ActionHandler;
      import org.jbpm.graph.exe.ExecutionContext;
      
      import javax.jms.ConnectionFactory;
      import javax.jms.Connection;
      import javax.jms.MessageProducer;
      import javax.jms.Queue;
      import javax.jms.Session;
      import javax.jms.TextMessage;
      import javax.naming.InitialContext;
      /**
      import javax.jms.MessageConsumer;
      */
      
      public class SendMessageToQueueForPartTwo implements ActionHandler
      {
      
       private static final long serialVersionUID = 1L;
      
       InitialContext jndi = null;
       ConnectionFactory conFactory = null;
       Connection connection = null;
       Session session = null;
       MessageProducer producer = null;
       TextMessage textMessage = null;
       TextMessage receivedMessage = null;
      
       public void execute(ExecutionContext context) throws Exception
       {
       try
       {
       jndi = new InitialContext();
       System.out.println("SendMessageToQueueForPartTwo: jndi Initial Context created");
      
       conFactory = (ConnectionFactory)jndi.lookup("ConnectionFactory");
       System.out.println("SendMessageToQueueForPartTwo: ConnectionFactory created");
      
      
       connection = conFactory.createConnection();
       System.out.println("SendMessageToQueueForPartTwo: Connection created");
       session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
       System.out.println("SendMessageToQueueForPartTwo: Session created");
       Queue queue = (Queue) jndi.lookup("queue/jbpmQueue");
       System.out.println("SendMessageToQueueForPartTwo: Looked up jbpmQueue");
       if (queue == null)
       {
       System.out.println("SendMessageToQueueForPartTwo: Queue Not Found...Creating Queue");
       queue = session.createQueue("queue/jbpmQueue");
       }
       producer = session.createProducer(queue);
       System.out.println("SendMessageToQueueForPartTwo: Producer Created");
      
       textMessage = session.createTextMessage("Hello, has this worked?");
       System.out.println("SendMessageToQueueForPartTwo: textMessage Created");
       producer.send(textMessage);
       System.out.println("SendMessageToQueueForPartTwo: textMessage sent to " + queue.getQueueName() + " queue");
      
       /** The following code was to check that the message was passed to the Queue
       MessageConsumer consumer = session.createConsumer(queue);
       System.out.println("MessageConsumer created");
      
       connection.start();
       System.out.println("Connection started");
      
       receivedMessage = (TextMessage) consumer.receive();
       System.out.println("The Message "+ receivedMessage.getText());
       */
      
       }
       finally
       {
       if(jndi != null)
       {
       try
       {
       jndi.close();
       }
       catch(Exception e)
       {
       throw e;
       }
       }
       }
       }
      }


      Here is the Message Driven Bean.

      package com.distributed.action;
      
      import javax.ejb.EJB;
      import javax.ejb.MessageDriven;
      import javax.ejb.ActivationConfigProperty;
      import javax.jms.JMSException;
      import javax.jms.Message;
      import javax.jms.MessageListener;
      import javax.jms.TextMessage;
      
      @MessageDriven(mappedName = "jms/PartTwoMDB", activationConfig =
       {
       @ActivationConfigProperty(propertyName="acknowledgeMode", propertyValue = "Auto-acknowledge"),
       @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
       @ActivationConfigProperty(propertyName="destination", propertyValue="/queue/jbpmQueue")
       })
      
      public class PartTwoMDB implements MessageListener
      {
       @EJB
       private static final long serialVersionUID = 1L;
      
       public void onMessage(Message inMessage)
       {
       TextMessage textMessage = null;
       try
       {
       if(inMessage instanceof TextMessage)
       {
       textMessage = (TextMessage) inMessage;
       System.out.println("PartTwoMDB: Message received: "+ textMessage.getText());
       }
       else
       {
       System.out.println("PartTwoMDB: MEssage of wrong type: " + inMessage.getClass().getName());
       }
       }
       catch (JMSException e)
       {
       e.printStackTrace();
       }
       catch (Throwable t)
       {
       t.printStackTrace();
       }
      
       }
      }
      


      I would expect it to print the message text message but nothing happens.

      Would anyone be able to assist?

        • 1. Re: Message Driven Bean not working with jBPM
          kukeltje

          you can store the message on the queue but it is not picked up... does not sound like a jbpm issue....

          what if you make a unit test independent of jbpm? does it work then?

          • 2. Re: Message Driven Bean not working with jBPM
            chrisrjcox

            Thanks for your feedback Ronald.

            In the last few minutes I have managed to get this to work.

            I'm unsure of how exactly Eclipse deploys the java class files, but the MDB needs to be within a .jar, so what I managed to do was remove it from the src/main/java com.distributed.action package and deploy it manually within a new jar file.

            Now it works, even picking up the 10 text messages sent to the queue over the last few days :)

            You are correct this is not specifically a jBPM issue.

            Thanks again for taking the time to respond, much appreciated.

            Chris

            • 3. Re: Message Driven Bean not working with jBPM
              kukeltje

              Chris,

              MDB's always have to be in a jar. That's how you deploy them. You cannot put them in the jbpm processarchive