This document provides an overview of messaging frameworks and Java Message Service (JMS). It defines messaging as communication between disparate systems and explains why messaging is important for enabling asynchronous and reliable communication. The key concepts covered include messaging fundamentals using a message-oriented middleware, common messaging protocols like AMQP and JMS, JMS domains for point-to-point and publish/subscribe messaging, consuming messages synchronously and asynchronously, the JMS API for creating connections and messages, and best practices for building robust JMS applications. It concludes with an example of designing a drone delivery system using JMS queues.
2. 2 /
Agenda
●What is Messaging?
●Why is it important?
●Messaging Fundamentals
●Messaging Protocols
●JMS Domains
●Message consumption
●JMS API
●Building Robust JMS Applications
3. 3 /
What is Messaging
●Messaging is process of communication between two or more disparate
systems.
●Consider two legacy systems that are information silos. One system has
information about allemployees and financesin company ABC. Another
company does auditing for ABC, requires information about the employees and
financesof ABC.
●Messaging can be used for communication between these two systems.
4. 4 /
Whyis it important
●Messaging is a technology that enables high-speed, asynchronous, atomic,
program-to-program communication with reliable delivery.
●Messaging provides fault tolerance and load-balancingcapabilities.
●Messaging can be easily integrated into different heterogeneous systems.
●Messaging allows communication between two loosely coupled systems.
5. 5 /
Messaging Fundamentals
●Messaging capabilities are provided by a separate software system called
messaging system or MOM (Message-oriented middleware).
●A messaging system manages messaging the way a database system manager
data persistence.
●Similar to how a db admin must populate the db with the schema, a messaging
system admin must configure the channels that define the paths of
communication between the applications.
7. 7 /
Messaging protocols
●AMQP : Advanced message queuing protocol. It defines the protocol of
messages, so different clients like RabbitMQ, ActiveMQ (5.8 onwards),
StormMQ.
●JMS: Java Message Service is a Java API that allows applications to create, send,
receive, and read messages. Designed by Sun and several partner companies,
the JMS API defines a common set of interfaces and associated semantics.
8. 8 /
JMS Domains –Point to Point
●Point-to-Point Messaging Domain
–Eachmessage is addressed to a specific queue.
–Receiving clients extract messages from the queues.
–Eachmessage hasonlyone consumer.
–Receiver acknowledges succcessful processing of message.
9. 9 /
JMS Domains –Publish/Subscribe
●Publish/Subscribe Messaging Domain
–clients address messages to a topic, which functions somewhat like abulletin
board.
–Publishers and subscribers can dynamically publish or subscribe to the
content hierarchy.
–System takescare of distributing the messages arriving from a topic’s multiple
publishers to its multiple subscribers.
10. 10/
JMS Domains - Publish/Subscribe
●Eachmessage can have multiple consumers.
●Topics retain messages only aslong as it takes to distribute them to current
subscribers.
●A client that subscribes to a topic canconsume onlymessages published after
the client hascreated a subscription, and the subscriber must continue to be
active in order for it to consume messages.
11. 11/
Messageconsumption
●Synchronously: A subscriber or a receiver explicitlyfetches the message from
the destination by callingthe receive method. The receive method can block
until amessage arrives or can time out if a message does not arrive withina
specified time limit.
●Asynchronously: A client canregister a message listener with a consumer.
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.
12. 12/
JMS API
●JMS clients accessobjects like Connection Factory and Destinations through
interfaces that are portable, so no change isnecessary between different
implementations of JMS API.
13. 13/
JMS API
●A connection factory is the object a client uses to create a connection to a
provider.
●A destination is the object a client uses to specify the target of messages it
produces and the source of messages it consumes.
●A connection encapsulates a virtual connection with a JMS provider.
●When you have a ConnectionFactory object, you can use it to create a
Connection:
●Connection connection = connectionFactory.createConnection();
14. 14/
JMS API
●A sessionis a single-threaded context for producing and consuming messages.
You use sessions to create the following:
●Message producers
●Message consumers
●Messages
●Queue browsers
15. 15/
JMS API
●After you create a Connection object, you use it to create a Session:
●Session session = connection.createSession(false,
●Session.AUTO_ACKNOWLEDGE);
●First argument - if it is transactedsession
●Second argument – session automatically acknowledges on receiving.
16. 16/
JMS MessageProducer
●MessageProducer producer = session.createProducer(dest);
●MessageProducer producer = session.createProducer(queue);
●MessageProducer producer = session.createProducer(topic);
●After you have created a message producer, you canuse it to send messages by
using the send method:
●producer.send(message);
18. 18/
JMS MessageConsumer
●You use the receive method to consume a message synchronously. You can
use this method at any time after you call the start method:
●connection.start();
●Message m = consumer.receive();
●connection.start();
●Message m = consumer.receive(1000); // time out after a second
●To consume a message asynchronously, you use a message listener.
19. 19/
JMS MessageListeners
●You register the message listener with a specific MessageConsumer by using
the setMessageListener method. For example, if you define a classnamed
Listener that implements the MessageListener interface, you can register the
message listener asfollows:
●Listener myListener = new Listener();
●consumer.setMessageListener(myListener);
●The same listener can obtain messages from either a queue or a topic.
20. 20/
JMS Messages
●Message Bodies, type of message body formats are:
●TextMessage (string)
●MapMessage (key/value pair)
●BytesMessage
●StreamMessage
●ObjectMessage
●Message(empty body)
21. 21/
JMS Messages
●A JMS message can have three parts: a header,properties, and a body. Only
the header is required.
●JMS Headers have number of predefined fields, likeJMSMessageID, which
identifies each message uniquely.
●You can create and set properties for messages, for use in message selectors or
other messaging systems.
22. 22/
JMS MessageSelectors
●You can use aJMS API message selector, which allows a message consumer to
specify the messages that interest it.
●MessageConsumer consumer;
●consumer = session.createConsumer(destination, "myProp = 'blue'");
24. 24/
Messageacknowledgement
●In transactedsessions, acknowledgment happens automaticallywhen a
transaction iscommitted. If a transactionis rolled back, all consumed messages
are redelivered.
●In nontransacted sessions, when and how a message is acknowledged depend
on the value:
–Session.AUTO_ACKNOWLEDGE (when MessageListener returns successfully)
–Session.CLIENT_ACKNOWLEDGE (on message callacknowledge method)
–Session.DUPS_OK_ACKNOWLEDGE (lazy acknowledge)
25. 25/
Demo–Drone Delivery system
Use JMS Queue to design and implement a drone delivery system.
Objects :
●CommandCenter
●Warehouse
●Drone
●DroneEvent -This are Events sent to a JMS queue by the drones.
●Coordinates - Coordinates to send the drone
●Utility-Helper methods