JMS - Java Messaging Service
JMS - Java Messaging Service
indigoo.com
JMS
Peter R. Egli
INDIGOO.COM
1/24
Rev. 1.90
indigoo.com
Contents
1.
2.
3.
4.
5.
6.
7.
8.
9.
What is JMS?
JMS messaging domains
JMS architecture
JMS programming model
JMS receive modes
JMS robustness features
Integrating JMS with EJB
JNDI (Java Naming and Directory Interface)
JMS 2.0
2/24
Rev. 1.90
indigoo.com
1. What is JMS?
JMS is an API for asynchronous message exchange between Java applications.
JMS implementations (instances that implement the JMS API) are called JMS provider.
Different JMS providers are not directly interoperable due to the lack of a defined wire protocol
in JMS. JMS vendors usually provide bridges to connect to other JMS providers.
JMS is standardized as JSR-914 (Java Specification Request).
JMS
spec.
<<defines>>
JMS API
<<implements>>
JMS provider
indigoo.com
Message
Message
P2P queue
Client 1
Send
Receive
Client 2
Acknowledge
4/24
Rev. 1.90
indigoo.com
Client 1
Client 2
Publish
Topic
Receive
Subscribe
Client 3
Message
5/24
Rev. 1.90
indigoo.com
4. JMS architecture
JMS is an interface specification. JMS providers implement the messaging services (basically
implement a queue or topic).
JMS makes heavy use of JNDI for lookup / discovery.
Queues, topics and connection factories are administratively created through an administrative
tool.
Admin
Tool
Bind
JNDI
Namespace
JNDI namespace:
Contains bindings for connection factories and
destinations
JMS
Provider
JMS provider:
- implements the JMS interface
- provides administrative and control interfaces
JNDI
Interfaces
JMS
Client
Connection
JMS
Interface
6/24
Rev. 1.90
indigoo.com
Connection
Creates
Creates
Message
Producer
Sends to
Creates
Message
Consumer
Session
Creates
Receives from
Message
Destination
Destination
7/24
Rev. 1.90
indigoo.com
Destination
Queue
Topic
Topic:
A topic is a PubSub (publish / subscribe) queue (mailbox).
Multiple senders may send to a topic and multiple subscribers may receive the message.
Publisher
Subscriber
Subscriber
Topic
Publisher
Subscriber
Queue:
A queue is a P2P-queue (Point To Point) where multiple senders send to the queue but only 1
receiver receives the messages. Once a message is received, it is removed from the queue.
Publisher
Subscriber
Publisher
Peter R. Egli 2015
8/24
Rev. 1.90
indigoo.com
QueueConnectionFactory
TopicConnectionFactory
Queue connection:
A queue connection is a connection from JMS a client to a JMS provider.
Connections are created through connection factories.
A connection is either a QueueConnection (P2P model) or a TopicConnection (PubSub model).
Connection
QueueConnection
TopicConnection
9/24
Rev. 1.90
indigoo.com
QueueSender
MessageConsumer
TopicPublisher
QueueReceiver
Message
MapMessage
BytesMessage
Peter R. Egli 2015
TopicSubscriber
ObjectMessage
StreamMessage
10/24
Rev. 1.90
indigoo.com
11/24
Rev. 1.90
indigoo.com
Sender
JMS Queue
Message
Consumer
Message
Asynchronous receive:
The consumer registers a message listener. Upon reception of a message, the destination
(message queue or topic) calls the message listener.
Queue
Sender
Message
Consumer
Message
12/24
Rev. 1.90
indigoo.com
N.B.: These features should not be turned on or used by default as they consume additional
processing power. The pros and cons of these features need to be carefully weighed against
each other.
13/24
Rev. 1.90
indigoo.com
14/24
Rev. 1.90
indigoo.com
15/24
Rev. 1.90
indigoo.com
Messages M3 and M4
are not received by the subscriber
M1
M2
M3
Subscription
Create
M4
M5
M1
Close
Create
Close
Create
M3
M4
M5
M6
Subscription
Close
Create
Close
Create
Close
Session
Session
M2
Subscription
Session
Create
M6
Close
Session
Create
Close
Session
Create
Close
16/24
Rev. 1.90
indigoo.com
Client 1
Send
Transaction 1
Consumes
Client 2
Transaction 2
17/24
Rev. 1.90
indigoo.com
JMS Queue
send()
send()
send()
M0
M0
M1
M1 M0
M2
M2 M1 M0
rollback()
M2 M1 M0
send()
M2 M1 M0
send()
M2 M1 M0
send()
M0
M0
M1
M1 M0
M2
M2 M1 M0
commit()
M2 M1
indigoo.com
Consumer
receive()
M2 M1 M0
receive()
M2 M1 M0
receive()
M2 M1 M0
M0
M0
M1
M1 M0
M2
M2 M1 M0
M0
M0
rollback()
M2 M1 M0
receive()
M2 M1 M0
receive()
M2 M1 M0
receive()
M2 M1 M0
commit()
M1
M1 M0
M2
M2 M1 M0
M2 M1 M0
19/24
Rev. 1.90
indigoo.com
JEE Server
Message
Message
MDB
Queue
Client 1
Send
Deliver
EJB Container
20/24
Rev. 1.90
indigoo.com
21/24
Rev. 1.90
indigoo.com
JNDI providers
Source: Oracle
22/24
Rev. 1.90
indigoo.com
23/24
Rev. 1.90
indigoo.com
9. JMS 2.0
JMS 2.0, released in April 2013, builds on the API defined by JMS 1.1, but brings a couple of
simplifications, namely:
New method to extract message body getBody() taking the expected body type as
parameter thus obviating the need to type cast the body.
Easier resource configuration, namely the possibility to use the Java EE 7 default
connection factory by applying the @Inject annotation.
This eliminates the need for proprietary JMS provider connection factory configuration.
24/24
Rev. 1.90