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
- Point to point
- 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
- Use the JNDI mechanism to established
connection with the application server and SPI.
- Create an object of queueconnectionFactory
by using the JNDI name of MESSAGE ORIENTED MIDDLEWARE into the lookup ()
method of the initial context.
- Create an object of queueConnection by
using createQueueConnection () method of QueueConnectionFactory.
- 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.
- Create an object of Queue by using JNDI
name of a destination into the lookup () method of the initial context.
- Create an object of Queuesender by using
createQueuesender() method of QueueSession.
- Create an object of a message by using
createTextMesage() method of QueueSession.
- Use send() method of QueueSender to send
the message.
Example
-
- import java.io.*;
- import java.util.*;
- import javax.naming.*;
- import javax.jms.*;
-
- public class Qsender
- {
- public static void main(String arg[]) throws Exception
- {
-
- 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);
-
- QueueConnectionFactory qconFactory = (QueueConnectionFactory) con.lookup("weblogic.examples.jms.QueueConnectionFactory");
-
- QueueConnection qcon = qconFactory.createQueueConnection();
-
- QueueSession qses = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
-
- javax.jms.Queue q = (javax.jms.Queue) con.lookup("weblogic.examples.jms.exampleQueue");
-
- QueueSender qsend = qses.createSender(q);
- qcon.start();
-
-
- Scanner sc = new Scanner(System.in);
- System.out.println("Write message");
- String msg1 = sc.nextLine();
- Message mes = qses.createTextMessage(msg1);
-
- qsend.send(mes);
- System.out.println("JMS message sent..." + ((TextMessage) mes).getText());
- }
- }
Creation process of Jms receiver
-
Use the jndi mechanism to
establish a connection with the application server and SPI.
-
Create an object of
queueconnectionFactory by using the JNDI name of MESSAGE ORIENTED MIDDLEWARE
into the lookup () method of the initial context.
-
Create an object of
queueConnection by using createQueueConnection() method of
QueueConnectionFactory.
-
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.
-
Create an object of Queue
by using JNDI name of a destination into the lookup () method of the initial context.
-
Create an object of
javax.jmsQueueReceiver by using CreateQueueReceive() method of QueueSession.
-
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.
-
Create an object of
javax.jms.message by using receive () method of QueueReceiver.
-
Convert the message into
javax.jms.textmessage and find the message as a string by using getText()
method of the text message.
Example
-
- 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);
-
-
- QueueConnectionFactory qconFactory = (QueueConnectionFactory) con.lookup("weblogic.examples.jms.QueueConnectionFactory");
-
- QueueConnection qcon = qconFactory.createQueueConnection();
-
- QueueSession qses = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
-
- javax.jms.Queue q = (javax.jms.Queue) con.lookup("weblogic.examples.jms.exampleQueue");
-
- QueueReceiver qrec = qses.createReceiver(q);
- qcon.start();
- UserTransaction utx = (UserTransaction) con.lookup("javax.transaction.UserTransaction");
- utx.begin();
- System.out.println("Started..");
-
- Message mes = qrec.receive();
- System.out.println("Received...");
-
-
- 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
-
The sender and receiver
files can be stored in the same or different location in a network.
-
Compile sender and
receiver files by using weblogic.jar in classpath. This is required for getting javax.jms and javax.transaction package.
-
Create an example domain
in WebLogic for getting the JNDI name of MESSAGE ORIENTED
MIDDLEWARE, destination and transaction server.
-
Start the server in
example domain.
-
Execute the sender by
using weblogic.jar and current directory path in classpath.
-
Execute the receiver by
using weblogic.jar and current directory path in the classpath.