Java Messaging Service





In this particular I will give some brief idea about how to configure weblogic for sending and receiving messages using JMS.

Let's try to find out what JMS is all about.  JMS stand for java messaging service. This is mainly used for sending asynchronous message to the receiver. After sending the message sender does not wait for the response from the receiver and proceed with it's work. If you want to get more information about JMS you can read nice tutorial on http://download-llnw.oracle.com/javaee/1.3/jms/tutorial/1_3_1-fcs/doc/jms_tutorialTOC.html

Now, lets configure weblogic for JMS.

Steps :
 1 ) Create Connection Factory
2) Create JMS Server
3) Create JMS Destination






Click on Services--->Messaging--->JMS Module



Click New






Click Next


Select AdminServer and then Next




Select Would you like to add resources to thin JMS system module?
Then select FINISH.


After that select the ServicesàMessagingàJMS Modules
Now We have to create Connection Factory
Click on demoJMS module that you have created earlier.
And click on NEW.






Select connection factory and select next
Give a Name and JNDI name. Remember this JNDI name we will be using to lookup for the connection factory. And then select NEXT and FINISH.
Now Lets Create JMS Server.
Click on ServiceàMessagingàJMS Server
Click on New and Give a Name to JMS Server Than Click Next and Select Target As Admin Server





Now We need to create a Queue.
Click ServicesàMessagingàJMS Modules and then click on the module that you have created and then select new click on Queue.




Give a name to the queue and JNDI name. Remember this JNDI name we will be using to look up the queue.

Click Finish.

Now everything is set up in weblogic.
Now We have to write Sample Java sender and receiver that can send and receive message.
The Sender: Simpley sender will put the message in the queue and the receiver receives the message from the queue.
Sender Program :
Create a simple java project in eclipse. Now for this we need to add weblogic.jar in our class path.
So,
 And add weblogic.jar file using Add External Jar button.

TestSender.java
package com.test;
import java.util.Hashtable;

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

public class TestSender {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub

            String queueName = null;
            Context jndiContext = null;
            QueueConnectionFactory queueConnectionFactory = null;
            QueueConnection queueConnection = null;
            QueueSession queueSession = null;
            Queue queue = null;
            QueueSender queueSender = null;
            TextMessage message = null;
           
            queueName = new String("queue");
           
            try {
                 
                  Hashtable properties = new Hashtable();
                   properties.put(Context.INITIAL_CONTEXT_FACTORY,
                                  "weblogic.jndi.WLInitialContextFactory");
                   // NOTE: The port number of the server is provided in the next line,
                   //       followed by the userid and password on the next two lines.
                   properties.put(Context.PROVIDER_URL, "t3://localhost:7001");
                   properties.put(Context.SECURITY_PRINCIPAL, "weblogic user name");
                   properties.put(Context.SECURITY_CREDENTIALS, "your password");
                 
                 
                  jndiContext = new InitialContext(properties);
                  } catch (NamingException e) {
                  System.out.println("Could not create JNDI API " +
                  "context: " + e.toString());
                  System.exit(1);
                  }
                 
                  try {
                        queueConnectionFactory = (QueueConnectionFactory)
                        jndiContext.lookup("jms/myCF");
                        queue = (Queue) jndiContext.lookup("jms/myQueue");
                        } catch (NamingException e) {
                        System.out.println("JNDI API lookup failed: " +
                        e.toString());
                        System.exit(1);
                        }
                       
                       
                        try {
                              queueConnection =
                              queueConnectionFactory.createQueueConnection();
                              queueSession =
                              queueConnection.createQueueSession(false,
                              Session.AUTO_ACKNOWLEDGE);
                              queueSender = queueSession.createSender(queue);
                              message = queueSession.createTextMessage();
                              for (int i = 0; i < 4; i++) {
                              message.setText("This is message " + (i + 1));
                              System.out.println("Sending message: " +
                              message.getText());
                              queueSender.send(message);
                              }
                              queueSender.send(queueSession.createMessage());
                        } catch (JMSException e) {
                        System.out.println("Exception occurred: " +
                        e.toString());
                        } finally {
                        if (queueConnection != null) {
                        try {
                        queueConnection.close();
                        } catch (JMSException e) {}
                        }
                        }
                        }
                       
           
      }

Now let’s run the sender and test it.


Lets Moniter JMS Queue that we have created earlier in Weblogic :




There are 5 Messages in the queue pending to be read.
Receiver Program:
Configure the java project as sender and use this code to receive the Message from the queue.
TestReceiver.java
package com.test;
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.*;
public class TestRecever {
/**
                 * @param args
                 */
                public static void main(String[] args) {
                               
                                String queueName = null;
                                Context jndiContext = null;
                                QueueConnectionFactory queueConnectionFactory = null;
                                QueueConnection queueConnection = null;
                                QueueSession queueSession = null;
                                Queue queue = null;
                                QueueReceiver queueReceiver = null;
                                TextMessage message = null;

                                try {
                                                Hashtable properties = new Hashtable();
                                       properties.put(Context.INITIAL_CONTEXT_FACTORY,
                                                      "weblogic.jndi.WLInitialContextFactory");
                                 
                                       properties.put(Context.PROVIDER_URL, "t3://localhost:7001");
                                       properties.put(Context.SECURITY_PRINCIPAL, "user ID in weblogic");
                                       properties.put(Context.SECURITY_CREDENTIALS, "your password");
                                               
                                                jndiContext = new InitialContext(properties);
                                                } catch (NamingException e) {
                                                System.out.println("Could not create JNDI API " +
                                                "context: " + e.toString());
                                                System.exit(1);
                                                }
                                               
                                               
                                                /*
                                                * Look up connection factory and queue. If either does
                                                * not exist, exit.
                                                */
                                                try {
                                                queueConnectionFactory = (QueueConnectionFactory)
                                                jndiContext.lookup("jms/myCF");
                                                queue = (Queue) jndiContext.lookup("jms/myQueue");
                                                } catch (NamingException e) {
                                                System.out.println("JNDI API lookup failed: " +
                                                e.toString());
                                                System.exit(1);
                                                }

                                               
                                               
                                                try {
                                                                queueConnection =
                                                                queueConnectionFactory.createQueueConnection();
                                                                queueSession =
                                                                queueConnection.createQueueSession(false,
                                                                Session.AUTO_ACKNOWLEDGE);
                                                                queueReceiver = queueSession.createReceiver(queue);
                                                                queueConnection.start();
                                                                while (true) {
                                                                Message m = queueReceiver.receive(1);
                                                                if (m != null) {
                                                                if (m instanceof TextMessage) {
                                                                message = (TextMessage) m;
                                                                System.out.println("Reading message: " +
                                                                message.getText());
                                                                } else {
                                                                break;
                                                                }
                                                                }
                                                                }
                                                                } catch (JMSException e) {
                                                                System.out.println("Exception occurred: " +
                                                                e.toString());
                                                                } finally {
                                                                if (queueConnection != null) {
                                                                try {
                                                                queueConnection.close();
                                                                } catch (JMSException e) {}
                                                                }

                                                                }
               
                }
}
Now lets run the receiver and test the Queue in weblogic.


As the messages are read there is not messages pending.
Hope this will help.
Thanks
Mitesh Patel