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 message even if the receiver is absent. Whenever receiver becomes online then it can receives its message. This is called offline messaging. A sender can send 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 message to a destination called as 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 topic. The MESSAGE ORIENTED MIDDLEWARE never removes the message till the allocated memory of the destination get full.

Creation process of Jms sender

  1. Use 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 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 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 message by using createTextMesage() method of QueueSession.
     
  8. Use send () method of QueueSender to send the message.

Example

//Creating Sender
import java.io.*;
import java.util.*;
import javax.naming.*;
import javax.jms.*;
 
public class Qsender
{
public static void main(String arg[]) throws Exception
{
/*Look up a Connection Factory In JNDI */
Properties p1=new Properties();
p1.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");  p1.put(Context.PROVIDER_URL,"t3://localhost:7001");
InitialContext con=new InitialContext(p1);

/*Configure Connection factory*/
QueueConnectionFactory qconFactory=(QueueConnectionFactory)con.lookup("weblogic.examples.jms.QueueConnectionFactory");
/*Create Connection */
QueueConnection qcon=qconFactory.createQueueConnection();
/*Create Session */

QueueSession qses=qcon.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
/*Search for the Destination Queue */
javax.jms.Queue q=(javax.jms.Queue)con.lookup("weblogic.examples.jms.exampleQueue");
/*Create Message Producer */
QueueSender qsend=qses.createSender(q);
qcon.start();
 /*Create Message Object to Send
Message */
Scanner sc=new Scanner(System.in);
System.out.println("Write message");
String msg1=sc.nextLine();
Message mes=qses.createTextMessage(msg1);
/*Use send() to send message*/
qsend.send(mes);
System.out.println("JMS message sent..." +((TextMessage)mes).getText());
}
}


Creation process of Jms receiver

  1. Use 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 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 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 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 text message.

Example

//Creating Receiver
import java.io.*;
import java.util.*;
import javax.transaction.*;
import javax.naming.*;
import javax.jms.*;
public class Qreceiver
{
public static void main(String arg[])
{
try
{
Properties p1=new Properties();
p1.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
p1.put(Context.PROVIDER_URL,"t3://localhost:7001");
Context con=new InitialContext(p1);
 
/*Configure Connection Factory*/
QueueConnectionFactory qconFactory=(QueueConnectionFactory)con.lookup("weblogic.examples.jms.QueueConnectionFactory");
/*Create Connection */
QueueConnection qcon=qconFactory.createQueueConnection();
/*Create Session */
QueueSession qses=qcon.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
/*Search for the Destination Queue */
javax.jms.Queue q=(javax.jms.Queue)con.lookup("weblogic.examples.jms.exampleQueue");
/*Create Message Producer */
QueueReceiver qrec=qses.createReceiver(q);
qcon.start();
UserTransaction utx=(UserTransaction)con.lookup("javax.transaction.UserTransaction");
utx.begin();
System.out.println("Started..");
/*Create Object to Receive  the Message */
Message mes=qrec.receive();
System.out.println("Received...");
/*Use send() to send
message */
TextMessage tm=(TextMessage)mes;
String data=tm.getText();
System.out.println("JMS message received..." +data);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}


Storing compilation and execution of sender and receiver

  1. The sender and receiver files can be stored in 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 classpath.

Up Next
    Ebook Download
    View all
    Learn
    View all