SlideShare a Scribd company logo
1 of 15
A Study in JMS
(Java Messaging
Service)
BY
Prabhat Gangwar
JMS OverviewJMS Overview
The Java Message Service is a Java API that
allows applications to create, send, receive,
and read messages
The JMS API minimizes the set of concepts a
programmer must learn to use messaging
products but provides enough features to
support sophisticated messaging applications
JMS Overview (cont)JMS Overview (cont)
The JMS API enables communication that is
not only loosely coupled but also:
Asynchronous. A JMS provider can deliver
messages to a client as they arrive; a client does
not have to request messages in order to receive
them.
Reliable. The JMS API can ensure that a
message is delivered once and only once. Lower
levels of reliability are available for applications
that can afford to miss messages or to receive
duplicate messages.
JMS Overview (cont)JMS Overview (cont)
Messages can be consumed in either of two ways:
Synchronously. A subscriber or a receiver explicitly fetches
the message from the destination by calling the receive
method. The receive method can block until a message
arrives or can time out if a message does not arrive within a
specified time limit.
Asynchronously. A client can register a message listener
with a consumer. A message listener is similar to an event
listener. Whenever a message arrives at the destination, the
JMS provider delivers the message by calling the listener's
onMessage() method, which acts on the contents of the
message.
JMS And JNDIJMS And JNDI
JMS does not not define a standard address syntax by which
clients communicate with each other. Instead JMS utilizes Java
Naming & Directory Interface(JNDI).
JNDI provides a mechanism by which clients can perform a
“lookup” to find the correct information to connect to each other.
Using JNDI provides the following advantages:
It hides provider-specific details from JMS clients.
It abstracts JMS administrative information into Java objects that
are easily organized and administrated from a common
management console.
Since there will be JNDI providers for all popular naming services,
this means JMS providers can deliver one implementation of
administered objects that will run everywhere. Thereby eliminating
deployment and configuration issues.
JMS Advantages over RPCJMS Advantages over RPC
RPC relies on the physical connection of the client
and server to the network; it is a synchronous
protocol.
What happens if the client is disconnected?
Network could go down
Client could be a laptop that is used on the road.
In this case, the end user might still want to carry on
working, but can't if an RPC model is being used—at
least not without a great deal of work by the
programmer.
Messaging BenefitsMessaging Benefits
JMS vs RPCJMS vs RPC
Messaging provides the ability to send data
asynchronously and in a disconnected manner.
Two messaging models
Point-to-point
Publish-and-Subscribe
In an email like model, these would be equivalent to
sending and receiving e-mails directly (point-to-point),
or subscribing to a list server and sending and
receiving e-mails through the list server (publish-and-
subscribe).
What is the cost?What is the cost?
JMS vs RPCJMS vs RPC
Applications become asynchronous by nature
What if we require a method to give us a return
value?
What if we require the data (the messages) to be
delivered in a specific order?
Using messaging, JMS, we have to deal with
these problems ourselves. RPC handled
these issues for the programmer.
Messages ExplainedMessages Explained
A message typically consists of a header and a body.
The message header contains vendor-specified
values, but could also contain application-specific
data as well.
Headers are typically name/value pairs.
The body contains data; the type of the data is
defined by the specification.
Text
A serialized Java object
One of a number of other types of data.
Publisher SamplePublisher Sample
See MyTopicPublisher.java for source.
1. Perform a JNDI API lookup of the TopicConnectionFactory and topic
topic = (Topic) jndiContext.lookup(topicName);
2. Create a connection and a session
topicConnection = topicConnectionFactory.createTopicConnection();
topicSession = topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
3. Create a TopicPublisher
topicPublisher = topicSession.createPublisher(topic);
4. Create a TextMessage
Message = topicSession.createTextMessage();
message.setText("This is message " + (i + 1));
5. Publishe one or more messages to the topic
topicPublisher.publish(message);
6. Close the connection, which automatically closes the session and
TopicPublisher
Subscriber SampleSubscriber Sample
See MyTopicSubscriber.java for source.
1.Perform a JNDI API lookup of the TopicConnectionFactory and
topic (same as publisher)
2.Create a connection and a session (same as publisher)
3.Create a TopicSubscriber
topicSubscriber = topicSession.createSubscriber(topic);
4.Create an instance of the TextListener class and registers it as
the message listener for the TopicSubscriber
topicListener = new TextListener();
topicSubscriber.setMessageListener(topicListener);
5.Start the connection, causing message delivery to begin
topicConnection.start();
6.Close the connection, which automatically closes the session
and TopicSubscriber
topicConnection.close();
TextListener SampleTextListener Sample
1. public void onMessage(Message message) {
2. TextMessage msg = null;
3.
4. try {
5. if (message instanceof TextMessage) {
6. msg = (TextMessage) message;
7. System.out.println("Reading message: " + msg.getText());
8. } else {
9. System.out.println("Message of wrong type: " +
10. message.getClass().getName());
11. }
12. } catch (JMSException e) {
13. System.out.println("JMSException in onMessage(): " + e.toString());
14. } catch (Throwable t) {
15. System.out.println("Exception in onMessage():" + t.getMessage());
16. }
17.}
Running The SampleRunning The Sample
1. Start the JMS provider. In this case the J2EE SDK
From a command prompt run the following command:
j2ee –verbose
Wait until the server displays the message "J2EE server startup
complete
1. Create the Administered Object. This is the object to which
you will publish and subscribe.
From a second command prompt run the following command
j2eeadmin -addJmsDestination CS522Topic topic
To verify the topic was created, view the list of Administered
Objects by typing:
j2eeadmin –listJmsDestination
Running The Sample (cont)Running The Sample (cont)
3. Run the subscriber program
From a command prompt run the following command within the
directory that contains the MyTopicSubscriber.class:
java -Djms.properties=%J2EE_HOME
%configjms_client.properties MyTopicSubscriber
-topic=CS522Topic
3. Run the Publisher program
From a command prompt run the following command within the
directory that contains the MyTopicPublisher.class:
java -Djms.properties=%J2EE_HOME
%configjms_client.properties MyTopicPublisher
-topic=CS522Topic -count=500 -delay=500
5. You will see text output in both the Publisher and Subscriber
windows
ReferencesReferences
http://java.sun.com/products/jms/tutorial/1_3_1
http://java.sun.com/products/jndi/

More Related Content

What's hot

Mom those things v1
Mom those things v1 Mom those things v1
Mom those things v1
von gosling
 
Java message service
Java message serviceJava message service
Java message service
Veeramani S
 
JMS Providers Overview
JMS Providers OverviewJMS Providers Overview
JMS Providers Overview
Vadym Lotar
 
Message Driven Beans (6)
Message Driven Beans (6)Message Driven Beans (6)
Message Driven Beans (6)
Abdalla Mahmoud
 

What's hot (20)

Spring JMS
Spring JMSSpring JMS
Spring JMS
 
Jms intro
Jms introJms intro
Jms intro
 
Jms
JmsJms
Jms
 
Enterprise messaging with jms
Enterprise messaging with jmsEnterprise messaging with jms
Enterprise messaging with jms
 
Mom those things v1
Mom those things v1 Mom those things v1
Mom those things v1
 
Jms
JmsJms
Jms
 
Jms
JmsJms
Jms
 
Java message service
Java message serviceJava message service
Java message service
 
JMS Providers Overview
JMS Providers OverviewJMS Providers Overview
JMS Providers Overview
 
Jms
JmsJms
Jms
 
Jms deep dive [con4864]
Jms deep dive [con4864]Jms deep dive [con4864]
Jms deep dive [con4864]
 
Messaging in Java
Messaging in JavaMessaging in Java
Messaging in Java
 
Message Driven Beans (6)
Message Driven Beans (6)Message Driven Beans (6)
Message Driven Beans (6)
 
Mule jms-topics
Mule jms-topicsMule jms-topics
Mule jms-topics
 
19 08-22 introduction to activeMQ
19 08-22 introduction to activeMQ19 08-22 introduction to activeMQ
19 08-22 introduction to activeMQ
 
An Introduction to the Message Queuning Technology
An Introduction to the Message Queuning TechnologyAn Introduction to the Message Queuning Technology
An Introduction to the Message Queuning Technology
 
Wcf faq
Wcf faqWcf faq
Wcf faq
 
Jms session (1)
Jms session (1)Jms session (1)
Jms session (1)
 
Introduction tojms
Introduction tojmsIntroduction tojms
Introduction tojms
 
Weblogic - Introduction to configure JMS
Weblogic  - Introduction to configure JMSWeblogic  - Introduction to configure JMS
Weblogic - Introduction to configure JMS
 

Viewers also liked

WSO2Con US 2013 - Creating the API Centric Enterprise Towards a Connected Bus...
WSO2Con US 2013 - Creating the API Centric Enterprise Towards a Connected Bus...WSO2Con US 2013 - Creating the API Centric Enterprise Towards a Connected Bus...
WSO2Con US 2013 - Creating the API Centric Enterprise Towards a Connected Bus...
WSO2
 
IPAAS_information on your terms
IPAAS_information on your termsIPAAS_information on your terms
IPAAS_information on your terms
Market Engel SAS
 
IPaaS: Cloud Integration Analysis
IPaaS: Cloud Integration AnalysisIPaaS: Cloud Integration Analysis
IPaaS: Cloud Integration Analysis
Jesus Rodriguez
 

Viewers also liked (16)

FINAL
FINALFINAL
FINAL
 
Dr Deepak cv
Dr Deepak cvDr Deepak cv
Dr Deepak cv
 
JMS java messaging service
JMS java messaging serviceJMS java messaging service
JMS java messaging service
 
WSO2Con US 2013 - Creating the API Centric Enterprise Towards a Connected Bus...
WSO2Con US 2013 - Creating the API Centric Enterprise Towards a Connected Bus...WSO2Con US 2013 - Creating the API Centric Enterprise Towards a Connected Bus...
WSO2Con US 2013 - Creating the API Centric Enterprise Towards a Connected Bus...
 
SnapLogic Adds Support for Kafka and HDInsight to Elastic Integration Platform
SnapLogic Adds Support for Kafka and HDInsight to Elastic Integration PlatformSnapLogic Adds Support for Kafka and HDInsight to Elastic Integration Platform
SnapLogic Adds Support for Kafka and HDInsight to Elastic Integration Platform
 
IPAAS_information on your terms
IPAAS_information on your termsIPAAS_information on your terms
IPAAS_information on your terms
 
Cloud-Con: Integration & Web APIs
Cloud-Con: Integration & Web APIsCloud-Con: Integration & Web APIs
Cloud-Con: Integration & Web APIs
 
Anypoint mq (mulesoft) introduction
Anypoint mq (mulesoft)  introductionAnypoint mq (mulesoft)  introduction
Anypoint mq (mulesoft) introduction
 
IPaaS
IPaaSIPaaS
IPaaS
 
SnapLogic's Latest Elastic iPaaS Release Adds Hybrid Links for Spark, Cortana...
SnapLogic's Latest Elastic iPaaS Release Adds Hybrid Links for Spark, Cortana...SnapLogic's Latest Elastic iPaaS Release Adds Hybrid Links for Spark, Cortana...
SnapLogic's Latest Elastic iPaaS Release Adds Hybrid Links for Spark, Cortana...
 
Cloud fuse-apachecon eu-2012
Cloud fuse-apachecon eu-2012Cloud fuse-apachecon eu-2012
Cloud fuse-apachecon eu-2012
 
API Athens Meetup - API standards 22.03.2016
API Athens Meetup - API standards 22.03.2016API Athens Meetup - API standards 22.03.2016
API Athens Meetup - API standards 22.03.2016
 
MQ Light for Bluemix - IBM Interconnect 2015 session AME4183
MQ Light for Bluemix - IBM Interconnect 2015 session AME4183MQ Light for Bluemix - IBM Interconnect 2015 session AME4183
MQ Light for Bluemix - IBM Interconnect 2015 session AME4183
 
MuleSoft CloudHub FAQ
MuleSoft CloudHub FAQMuleSoft CloudHub FAQ
MuleSoft CloudHub FAQ
 
Business Agility through Self-Service Messaging - InterConnect 2016
Business Agility through Self-Service Messaging - InterConnect 2016Business Agility through Self-Service Messaging - InterConnect 2016
Business Agility through Self-Service Messaging - InterConnect 2016
 
IPaaS: Cloud Integration Analysis
IPaaS: Cloud Integration AnalysisIPaaS: Cloud Integration Analysis
IPaaS: Cloud Integration Analysis
 

Similar to Jms

Test DB user
Test DB userTest DB user
Test DB user
techweb08
 
test validation
test validationtest validation
test validation
techweb08
 
Apache ActiveMQ and Apache Camel
Apache ActiveMQ and Apache CamelApache ActiveMQ and Apache Camel
Apache ActiveMQ and Apache Camel
Omi Om
 
WebLogic JMS System Best Practices
WebLogic JMS System Best PracticesWebLogic JMS System Best Practices
WebLogic JMS System Best Practices
Trivadis
 
ActiveMQ Configuration
ActiveMQ ConfigurationActiveMQ Configuration
ActiveMQ Configuration
Ashish Mishra
 
Indianapolis mule soft_meetup_12_june_2021
Indianapolis mule soft_meetup_12_june_2021Indianapolis mule soft_meetup_12_june_2021
Indianapolis mule soft_meetup_12_june_2021
ikram_ahamed
 
High powered messaging with RabbitMQ
High powered messaging with RabbitMQHigh powered messaging with RabbitMQ
High powered messaging with RabbitMQ
James Carr
 

Similar to Jms (20)

Jms
JmsJms
Jms
 
Test DB user
Test DB userTest DB user
Test DB user
 
test validation
test validationtest validation
test validation
 
Apache ActiveMQ and Apache Camel
Apache ActiveMQ and Apache CamelApache ActiveMQ and Apache Camel
Apache ActiveMQ and Apache Camel
 
What's new in JMS 2.0 - OTN Bangalore 2013
What's new in JMS 2.0 - OTN Bangalore 2013What's new in JMS 2.0 - OTN Bangalore 2013
What's new in JMS 2.0 - OTN Bangalore 2013
 
About jms
About jmsAbout jms
About jms
 
#7 (Java Message Service)
#7 (Java Message Service)#7 (Java Message Service)
#7 (Java Message Service)
 
Mule jms
Mule   jmsMule   jms
Mule jms
 
WebLogic JMS System Best Practices
WebLogic JMS System Best PracticesWebLogic JMS System Best Practices
WebLogic JMS System Best Practices
 
Mule with jms
Mule with jmsMule with jms
Mule with jms
 
ActiveMQ Configuration
ActiveMQ ConfigurationActiveMQ Configuration
ActiveMQ Configuration
 
Messaging Frameworks using JMS
Messaging Frameworks using JMS Messaging Frameworks using JMS
Messaging Frameworks using JMS
 
Using the JMS 2.0 API with Apache Pulsar - Pulsar Virtual Summit Europe 2021
Using the JMS 2.0 API with Apache Pulsar - Pulsar Virtual Summit Europe 2021Using the JMS 2.0 API with Apache Pulsar - Pulsar Virtual Summit Europe 2021
Using the JMS 2.0 API with Apache Pulsar - Pulsar Virtual Summit Europe 2021
 
ActiveMQ interview Questions and Answers
ActiveMQ interview Questions and AnswersActiveMQ interview Questions and Answers
ActiveMQ interview Questions and Answers
 
Jms слайды
Jms слайдыJms слайды
Jms слайды
 
Jms topics
Jms topicsJms topics
Jms topics
 
Indianapolis mule soft_meetup_12_june_2021
Indianapolis mule soft_meetup_12_june_2021Indianapolis mule soft_meetup_12_june_2021
Indianapolis mule soft_meetup_12_june_2021
 
Java Messaging Services
Java Messaging ServicesJava Messaging Services
Java Messaging Services
 
High powered messaging with RabbitMQ
High powered messaging with RabbitMQHigh powered messaging with RabbitMQ
High powered messaging with RabbitMQ
 
Bpminto
BpmintoBpminto
Bpminto
 

More from Prabhat gangwar

More from Prabhat gangwar (20)

Middleware
MiddlewareMiddleware
Middleware
 
Pseudolocalization
PseudolocalizationPseudolocalization
Pseudolocalization
 
Mule anypoint studio
Mule anypoint studioMule anypoint studio
Mule anypoint studio
 
Mule anypoint platform
Mule anypoint platformMule anypoint platform
Mule anypoint platform
 
What is cluster analysis
What is cluster analysisWhat is cluster analysis
What is cluster analysis
 
clustering and load balancing
clustering and load balancingclustering and load balancing
clustering and load balancing
 
Middleware systems overview and introduction
Middleware systems overview and introductionMiddleware systems overview and introduction
Middleware systems overview and introduction
 
Restful api modeling language
Restful api modeling languageRestful api modeling language
Restful api modeling language
 
Mule esb
Mule esbMule esb
Mule esb
 
Mule fundamentals
Mule fundamentalsMule fundamentals
Mule fundamentals
 
Gsm architecture
Gsm architectureGsm architecture
Gsm architecture
 
Oracle vs-mulesoft-api-manager-features
Oracle vs-mulesoft-api-manager-featuresOracle vs-mulesoft-api-manager-features
Oracle vs-mulesoft-api-manager-features
 
Oracle real application_cluster
Oracle real application_clusterOracle real application_cluster
Oracle real application_cluster
 
Introducing adf business components
Introducing adf business componentsIntroducing adf business components
Introducing adf business components
 
File transfer methods
File transfer methodsFile transfer methods
File transfer methods
 
Ftp tftp
Ftp tftpFtp tftp
Ftp tftp
 
Bpm
BpmBpm
Bpm
 
Global warming
Global warmingGlobal warming
Global warming
 
Seo
SeoSeo
Seo
 
Vedic mathmetics
Vedic mathmeticsVedic mathmetics
Vedic mathmetics
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 

Jms

  • 1. A Study in JMS (Java Messaging Service) BY Prabhat Gangwar
  • 2. JMS OverviewJMS Overview The Java Message Service is a Java API that allows applications to create, send, receive, and read messages The JMS API minimizes the set of concepts a programmer must learn to use messaging products but provides enough features to support sophisticated messaging applications
  • 3. JMS Overview (cont)JMS Overview (cont) The JMS API enables communication that is not only loosely coupled but also: Asynchronous. A JMS provider can deliver messages to a client as they arrive; a client does not have to request messages in order to receive them. Reliable. The JMS API can ensure that a message is delivered once and only once. Lower levels of reliability are available for applications that can afford to miss messages or to receive duplicate messages.
  • 4. JMS Overview (cont)JMS Overview (cont) Messages can be consumed in either of two ways: Synchronously. A subscriber or a receiver explicitly fetches the message from the destination by calling the receive method. The receive method can block until a message arrives or can time out if a message does not arrive within a specified time limit. Asynchronously. A client can register a message listener with a consumer. A message listener is similar to an event listener. Whenever a message arrives at the destination, the JMS provider delivers the message by calling the listener's onMessage() method, which acts on the contents of the message.
  • 5. JMS And JNDIJMS And JNDI JMS does not not define a standard address syntax by which clients communicate with each other. Instead JMS utilizes Java Naming & Directory Interface(JNDI). JNDI provides a mechanism by which clients can perform a “lookup” to find the correct information to connect to each other. Using JNDI provides the following advantages: It hides provider-specific details from JMS clients. It abstracts JMS administrative information into Java objects that are easily organized and administrated from a common management console. Since there will be JNDI providers for all popular naming services, this means JMS providers can deliver one implementation of administered objects that will run everywhere. Thereby eliminating deployment and configuration issues.
  • 6. JMS Advantages over RPCJMS Advantages over RPC RPC relies on the physical connection of the client and server to the network; it is a synchronous protocol. What happens if the client is disconnected? Network could go down Client could be a laptop that is used on the road. In this case, the end user might still want to carry on working, but can't if an RPC model is being used—at least not without a great deal of work by the programmer.
  • 7. Messaging BenefitsMessaging Benefits JMS vs RPCJMS vs RPC Messaging provides the ability to send data asynchronously and in a disconnected manner. Two messaging models Point-to-point Publish-and-Subscribe In an email like model, these would be equivalent to sending and receiving e-mails directly (point-to-point), or subscribing to a list server and sending and receiving e-mails through the list server (publish-and- subscribe).
  • 8. What is the cost?What is the cost? JMS vs RPCJMS vs RPC Applications become asynchronous by nature What if we require a method to give us a return value? What if we require the data (the messages) to be delivered in a specific order? Using messaging, JMS, we have to deal with these problems ourselves. RPC handled these issues for the programmer.
  • 9. Messages ExplainedMessages Explained A message typically consists of a header and a body. The message header contains vendor-specified values, but could also contain application-specific data as well. Headers are typically name/value pairs. The body contains data; the type of the data is defined by the specification. Text A serialized Java object One of a number of other types of data.
  • 10. Publisher SamplePublisher Sample See MyTopicPublisher.java for source. 1. Perform a JNDI API lookup of the TopicConnectionFactory and topic topic = (Topic) jndiContext.lookup(topicName); 2. Create a connection and a session topicConnection = topicConnectionFactory.createTopicConnection(); topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 3. Create a TopicPublisher topicPublisher = topicSession.createPublisher(topic); 4. Create a TextMessage Message = topicSession.createTextMessage(); message.setText("This is message " + (i + 1)); 5. Publishe one or more messages to the topic topicPublisher.publish(message); 6. Close the connection, which automatically closes the session and TopicPublisher
  • 11. Subscriber SampleSubscriber Sample See MyTopicSubscriber.java for source. 1.Perform a JNDI API lookup of the TopicConnectionFactory and topic (same as publisher) 2.Create a connection and a session (same as publisher) 3.Create a TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic); 4.Create an instance of the TextListener class and registers it as the message listener for the TopicSubscriber topicListener = new TextListener(); topicSubscriber.setMessageListener(topicListener); 5.Start the connection, causing message delivery to begin topicConnection.start(); 6.Close the connection, which automatically closes the session and TopicSubscriber topicConnection.close();
  • 12. TextListener SampleTextListener Sample 1. public void onMessage(Message message) { 2. TextMessage msg = null; 3. 4. try { 5. if (message instanceof TextMessage) { 6. msg = (TextMessage) message; 7. System.out.println("Reading message: " + msg.getText()); 8. } else { 9. System.out.println("Message of wrong type: " + 10. message.getClass().getName()); 11. } 12. } catch (JMSException e) { 13. System.out.println("JMSException in onMessage(): " + e.toString()); 14. } catch (Throwable t) { 15. System.out.println("Exception in onMessage():" + t.getMessage()); 16. } 17.}
  • 13. Running The SampleRunning The Sample 1. Start the JMS provider. In this case the J2EE SDK From a command prompt run the following command: j2ee –verbose Wait until the server displays the message "J2EE server startup complete 1. Create the Administered Object. This is the object to which you will publish and subscribe. From a second command prompt run the following command j2eeadmin -addJmsDestination CS522Topic topic To verify the topic was created, view the list of Administered Objects by typing: j2eeadmin –listJmsDestination
  • 14. Running The Sample (cont)Running The Sample (cont) 3. Run the subscriber program From a command prompt run the following command within the directory that contains the MyTopicSubscriber.class: java -Djms.properties=%J2EE_HOME %configjms_client.properties MyTopicSubscriber -topic=CS522Topic 3. Run the Publisher program From a command prompt run the following command within the directory that contains the MyTopicPublisher.class: java -Djms.properties=%J2EE_HOME %configjms_client.properties MyTopicPublisher -topic=CS522Topic -count=500 -delay=500 5. You will see text output in both the Publisher and Subscriber windows

Editor's Notes

  1. Although a standard address syntax was considered, it was decided that the differences in address semantics between existing MOM products was too wide to bridge with a single syntax.