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
- Point to point
- 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
- Use 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 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 destination into the lookup () method of the initial context.
- Create an object of Queuesender by using
createQueuesender() method of QueueSession.
- Create an object of message by using
createTextMesage() method of QueueSession.
- 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
-
Use 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 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 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 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 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
-
The sender and receiver
files can be stored in 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 classpath.