30
hSenid Lanka: Java Message Service By Kasun Dinesh Madusanke JMS (Java Message Service)

JMS-Java Message Service

Embed Size (px)

Citation preview

Page 1: JMS-Java Message Service

hSenid Lanka: Java Message Service

By Kasun Dinesh Madusanke

JMS(Java Message

Service)

Page 2: JMS-Java Message Service

2

hSenid Lanka: JMS

Topicso Message and Messagingo What is JMS?o Goals of JMSo Benefits of JMSo When to use JMS?o JMS Message

Page 3: JMS-Java Message Service

3

Topics Cont.o Java Mail vs JMSo JMS Componentso JMS Applicationo MOM Service Providerso Messaging Modelso JMS API Programming

ModelhSenid Lanka: JMS

Page 4: JMS-Java Message Service

4

Topics Cont.o Demo-JMS Pure Javao Spring JMSo JMSTemplateo Demo-Spring JMSo Summary

hSenid Lanka: JMS

Page 5: JMS-Java Message Service

5

Message & Messaging

▸ Bytes of data that is meaningful between the applications which use it.

▸ Used to transfer information form one application to others.

hSenid Lanka: JMS

Message

▸ Communication between system components or different applications.

Messaging

Page 6: JMS-Java Message Service

6

What is JMS?▸ JMS is a Message Oriented Middleware (MOM)▸ Published and maintained by Sun Microsystems▸ First published in August 1998▸ Loosely coupled communication▸ Asynchronous messaging▸ Reliable delivery

hSenid Lanka: JMS

Page 7: JMS-Java Message Service

7

What is JMS? Cont.

hSenid Lanka: JMS

Page 8: JMS-Java Message Service

8

Goals of JMS▸ Minimizes the set of concepts a programmer

must learn to use messaging products (programmer friendly).

▸ Provides enough features to support sophisticated messaging applications.

▸ Maximize the portability of JMS applications across JMS providers in the same messaging domain.hSenid Lanka: JMS

Page 9: JMS-Java Message Service

9

When to use JMS?▸ The provider wants the components not to

depend on information about other components' interfaces, so that components can be easily replaced.

▸ The provider wants the application to run whether or not all components are up and running simultaneously.

▸ The application business model allows a component to send information to another and to continue to operate without receiving an immediate response.

hSenid Lanka: JMS

Page 10: JMS-Java Message Service

10

JMS Message

hSenid Lanka: JMS

Header

Properties

Body

This image shows theStructure of a JMS Message

Page 11: JMS-Java Message Service

11

hSenid Lanka: JMS

Header Properties Body

Identify message Added by the application developer Message body

Destination Application specific propertiesCan contain arbitrary data typesEg:Text messagesMap (key-value pairs)XMLSerialized objects (Java)Binary dataEmpty

Routing Information Key-value pairs

Priority Extensions for messaging systems

Timestamp

Message type

Header, Properties and Body

Page 12: JMS-Java Message Service

12

Java Mail vs JMS

hSenid Lanka: JMS

Java Mail JMSAPI for sending emails (eg: with attachments).

Capable of exchanging messages between applications.

Mainly for human information exchange

Application/Human to Application/Human Messaging.

Delivery can be duplicated. More like a database. Messages removed once it reads by the recipient system.

Delivery not guaranteed. Delivery is guaranteed (Above reason).Designed for connectivity on the web.

Bunch of different qualities of service (durable vs non-durable, queue vs topic).

Page 13: JMS-Java Message Service

13

JMS Components▸ JMS Provider:

▹ The messaging system (MOM) that implements JMS.▸ JMS Clients

▹ Java applications that produce/receive messages.▸ JMS Producer/Publisher

▹ A JMS client that creates and sends messages.▸ JMS Consumer/Subscriber

▹ A JMS client that receives messages.▸ JMS Application

▹ The system composed of JMS clients and a JMS provider.

hSenid Lanka: JMS

Page 14: JMS-Java Message Service

14

JMS Application

hSenid Lanka: JMS

Page 15: JMS-Java Message Service

15

MOM Service Providers

hSenid Lanka: JMS

Service provider Product CompanyWeblogic OracleMQSeries IBMJBOSSMQ JBOSSSoniqMQ ProgressTIBCO EMS TIBCOActiveMQ Apache

Page 16: JMS-Java Message Service

16

Messaging Models

hSenid Lanka: JMS

Page 17: JMS-Java Message Service

17

Messaging Models Cont.▸ Point to Point

▹ Message queues, senders and receivers.▹ Message is sent to a queue.▹ Each message has only one consumer.▹ Queue may be configured to persist messages.

▸ Publish-Subscribe▹ Publishers, subscribers, topics.▹ Message may have multiple consumers, or no

consumer at all.▹ Each message is delivered to every client subscribed

to a topic.hSenid Lanka: JMS

Page 18: JMS-Java Message Service

18

Messaging Models Cont.

hSenid Lanka: JMS

▸ Point to Point

Page 19: JMS-Java Message Service

19

Messaging Models Cont.

hSenid Lanka: JMS

▸ Publish-Subscribe

Page 20: JMS-Java Message Service

20

Messaging Models Cont.

hSenid Lanka: JMS

PTP vs PUB/SUBPoint to Point Publish-Subscribe

Each message has only one consumer. Each message can have multiple consumers.

Messages first sent to a destination called Queue. Messages first sent to a destination called Topic.

A sender and receiver of a message have no timing dependencies (receiver can fetch the message later).

Publishers and subscribers have a timing dependency (client need to be subscribed and active).

The receiver acknowledges the successful processing of a message. Does not provide acknowledgement.

Page 21: JMS-Java Message Service

21

JMS API Programming Model

hSenid Lanka: JMS

Page 22: JMS-Java Message Service

22

JMS API Programming Model Cont.

hSenid Lanka: JMS

▸ Connection Factory▹ The client uses an object which is a connection

factory used to create a connection to a provider.▹ It creates connection between JMS Provider and JMS

Client.▸ Connection

▹ A JMS connection encapsulates a virtual connection with a JMS provider.

▹ it is an open TCP/IP socket between a client and a provider service daemon.

Page 23: JMS-Java Message Service

23

JMS API Programming Model Cont.

hSenid Lanka: JMS

▸ Session▹ The session is a single threaded context which is used

for producing and consuming messages.▹ The sessions are used to create the following:

▹ Message Producers▹ Message Consumers

Page 24: JMS-Java Message Service

24

JMS API Programming Model Cont.

hSenid Lanka: JMS

▸ Message Producer▹ A JMS message producer object is created by a

session.▹ It is used for sending messages to a destination.

▸ Message Consumer▹ A JMS message consumer object is created by session. ▹ It helps for receiving messages sent to a destination.

Page 25: JMS-Java Message Service

25

DEMO(JMS Pure

Java)

hSenid Lanka: JMS

Download project files

Page 26: JMS-Java Message Service

26

Spring JMS

hSenid Lanka: JMS

▸ Advantages

▹ No EJB container required (no JEE container)

▹ Simplified resource management

▹ Connection Factory, Connections, Destinations

▹ Simplified concurrency management

▹ Simplified transaction management

Page 27: JMS-Java Message Service

27

Spring JMS Cont.

hSenid Lanka: JMS

▸ JMS Template

▹ Send and receive messages synchronously

▸ Message Listener Container

▹ Receive messages asynchronously

▹ Message-Driven POJOs (MDPs)

Page 28: JMS-Java Message Service

28

JMSTemplate

hSenid Lanka: JMS

Method Descriptionbrowse() Browse messages in a queueconvertAndSend() Send messages synchronously

Convert a Java object to a JMS messageexecute() Provides access to callbacks for more complex

scenariosreceive()receiveAndConvert() Receive messages synchronouslyreceiveSelected() receiveSelectedAndConvert() Receive filtered messages synchronouslysend() Send a message synchronously using a MessageCreator

Page 29: JMS-Java Message Service

29

hSenid Lanka: JMS

DEMO(Spring JMS)

Download project files

Page 30: JMS-Java Message Service

30hSenid Lanka: Java Message Service

By Kasun Dinesh Madusanke

THANKYOU!