Jms 2
Jms 2
com/jms-tutorial
Home EJB3 Core Java Servlet JSP Struts2 Mail API Hibernate Spring Android Design P. Quiz Projects
1 of 11 8/18/2023, 8:38 PM
JMS Tutorial - javatpoint https://www.javatpoint.com/jms-tutorial
JMS Tutorial
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.
Understanding Messaging
Messaging is a technique to communicate applications or software components.
JMS is mainly used to send and receive message from one application to another.
Requirement of JMS
Generally, user sends message to application. But, if we want to send message from one application to another, we need to
use JMS API.
Consider a scenario, one application A is running in INDIA and another application B is running in USA. To send message from
A application to B, we need to use JMS.
Advantage of JMS
1) Asynchronous: To receive the message, client is not required to send request. Message will arrive automatically to the
client.
Messaging Domains
There are two types of messaging domains in JMS.
In PTP model, one message is delivered to one receiver only. Here, Queue is used as a message oriented middleware
(MOM).
2 of 11 8/18/2023, 8:38 PM
JMS Tutorial - javatpoint https://www.javatpoint.com/jms-tutorial
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.
After creating JNDI, create server and receiver application. You need to run server and receiver in different console. Here, we are
using eclipse IDE, it is opened in different console by default.
Click on
Downloads theCh17.pptx
JMS Resource -> Connection Factories -> New,
1504860721SE-MOD19-… life cyclenow
2.pdf write the pool name and select the Resource
100% Type
Clear as
3 of 11 8/18/2023, 8:38 PM
JMS Tutorial - javatpoint https://www.javatpoint.com/jms-tutorial
Click on the JMS Resource -> Destination Resources -> New, now write the JNDI name and physical destination name then click
on ok button.
Let's see
Downloads the Sender and Receiver 1504860721SE-MOD19-…
Ch17.pptx code. Note that Receiver is attached
life cycle 2.pdf with listener which will be invoked when
100%user
Clearsends
4 of 11 8/18/2023, 8:38 PM
JMS Tutorial - javatpoint https://www.javatpoint.com/jms-tutorial
message.
File: MySender.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.naming.*;
import javax.jms.*;
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);}
}
Downloads Ch17.pptx 1504860721SE-MOD19-… life cycle 2.pdf 100% Clear
5 of 11 8/18/2023, 8:38 PM
JMS Tutorial - javatpoint https://www.javatpoint.com/jms-tutorial
}
File: MyReceiver.java
import javax.jms.*;
import javax.naming.InitialContext;
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);}
}
}
File: MyListener.java
import javax.jms.*;
public class MyListener implements MessageListener {
6 of 11 8/18/2023, 8:38 PM
JMS Tutorial - javatpoint https://www.javatpoint.com/jms-tutorial
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);}
}
}
File: MySender.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.naming.*;
import javax.jms.*;
public class MySender {
public static void main(String[] args) {
try
{ //Create and start connection
InitialContext ctx=new InitialContext();
TopicConnectionFactory f=(TopicConnectionFactory)ctx.lookup("myTopicConnectionFactory");
TopicConnection con=f.createTopicConnection();
con.start();
//2) create queue session
TopicSession ses=con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
//3) get the Topic object
Topic t=(Topic)ctx.lookup("myTopic");
//4)create TopicPublisher object
TopicPublisher publisher=ses.createPublisher(t);
//5) create TextMessage object
TextMessage msg=ses.createTextMessage();
//6) write message
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
Downloads while(true)
Ch17.pptx 1504860721SE-MOD19-… life cycle 2.pdf 100% Clear
7 of 11 8/18/2023, 8:38 PM
JMS Tutorial - javatpoint https://www.javatpoint.com/jms-tutorial
{
System.out.println("Enter Msg, end to terminate:");
String s=b.readLine();
if (s.equals("end"))
break;
msg.setText(s);
//7) send message
publisher.publish(msg);
System.out.println("Message successfully sent.");
}
//8) connection close
con.close();
}catch(Exception e){System.out.println(e);}
}
}
File: MyReceiver.java
import javax.jms.*;
import javax.naming.InitialContext;
public class MyReceiver {
public static void main(String[] args) {
try {
//1) Create and start connection
InitialContext ctx=new InitialContext();
TopicConnectionFactory f=(TopicConnectionFactory)ctx.lookup("myTopicConnectionFactory");
TopicConnection con=f.createTopicConnection();
con.start();
//2) create topic session
TopicSession ses=con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
//3) get the Topic object
Topic t=(Topic)ctx.lookup("myTopic");
//4)create TopicSubscriber
TopicSubscriber receiver=ses.createSubscriber(t);
//5) create listener object
MyListener listener=new MyListener();
//6) register the listener object with subscriber
receiver.setMessageListener(listener);
System.out.println("Subscriber1 is ready, waiting for messages...");
Downloads Ch17.pptx 1504860721SE-MOD19-… life cycle 2.pdf 100% Clear
8 of 11 8/18/2023, 8:38 PM
JMS Tutorial - javatpoint https://www.javatpoint.com/jms-tutorial
System.out.println("press Ctrl+c to shutdown...");
while(true){
Thread.sleep(1000);
}
}catch(Exception e){System.out.println(e);}
}
}
File: MyListener.java
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);}
}
}
Feedback
9 of 11 8/18/2023, 8:38 PM
JMS Tutorial - javatpoint https://www.javatpoint.com/jms-tutorial
Preparation
Trending Technologies
10 of 11 8/18/2023, 8:38 PM
JMS Tutorial - javatpoint https://www.javatpoint.com/jms-tutorial
B.Tech / MCA
11 of 11 8/18/2023, 8:38 PM