0 Replies Latest reply on Oct 22, 2007 5:15 PM by David Durst

    Message Driven Bean consuming response

    David Durst Newbie

      I am currently running JBoss 4.04.GA.

      I am trying to create a Message Driven Bean that onMessage persists some data using the local interface of a session bean and then sends the JMS client a response.

      The problem: When I send the response message from the MDB, the MDB picks this message up from the Queue and starts onMessage again (Hence it consumes the response). I know there has to be a really simple fix for this but I am just missing it.

      Thanks in advance.

      Here is my code for the MDB:
      public void onMessage(Message message) {
      ObjectMessage msg = null;
      Queue queue = null;
      QueueConnection connection = null;
      QueueSession session = null;
      MessageProducer producer = null;
      try {
      if (message instanceof ObjectMessage) {
      msg = (ObjectMessage) message;
      Order e = (Order) msg.getObject();
      System.out.println("Received new Order");
      e = save(e);
      System.out.println("Created new Order: "+e.getId());
      sendResponse(e,msg.getJMSReplyTo());
      }
      } catch (JMSException e) {
      e.printStackTrace();
      mdc.setRollbackOnly();
      } catch (Throwable te) {
      te.printStackTrace();
      }
      }
      public void sendResponse(Order order, Destination destination) {
      ObjectMessage msg = null;
      Queue queue = null;
      QueueConnection connection = null;
      QueueSession session = null;
      MessageProducer producer = null;
      try {
      InitialContext ctx = new InitialContext();
      queue = (Queue) ctx.lookup("queue/mdb");
      QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
      connection = factory.createQueueConnection();
      session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
      producer = session.createProducer(destination);

      ObjectMessage outMessage = session.createObjectMessage();
      outMessage.setStringProperty("RECIPIENT", "CLIENT");
      outMessage.setObject(order);
      System.out.println("Sending response");
      producer.send(destination,outMessage);
      System.out.println("Response sent");
      producer.close();

      } catch (JMSException e) {
      e.printStackTrace();
      mdc.setRollbackOnly();
      } catch (Throwable te) {
      te.printStackTrace();
      }
      }

      Here is my code for the JMS Client:

      public Order submitOrder(Order order) {
      Queue queue = null;
      QueueConnection connection = null;
      QueueSession session = null;
      MessageProducer producer = null;
      QueueReceiver consumer = null;

      try {
      InitialContext ctx = new InitialContext();
      queue = (Queue) ctx.lookup("queue/mdb");
      QueueConnectionFactory factory =
      (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
      connection = factory.createQueueConnection();
      session = connection.createQueueSession(false,
      QueueSession.AUTO_ACKNOWLEDGE);
      consumer = session.createReceiver(queue);
      producer = session.createProducer(queue);

      ObjectMessage outMessage = session.createObjectMessage();
      outMessage.setObject(order);

      producer.send(outMessage);
      producer.close();
      Message inMessage = consumer.receive(2000);
      if(inMessage != null) {
      if(inMessage instanceof ObjectMessage) {
      ObjectMessage msg = (ObjectMessage)inMessage;
      if(msg.getObject() instanceof Order) {
      return (Order) msg.getObject();
      }
      }
      }

      connection.close();

      } catch (JMSException ex) {
      ex.printStackTrace();
      } catch (NamingException ex) {
      ex.printStackTrace();
      }
      return null;
      }