Java EE MDB Send Example

This example shows you how to send a message to an MDB that returns a reply using the same XA transaction

The example will send deploy a simple MDB and demonstrate sending a message and the MDB consuming it

JBoss AS configuration

Please refer to HornetQ Quickstart guide to install it in JBoss AS 5

Example step-by-step

To deploy and start the server, simply type ./build.sh deploy (or build.bat deploy on windows) from the example directory

To run the example, simply type ./build.sh (or build.bat on windows) from the example directory

To remove the example profile, simply type ./build.sh undeploy (or build.bat undeploy on windows) from the example directory

** make sure that JBOSS_HOME is set to the JBoss installation directory

  1. First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the jndi.properties file in the directory config
  2.            initialContext = new InitialContext();
            
  3. We look up the JMS queue object from JNDI
  4.            Queue queue = (Queue) initialContext.lookup("/queue/testQueue");
            
  5. We look up the JMS connection factory object from JNDI
  6.            ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
            
  7. We create a JMS connection
  8.            connection = cf.createConnection();
            
  9. We create a JMS session. The session is created as non transacted and will auto acknowledge messages.
  10.            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
  11. We create a JMS message producer on the session. This will be used to send the messages.
  12.           MessageProducer messageProducer = session.createProducer(queue);
           
  13. We create a JMS text messages that we are going to send.
  14.             TextMessage message = session.createTextMessage("This is a text message");
            
  15. We send messages to the queue
  16.            messageProducer.send(message);
            
  17. The MDB receives the message
    We know the message is a TextMessage so we cast to it.
  18.            TextMessage tm = (TextMessage)message;
            
  19. The MDB gets the text and prints it
  20.             String text = tm.getText();
                System.out.println("message " + text + " received");
                
            
  21. The MDB creates a JMS connection
  22.             conn = connectionFactory.createConnection();
                
            
  23. The MDB creates a JMS session
  24.             Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
                
            
  25. The MDB creates a JMS message producer
  26.             MessageProducer producer = sess.createProducer(replyQueue);
                
            
  27. The MDB creates a text message and sends it to the reply queue
  28.             producer.send(sess.createTextMessage("this is a reply"));
                
            
  29. The Client looks up the reply queue
  30.             queue = (Queue) initialContext.lookup("/queue/replyQueue");
                
            
  31. The Client creates a JMS message consumer
  32.             MessageConsumer messageConsumer = session.createConsumer(queue);
                
            
  33. The Client starts the connection so it can receive messages
  34.             connection.start();
                
            
  35. The Client receives the reply and prints it out
  36.             message = (TextMessage) messageConsumer.receive(5000);
    
                System.out.println("message.getText() = " + message.getText());
                
            
  37. And finally, always remember to close your JMS connections and resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  38.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }