Java messaging services

Java messaging services

 
It is a mechanism to send and receive messages between two java programs. The sender and receiver program might be a standalone program or web-based. The sender and receiver use a storage place to store message called as MOM (Message-oriented middleware). The MESSAGE ORIENTED MIDDLEWARE can contain various destinations the sender sends message to a destination present in MESSAGE ORIENTED MIDDLEWARE. The receiver receives the message from the destination of MESSAGE ORIENTED MIDDLEWARE. The MESSAGE ORIENTED MIDDLEWARE can be available from the application server. The sender can send a message even if the receiver is absent. Whenever the receiver becomes online then it can receive its message. This is called offline messaging. A sender can send a message for multiple receivers.

Types of messaging model

JMS supports two types of messaging model they are below
  1. Point to point
  2. Publisher- Subscriber
Point to point: - This model supports one to one messaging. In this model, the sender sends a message to a destination called a queue. The receiver receives the message from queue. Whenever the receiver receives the message then MESSAGE ORIENTED MIDDLEWARE removes the message.

Publisher-Subscriber: - This model supports one to many messaging. In this model the sender or publisher sends the message to a destination called as topic. The receiver or subscriber receives the message from the topic. The MESSAGE ORIENTED MIDDLEWARE never removes the message until the allocated memory of the destination get full.

Creation process of JMS sender
  1. Use the JNDI mechanism to established connection with the application server and SPI.
  2. Create an object of queueconnectionFactory by using the JNDI name of MESSAGE ORIENTED MIDDLEWARE into the lookup () method of the initial context. 
  3. Create an object of queueConnection by using createQueueConnection () method of QueueConnectionFactory. 
  4. Create an object of QueueSession by using createQueueSession () method of QueueConnetion.This method accepts two arguments as transaction mode (true/false) and a constant for acknowledgement. 
  5. Create an object of Queue by using JNDI name of a destination into the lookup () method of the initial context. 
  6. Create an object of Queuesender by using createQueuesender() method of QueueSession. 
  7. Create an object of a message by using createTextMesage() method of QueueSession. 
  8. Use send() method of QueueSender to send the message.
Example
  1. //Creating Sender  
  2. import java.io.*;  
  3. import java.util.*;  
  4. import javax.naming.*;  
  5. import javax.jms.*;  
  6.   
  7. public class Qsender   
  8. {  
  9.  public static void main(String arg[]) throws Exception   
  10.  {  
  11.   /*Look up a Connection Factory In JNDI */  
  12.   Properties p1 = new Properties();  
  13.   p1.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");  
  14.   p1.put(Context.PROVIDER_URL, "t3://localhost:7001");  
  15.   InitialContext con = new InitialContext(p1);  
  16.   /*Configure Connection factory*/  
  17.   QueueConnectionFactory qconFactory = (QueueConnectionFactory) con.lookup("weblogic.examples.jms.QueueConnectionFactory");  
  18.   /*Create Connection */  
  19.   QueueConnection qcon = qconFactory.createQueueConnection();  
  20.   /*Create Session */  
  21.   QueueSession qses = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);  
  22.   /*Search for the Destination Queue */  
  23.   javax.jms.Queue q = (javax.jms.Queue) con.lookup("weblogic.examples.jms.exampleQueue");  
  24.   /*Create Message Producer */  
  25.   QueueSender qsend = qses.createSender(q);  
  26.   qcon.start();  
  27.   /*Create Message Object to Send 
  28. Message */  
  29.   Scanner sc = new Scanner(System.in);  
  30.   System.out.println("Write message");  
  31.   String msg1 = sc.nextLine();  
  32.   Message mes = qses.createTextMessage(msg1);  
  33.   /*Use send() to send message*/  
  34.   qsend.send(mes);  
  35.   System.out.println("JMS message sent..." + ((TextMessage) mes).getText());  
  36.  }  
  37. }  
Creation process of Jms receiver
  1. Use the jndi mechanism to establish a connection with the application server and SPI. 
  2. Create an object of queueconnectionFactory by using the JNDI name of MESSAGE ORIENTED MIDDLEWARE into the lookup () method of the initial context. 
  3. Create an object of queueConnection by using createQueueConnection() method of QueueConnectionFactory. 
  4. Create an object of QueueSession by using CreateQueueSession() method of QueueConnetion.This method accepts two arguments as transaction mode(true/false) and a constant for acknowledgement. 
  5. Create an object of Queue by using JNDI name of a destination into the lookup () method of the initial context. 
  6. Create an object of javax.jmsQueueReceiver by using CreateQueueReceive() method of QueueSession. 
  7. Create an object of javax.transaction.usertransaction by using JNDI name of transaction server into the lookup() method of the initial context. Use begin() method of user transaction to begin the transaction. 
  8. Create an object of javax.jms.message by using receive () method of QueueReceiver. 
  9. Convert the message into javax.jms.textmessage and find the message as a string by using getText() method of the text message.
Example
  1. //Creating Receiver  
  2. import java.io.*;  
  3. import java.util.*;  
  4. import javax.transaction.*;  
  5. import javax.naming.*;  
  6. import javax.jms.*;  
  7.   
  8. public class Qreceiver   
  9. {  
  10.  public static void main(String arg[])   
  11.  {  
  12.   try   
  13.   {  
  14.    Properties p1 = new Properties();  
  15.    p1.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");  
  16.    p1.put(Context.PROVIDER_URL, "t3://localhost:7001");  
  17.    Context con = new InitialContext(p1);  
  18.   
  19.    /*Configure Connection Factory*/  
  20.    QueueConnectionFactory qconFactory = (QueueConnectionFactory) con.lookup("weblogic.examples.jms.QueueConnectionFactory");  
  21.    /*Create Connection */  
  22.    QueueConnection qcon = qconFactory.createQueueConnection();  
  23.    /*Create Session */  
  24.    QueueSession qses = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);  
  25.    /*Search for the Destination Queue */  
  26.    javax.jms.Queue q = (javax.jms.Queue) con.lookup("weblogic.examples.jms.exampleQueue");  
  27.    /*Create Message Producer */  
  28.    QueueReceiver qrec = qses.createReceiver(q);  
  29.    qcon.start();  
  30.    UserTransaction utx = (UserTransaction) con.lookup("javax.transaction.UserTransaction");  
  31.    utx.begin();  
  32.    System.out.println("Started..");  
  33.    /*Create Object to Receive  the Message */  
  34.    Message mes = qrec.receive();  
  35.    System.out.println("Received...");  
  36.    /*Use send() to send 
  37.    message */  
  38.    TextMessage tm = (TextMessage) mes;  
  39.    String data = tm.getText();  
  40.    System.out.println("JMS message received..." + data);  
  41.   }   
  42.   catch (Exception e)   
  43.   {  
  44.    e.printStackTrace();  
  45.   }  
  46.  }  
  47. }  
Storing compilation and execution of sender and receiver
  1. The sender and receiver files can be stored in the same or different location in a network. 
  2. Compile sender and receiver files by using weblogic.jar in classpath. This is required for getting javax.jms and javax.transaction package. 
  3. Create an example domain in WebLogic for getting the JNDI name of MESSAGE ORIENTED MIDDLEWARE, destination and transaction server. 
  4. Start the server in example domain. 
  5. Execute the sender by using weblogic.jar and current directory path in classpath. 
  6. Execute the receiver by using weblogic.jar and current directory path in the classpath.


Similar Articles