01 Java Message Service
01 Java Message Service
Daniel Stollenwerk
1
Java Message Service: Topic Objectives
© SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /
Java Message Service
2
Asynchronous Messaging
Message-Oriented
Middleware
System 1 System 2
Benefits:
z Better performance: Avoid waiting for replies
z Loosely coupling of systems
z Connection of systems written in different languages
© SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /
Java Message Service
3
Java Message Service
© SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /
Java Message Service
JMS was not designed from scratch, but influenced by existing messaging
technologies
One target of JMS was to guarantee backward compatibility
Some points in the specification are left open; JMS Providers may decide on their own if and
how they fulfill some „requirements“
4
JMS Providers & Clients
© SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /
Java Message Service
Providers have to stick to Sun‘s specification, but like mentioned before there are
some points where they can decide what to do on their own
All applications using the functionalities offered by JMS providers are referred to
as Clients
5
JMS Destinations
Queue Topic
z Point-To-Point z Publish-Subscribe
z 2 kinds of subscription
Non-durable
Durable
z Durability by store-and-forward
messaging
z FIFO
© SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /
Java Message Service
6
Messaging Models
Point-to-Point / Publish-Subcribe
Queue
Sender Queue
3 2 Receiver
5 4 1
Queue
Topic 1a Topic
Publisher Subscriber 1
2
3 Topic 1b
Topic
JMS Provider Subscriber 2
1 2 3
1. Messages to be sent
2. Messages waiting in the Queue
3. Messages being consumed
© SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /
Java Message Service
Topic Messages:
Messages 1a and 1b are copies of the original message 1. These copies are sent to all
applications which are subscribed for the topic.
Message 2 will be sent to the according TopicSubscribers next
Message 3 will be sent to the Topic next.
7
Point-to-point Æ QueueBrowser
© SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /
Java Message Service
A usual QueueReceiver either consumes a message or cannot read it. If you only
want to look at a message but still let a receiver get it, you will have to use a
QueueBrowser.
8
Message Format
© SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /
Java Message Service
Some of the header fields may be disabled to gain a faster processing of the
messages. For example there is a unique ID generated for every message by
default. Switching this off may save some time if an application produces many
messages.
The type Message is useful if you only want to send a little information in form of
key-value-pairs because it has no body.
9
Sending a message
Source code / 1
QueueSession queueSession =
queueConnection.createQueueSession(true, 0); Queue
//true -> session is transacted; 0 -> acknowledgement Session
not specified
Queue
QueueSender queueSender = Sender
queueSession.createSender(queue);
© SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /
Java Message Service
10
Sending a message
Source code / 2
1
Queue
Sender
2
Queue
© SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /
Java Message Service
In the first step the message is created; in the second one it is being sent to the
Queue
Between the first and the second step properties for the Message can be set
There is a circle connection between the Queue and the QueueSender; this is
because the Queue object is used for creating the sender and due to this the
messages are sent to the according Queue.
11
Sending a message
Logical names and Mapping
QueueConnectionFactory queueConnectionFactory =
Source Code (QueueConnectionFactory)
initialContext.lookup("java:comp/env/jms/QConnFactory");
<resource-ref>
Standard
<res-ref-name>jms/QConnFactory</res-ref-name>
deployment ...
descriptor </resource-ref>
Vendor <resource-ref>
<res-ref-name>jms/QConnFactory</res-ref-name>
deployment <res-link>QueueConnectionFactory</res-link>
descriptor </resource-ref>
© SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /
Java Message Service
In the source code you use logical names for JMS objects. These logical names
have to be mapped to real objects.
You have to declare a resource-ref for the ConnectionFactory and a resource-env-
ref for the Destination.
Æ Both have to be declared in the standard deployment descriptor (ejb-jar.xml)
and mapped in the vendor specific one (ejb-j2ee-engine.xml).
12
Transactions
Sending Receiving
App App
4 3
6 5 2 1
Transaction A1 Transaction B1
JMS Provider
Session A Session B
© SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /
Java Message Service
13
Acknowledgement
© SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /
Java Message Service
14
Java Message Service: Topic Summary
© SAP AG 2004 / Daniel Stollenwerk / Java Message Service & Message Driven Beans /
Java Message Service
15