JMS Example
JMS (Java Message Service) is an API that provides the facility to create, send and read messages. It provides loosely coupled, reliable and asynchronous communication
Advantage of JMS
1) Asynchronous: To receive the message, client is not required to send request. Message will arrive automatically to the client.
2) Reliable: It provides assurance that message is delivered.Messaging Domains
There are two types of messaging domains in JMS.
Point-to-Point Messaging Domain
Publisher/Subscriber Messaging Domain1) Point-to-Point (PTP) Messaging Domain.
In PTP model, one message is delivered to one receiver only. Here,
Queue is used as a message oriented middleware (MOM).
The Queue is responsible to hold the message until receiver is ready.
In PTP model, there is no timing dependency between sender and receiver.
Each message has only one consumer
2) Publisher/Subscriber (Pub/Sub) Messaging Domain
In Pub/Sub model, one message is delivered to all the subscribers. It is like broadcasting. Here, Topic is used as a message oriented middleware that is responsible to hold and deliver messages.
In PTP model, there is timing dependency between publisher and subscriber.
Each message can have multiple client.
JMS Queue Example
To develop JMS queue example, you need to install any application server. Here, we are using glassfish3 server where we are creating two JNDI.
Create connection factory named myQueueConnectionFactory
Create destination resource named myQueue
MySender.java
public class MySender {
public static void main(String[] args) {
try
{ //Create and start connection
InitialContext ctx=new InitialContext();
QueueConnectionFactory f=(QueueConnectionFactory)ctx.lookup(“myQueueConnectionFactory”);
QueueConnection con=f.createQueueConnection();
con.start();
//2) create queue session
QueueSession ses=con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
//3) get the Queue object
Queue t=(Queue)ctx.lookup(“myQueue”);
//4)create QueueSender object
QueueSender sender=ses.createSender(t);
//5) create TextMessage object
TextMessage msg=ses.createTextMessage();
//6) write message
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
while(true)
{
System.out.println(“Enter Msg, end to terminate:”);
String s=b.readLine();
if (s.equals(“end”))
break;
msg.setText(s);
//7) send message
sender.send(msg);
System.out.println(“Message successfully sent.”);
}
//8) connection close
con.close();
}catch(Exception e){System.out.println(e);}
}
}
public class MyReceiver {
public static void main(String[] args) {
try{
//1) Create and start connection
InitialContext ctx=new InitialContext();
QueueConnectionFactory f=(QueueConnectionFactory)ctx.lookup(“myQueueConnectionFactory”);
QueueConnection con=f.createQueueConnection();
con.start();
//2) create Queue session
QueueSession ses=con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
//3) get the Queue object
Queue t=(Queue)ctx.lookup(“myQueue”);
//4)create QueueReceiver
QueueReceiver receiver=ses.createReceiver(t);
//5) create listener object
MyListener listener=new MyListener();
//6) register the listener object with receiver
receiver.setMessageListener(listener);
System.out.println(“Receiver1 is ready, waiting for messages…”);
System.out.println(“press Ctrl+c to shutdown…”);
while(true){
Thread.sleep(1000);
}
}catch(Exception e){System.out.println(e);}
}
} import javax.jms.*;
public class MyListener implements MessageListener {
public void onMessage(Message m) {
try{
TextMessage msg=(TextMessage)m;
System.out.println(“following message is received:”+msg.getText());
}catch(JMSException e){System.out.println(e);}
}
}
Run the Receiver class first then Sender class.