2 Replies Latest reply on May 18, 2006 5:08 PM by Ed cooper

    Simple Contained JMS example with JBoss?

    Ed cooper Newbie



      I could find any simple self-contained JMS examples without JNDI using JBoss, so I grabbed an example from Sun's JMS site, and am wondering what connection information is needed to make this work with JBoss JMS? I have JBoss installed and the example queue working with the ant script. I am excited about an open source JMS implementation, but could use a little help.
      Regards,
      ed

      import javax.jms.QueueConnectionFactory;
      import javax.jms.QueueConnection;
      import javax.jms.QueueSession;
      import javax.jms.QueueSender;
      import javax.jms.QueueReceiver;
      import javax.jms.Queue;
      import javax.jms.Session;
      import javax.jms.Message;
      import javax.jms.TextMessage;
      //Import the classes to use JNDI.
      import javax.naming.*;
      import java.util.*;

      public class JbossJmsSendRecv {

      /**
      * Main method.
      *
      * @param args not used
      *
      */
      public static void main(String[] args) {

      try {
      QueueConnectionFactory myQConnFactory;
      Queue myQueue;

      //This statement can be eliminated if the JNDI code above is used.
      myQConnFactory = new com.sun.messaging.QueueConnectionFactory();

      //Step 3:
      //Create a connection to the Sun Java(tm) System Message Queue Message
      //Service.
      QueueConnection myQConn = myQConnFactory.createQueueConnection();

      //Step 4:
      //Create a session within the connection.
      QueueSession myQSess = myQConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

      //Step 5:
      //Instantiate a Sun Java(tm) System Message Queue Destination
      //administered object.
      myQueue = new com.sun.messaging.Queue("world");


      //Step 6:
      //Create a QueueSender message producer.
      QueueSender myQueueSender = myQSess.createSender(myQueue);


      //Step 7:
      //Create and send a message to the queue.
      TextMessage myTextMsg = myQSess.createTextMessage();
      myTextMsg.setText("Hello World");
      myQueueSender.send(myTextMsg);
      System.out.println("Message sent");
      //session.commit();

      //Step 8:
      //Create a QueueReceiver message consumer.
      QueueReceiver myQueueReceiver = myQSess.createReceiver(myQueue);

      //Step 9:
      //Start the QueueConnection created in step 3.
      myQConn.start();

      //Step 10:
      //Receive a message from the queue.

      Message msg = myQueueReceiver.receive();

      //Step 11:
      //Retreive the contents of the message.
      TextMessage txtMsg = (TextMessage) msg;
      System.out.println("Read Message: " + txtMsg.getText());

      //Step 12:
      //Close the session and connection resources.
      myQSess.close();
      myQConn.close();

      } catch (Exception jmse) {
      System.out.println("Exception occurred : " + jmse.toString());
      jmse.printStackTrace();
      }
      }
      }

        • 1. Re: Simple Contained JMS example with JBoss?
          Tim Fox Master

          In order to use JBoss Messaging you need, at the minimum, to get a reference to a ConnectionFactory and a Destination (a queue or a topic) - these are called "administered objects" in the JMS spec.

          Currently we only provide these objects via JNDI, so in your example you need to replace the line that creates a Sun specific connection factory with a JNDI lookup and the line that creates the Sun specific queue with another lookup.

          Have a look at http://java.sun.com/products/jms/tutorial/1_3_1-fcs/doc/jms_tutorialTOC.html for more simple JMS examples

          • 2. Re: Simple Contained JMS example with JBoss?
            Ed cooper Newbie

            OK, Finally got one working usign the default installed example:

            Passed the following as JVM arguments:

            -Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
            -Djava.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
            -Djava.naming.provider.url=jnp://localhost:1099

            Source:

            import javax.jms.ConnectionFactory;
            import javax.jms.QueueConnectionFactory;
            import javax.jms.QueueConnection;
            import javax.jms.QueueSession;
            import javax.jms.QueueSender;
            import javax.jms.QueueReceiver;
            import javax.jms.Queue;
            import javax.jms.Message;
            import javax.jms.TextMessage;
            //Import the classes to use JNDI.
            import javax.naming.*;

            public class JbossJmsSendRecv {

            /**
            * Main method.
            *
            * @param args not used
            *
            */

            public static void main(String[] args) {

            try {
            InitialContext iniCtx = new InitialContext();
            ConnectionFactory cf = (ConnectionFactory) iniCtx.lookup("/ConnectionFactory");
            QueueConnectionFactory myQConnFactory = (QueueConnectionFactory) cf;
            QueueConnection conn = myQConnFactory.createQueueConnection();

            QueueSession myQSess = conn.createQueueSession(false,QueueSession.AUTO_ACKNOWLEDGE);
            //QueueSession myQSess = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

            Queue myQueue = (Queue) iniCtx.lookup("/queue/testQueue");

            //Create a QueueSender message producer.
            QueueSender myQueueSender = myQSess.createSender(myQueue);

            //Create and send a message to the queue.
            TextMessage myTextMsg = myQSess.createTextMessage();
            myTextMsg.setText("Hello World");
            myQueueSender.send(myTextMsg);
            System.out.println("Message sent");
            //session.commit();

            //Create a QueueReceiver message consumer.
            QueueReceiver myQueueReceiver = myQSess.createReceiver(myQueue);

            //Start the QueueConnection created in step 3.
            conn.start();

            //Receive a message from the queue.
            Message msg = myQueueReceiver.receive();

            //Retreive the contents of the message.
            TextMessage txtMsg = (TextMessage) msg;
            System.out.println("Read Message: " + txtMsg.getText());

            //Close the session and connection resources.
            myQSess.close();
            conn.close();

            } catch (Exception jmse) {
            System.out.println("Exception occurred : " + jmse.toString());
            jmse.printStackTrace();
            }
            }
            }