Java .

Java JMS With A Queue

A Point to Point JMS Queue

The Sender - BilboRingQueueSender.java

import javax.jms.*;
import javax.naming.*;

public class BilboRingQueueSender {

    public static void main(String[] args) {
        String ringMessage[] = {"One Ring to rule them all," ,
                                "One Ring to find them," , 
                                "One Ring to bring them all" , 
                                "And in the darkness bind them"};
                                
        QueueConnectionFactory queueConnectionFactory = null;
        Queue ringQueue = null;

        try {
            Context jndiContext = new InitialContext();
            queueConnectionFactory = 
              (QueueConnectionFactory) 
                jndiContext.lookup("QueueConnectionFactory");
            ringQueue = (Queue) jndiContext.lookup("RingQueue");
        } catch (NamingException nameEx) {
          System.out.println("Naming Exception: " + 
                              nameEx.toString());
        }

        QueueConnection queueConnection = null;

        try {
            queueConnection = 
              queueConnectionFactory.createQueueConnection();
            QueueSession queueSession = 
              queueConnection.createQueueSession(
                false,Session.AUTO_ACKNOWLEDGE);
            QueueSender queueSender = 
                queueSession.createSender(ringQueue);
            TextMessage textMessage = 
                queueSession.createTextMessage();

            for (int msgCount = 0; 
                 msgCount < ringMessage.length; 
                 msgCount++) {
                textMessage.setText(ringMessage[msgCount]);
                queueSender.send(textMessage);
                System.out.println(" sending line " + 
                                     msgCount + " : " + 
                                     ringMessage[msgCount]);
            }

            textMessage.setText("end of message");
            queueSender.send(textMessage);
            System.out.println(" sending last line " + 
                               " : " + 
                               textMessage.getText());

            queueConnection.close();

            System.out.println(" ring sender closed");

        } catch (javax.jms.JMSException jmsEx) {
          System.out.println("JMS Exception: " + 
                              jmsEx.toString());
        } finally { if (queueConnection != null) 
            {   try 
                { queueConnection.close(); 
                } catch (javax.jms.JMSException jmse) {} 
            } 
        }

    }
}

To download source right-click here and "Save As...".

The Receiver - FrodoRingQueueReceiver.java

import javax.jms.*;
import javax.naming.*;

public class FrodoRingQueueReceiver {

    public static void main(String[] args) {

        QueueConnectionFactory queueConnectionFactory = null;
        Queue ringQueue = null;

        try {

            Context jndiContext = new InitialContext();
            queueConnectionFactory = 
              (QueueConnectionFactory) 
                jndiContext.lookup("QueueConnectionFactory");
            ringQueue = (Queue) jndiContext.lookup("RingQueue");

        } catch (NamingException nameEx) {
          System.out.println("Naming Exception: " + nameEx.toString());
        }

        QueueConnection queueConnection = null;

        try {

            queueConnection = 
              queueConnectionFactory.createQueueConnection();
            QueueSession queueSession = 
              queueConnection.createQueueSession(
                false, Session.AUTO_ACKNOWLEDGE);
            QueueReceiver queueReceiver = 
              queueSession.createReceiver(ringQueue);
            queueConnection.start();

            TextMessage textMessage = null;

            while (true) {
                textMessage = (TextMessage) queueReceiver.receive(1);
                System.out.println(" receiving line " + 
                                   " : " + textMessage.getText());
                if (textMessage.getText().equals("end of message")) {
                    break;
                }
            } 
        
            queueConnection.close();
            
            System.out.println(" ring receiver closed");
            
        } catch (javax.jms.JMSException jmsEx) {
            System.out.println("JMS Exception: " + jmsEx.toString());
        } finally { if (queueConnection != null)
            {   try
                { queueConnection.close();
                } catch (javax.jms.JMSException jmse) {} 
            } 
        }
    }
}


To download source right-click here and "Save As...".

Creating the RingQueue queue, Running BilboRingQueueSender, and then Running FrodoRingQueueReceiver

j2eeadmin -addJmsDestination RingQueue queue                        
                                
java -Djms.properties=%J2EE_HOME%\config\jms_client.properties
 BilboRingQueueSender
Java(TM) Message Service 1.0.2 Reference Implementation 
 (build b14)
 sending line 0 : One Ring to rule them all,
 sending line 1 : One Ring to find them,
 sending line 2 : One Ring to bring them all
 sending line 3 : And in the darkness bind them
 sending last line  : end of message
 ring sender closed

java -Djms.properties=%J2EE_HOME%\config\jms_client.properties FrodoRingQueueReceiver Java(TM) Message Service 1.0.2 Reference Implementation (build b14) receiving line : One Ring to rule them all, receiving line : One Ring to find them, receiving line : One Ring to bring them all receiving line : And in the darkness bind them receiving line : end of message ring receiver closed

References


online
Java Message Service Tutorial from Sun
Books
Enterprise Java Beans by Richard Monson-Haefel
Mastering Enterprise JavaBeans by Ed Roman
Lord of the Rings by J.R.R. Tolkien
Comments
Sign in to be the first to comment on Java JMS With A Queue.