April 3, 2011

Java Message Service (JMS) Queue - Hello World Example

1) Configure the JMS Queue in the JBoss 4.2.2GA Server, in the file ${JBOSS_SERVER}/server/default/deploy/jms/jbossmq-destinations-service.xml

<mbean code="org.jboss.mq.server.jmx.Queue"
  name="jboss.mq.destination:service=Queue,name=myQueue1">
  <depends optional-attribute-
  name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>

The name myQueue1 should be unique within the XML file

2) The ConnectionFactory administered object is pre-configured with the default JNDI name 'ConnectionFactory' in JBoss

Note: Add the following jar file to the classpath / build path of the Java Project / Application:

%JBOSS_HOME%\client\jbossall-client.jar

3) Create the QueueSenderClient.java

package info.icontraining.queue;

import javax.jms.*;
import javax.jms.Queue;
import javax.naming.*;
import java.util.*;

public class QueueSenderClient {

   public static void main(String[] args) {
      QueueConnection conn = null;
      QueueSession session = null;
      Queue que;
   
      try {
         Hashtable props = new Hashtable();
         props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory");
         props.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
         props.put(Context.PROVIDER_URL, "localhost:1099");
         props.put("java.naming.rmi.security.manager", "yes");

         InitialContext iniCtx = new InitialContext(props);
         Object tmp = iniCtx.lookup("ConnectionFactory");
         QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;
    
         conn = qcf.createQueueConnection();
         que = (Queue) iniCtx.lookup("queue/myQueue1");

         session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
   
         conn.start();

         QueueSender queueSender = session.createSender(que);
    
         TextMessage message = session.createTextMessage();
         message.setText("This is a TextMessage from QueueSenderClient");

         queueSender.send(message);
 
         queueSender.close();
 
      } catch (NamingException e) {
         e.printStackTrace();
      } catch (JMSException e) {
         e.printStackTrace();
      } finally {
         if (conn != null) {
            try {
               conn.close();
            } catch (JMSException e) {
               e.printStackTrace();
            }
         }  
      }
   }
}

4) Create the QueueReceiverClient.java

package info.icontraining.queue;

import javax.jms.*;
import javax.jms.Queue;
import javax.naming.*;
import java.util.*;

public class QueueReceiverClient {
   public static void main(String[] args) {
      QueueConnection queueConnection = null;

      try {
         Hashtable props = new Hashtable();

         props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory");
         props.put(Context.URL_PKG_PREFIXES, "jboss.naming:org.jnp.interfaces");
         props.put(Context.PROVIDER_URL, "localhost:1099");
         props.put("java.naming.rmi.security.manager", "yes");

         Context context = new InitialContext(props);

         QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) context.lookup("ConnectionFactory");

         String queueName = "queue/myQueue1";
         Queue queue = (Queue) context.lookup(queueName);
         queueConnection = queueConnectionFactory.createQueueConnection();

         QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
         QueueReceiver queueReceiver = queueSession.createReceiver(queue);

         queueConnection.start();

         Message message = queueReceiver.receiveNoWait();

         if (message != null) {
            if (message instanceof TextMessage) {
               TextMessage textMessage = (TextMessage) message;
               System.out.println("Received: " + textMessage.getText());
            }
         }
      } catch (NamingException e) {
         e.printStackTrace();
      } catch (JMSException e) {
         e.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         if (queueConnection != null) {
            try {
               queueConnection.close();
            } catch (JMSException e) {
               e.printStackTrace();
            }
         }
      }
   }
}

5) Run QueueSenderClient first to send the message to the JMS Queue. Next, run QueueReceiverClient to receive the message.

No comments:

Post a Comment