Java Message Service (JMS) - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

Java Message Service (JMS)

Description:

Abstraction from vendor specific APIs ... Two Delivery Modes. PERSISTENT delivery mode ... A durable subscription can have only one active. subscriber at a time ... – PowerPoint PPT presentation

Number of Views:300
Avg rating:3.0/5.0
Slides: 48
Provided by: Tolg1
Category:

less

Transcript and Presenter's Notes

Title: Java Message Service (JMS)


1
Java Message Service (JMS)
  • Karteek Suvarna
  • Hardik Vaishnav
  • Nagaraju Medavarapu

2
Outline
  • Basic Concepts
  • API Programming Model
  • Reliability Mechanisms
  • Transactions in JMS
  • Step by Step Sender/Receiver Application
  • JMS Providers

3
What is JMS?
  • A specification that describes a common way for
    Java programs to create, send, receive and read
    distributed enterprise messages
  • loosely coupled communication
  • Asynchronous messaging
  • Reliable delivery
  • Integration with J2EE
  • Synchronous message send and receive
  • Latest spec version is 1.1
  • Part of J2EE 1.4

4
Why use JMS?
  • JMS vs. Vendor Specific APIs
  • Abstraction from vendor specific APIs
  • Message Oriented Middleware (MOM) vendors
    offer similar functionality and APIs
  • Easy to upgrade components
  • JMS vs. RPC
  • Decoupled components
  • Easy to replace components of the system
  • Application does not require all components
    to be up and running
  • Asynchronous messaging scales better
    when multiple
  • systems need to be integrated

5
RPC vs. JMS
6
JMS Messaging Domains
  • Point-to-Point (PTP)
  • built around the concept of message queues
  • each message has only one consumer
  • Publish-Subscribe systems
  • uses a topic to send and receive messages
  • each message has multiple consumers

7
Point-to-Point Messaging
Msg
Msg
consumes
Client1
Client2
Queue
sends
acknowledges
8
Publish/Subscribe Messaging
subscribes
Client2
Topic
Msg
Client1
delivers
publishes
subscribes
Client3
delivers
9
Message Consumptions
  • 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.
  • Whenever a message arrives at the destination,
    the JMS provider delivers the message by calling
    the listener's onMessage() method.

10
A JMS Application
  • JMS Clients
  • Java programs that send/receive messages
  • Messages
  • Administered Objects
  • preconfigured JMS objects created by an admin for
    the use of clients
  • ConnectionFactory, Destination (queue or topic)
  • JMS Provider
  • messaging system that implements JMS and
    administrative functionality

11
JMS API Programming Model
  • Administered Objects
  • Connections
  • Sessions
  • Message Producers
  • Message Consumers
  • Messages

12
Administered Objects
  • Configured by the administrator
  • Usually looked up through JNDI
  • Connection Factories
  • Used by the client to create a connection
  • Has preconfigured parameters
  • QueueConnectionFactory or TopicConnectionFactor
    y
  • Destination Factories
  • Where messages are put /read from
  • Point To Point Queues
  • Publish/Subscribe Topics

13
JMS Administration
Administrative Tool
Bind
JNDI Namespace
Lookup
JMS Provider
JMS Client
Logical Connection
14
JMS API Programming Model
Connection Factory
creates
Connection
Message Consumer
Message Producer
creates
creates
Session
receives from
sends to
creates
Destination
Destination
Msg
15
Connections
  • A virtual connection with a JMS provider
  • Queue or Topic connections are created using the
  • corresponding connection factory
  • start needs to be called before messages can be
  • consumed
  • close needs to be called to release resources
  • stop can be used to temporarily stop message
  • delivery without closing the connection

16
Sessions
  • Single threaded context for consuming/producing
  • messages
  • Sessions are used to create message consumers,
  • message providers, and messages
  • Sessions provide a context to group as one
  • atomic operation a number of sends and receives
  • QueueSession and TopicSession
  • Can be transactional (local)
  • Can provide acknowledgment

17
Message Producers
  • Send messages to a destination
  • Unidentified producers can be used when
  • we want to specify the destination at send time
  • QueueSender interface
  • send
  • TopicPublisher interface
  • publish

18
Message Consumers
  • Receives messages sent to a destination
  • Message Receivers
  • QueueReceiver and TopicSubscriber
  • start and close
  • Used synchronously by calling the receive
    method
  • Message Listeners
  • QueueListener and TopicListener
  • Used asynchronously (onMessage)
  • Message Selectors
  • Filter messages using an SQL92 conditional
    expression
  • Only headers and properties can be filtered
    (not content)

19
JMS Messages
  • Message Header
  • used for identifying and routing messages
  • Unique ID, timestamp, destination, priority,
  • Message Properties (optional)
  • contains vendor-specified values, but could also
    contain application-specific data
  • Message Body(optional)
  • contains the data
  • five different message body types in the JMS
    specification

20
JMS Message Types
21
Basic Reliability Mechanisms
  • Controlling message acknowledgment
  • Specifying message persistence
  • Setting message priority levels
  • Allowing messages to expire
  • Creating temporary destinations
  • Advanced JMS Reliability Mechanisms
  • Creating durable subscriptions
  • Using local transactions

22
Acknowledgment Types
  • Auto acknowledgment (AUTO_ACKNOWLEDGE)
  • Message is considered acknowledged when
    successful
  • return on MessageConsumer.receive() or
  • MessageListener.onMessage()
  • Client acknowledgment (CLIENT_ACKKNOWLEDGE)
  • Client must call acknowledge() method of
    Message object
  • Lazy acknowledgment (DUPS_OK_ACKNOWLEDGE)
  • Messaging system acknowledges messages as
    soon as they are available for consumers

23
Two Delivery Modes
  • PERSISTENT delivery mode
  • Default
  • Instructs the JMS provider to take extra
  • care to ensure that a message is not lost
  • in transit in case of a JMS provider failure
  • NON_PERSISTENT delivery model
  • Does not require the JMS provider to store
  • the message
  • Better performance

24
How to Specify Delivery Mode
  • SetDeliveryMode method of the MessageProducer
    interface
  • producer.setDeliveryMode
  • (DeliveryMode.NON_PERSISTENT)
  • Use the long form of the send or the publish
  • method
  • producer.send(message,
  • DeliveryMode.NON_PERSISTENT, 3,10000)

25
How to Set Delivery Priority
  • Ten levels of priority range
  • from 0 (lowest) to 9 (highest)
  • Default is 4
  • Use the setPriority method of the
    MessageProducer interface
  • producer.setPriority(7)
  • Use the long form of the send or the publish
    method
  • producer.send(message,
  • DeliveryMode.NON_PERSISTENT, 7, 10000)

26
How Durable Subscription Works
  • A durable subscription can have only one active
  • subscriber at a time
  • A durable subscriber registers a durable
    subscription by specifying a unique identity that
    is retained by the JMS provider
  • Subsequent subscriber objects that have the same
    identity resume the subscription in the state in
    which it was left by the preceding subscriber
  • If a durable subscription has no active
    subscriber, the JMS provider retains the
    subscription's messages until they are received
    by the subscription or until they expire

27
More JMS Features
  • Request/Reply
  • by creating temporary queues and topics
  • Session.createTemporaryQueue()
  • producersession.createProducer(msg.getJMSReplyTo(
    ))
  • reply session.createTextMessage(reply)
  • reply.setJMSCorrelationID(msg.getJMSMessageID)
  • producer.send(reply)
  • Message selectors
  • SQL-like syntax for accessing header
  • subscriber session.createSubscriber(topic,
    priority gt 6 AND type alert )
  • Point to point selector determines single
    recipient
  • Pub-sub acts as filter

28
Transactions in JMS
  • Transaction scope is only between client
  • and Messaging system not between clients
  • a group of messages are dispatched as a unit
  • (on the sender side)
  • a group of messages are retrieved as a unit (on
  • the receiver side)
  • Local and Distributed transactions

29
Local Transactions in JMS
  • Local transactions are controlled by Session
  • object
  • Transaction begins when a session is created
  • There is no explicit begin transaction
    method
  • Transaction ends when Session.commit() or
  • Session.abort() is called
  • Transactional session is created by specifying
  • appropriate flag when a session is created
  • QueueConnection.createQueueSession(true, ..)
  • TopicConnection.createTopicSession(true, ..)

30
Distributed Transactions in JMS
  • Coordinated by a transactional manager
  • Applications will control the transaction via
  • JTA methods
  • Use of Session.commit() and Session.rollback()
    is
  • forbidden
  • Messaging operations can be combined with
    database transactions in a single transaction

31
JMS API in a J2EE Application
  • Since the J2EE1.3 , the JMS API has been an
    integral part of the platform
  • J2EE components can use the JMS API to send
    messages that can be consumed asynchronously by a
    specialized Enterprise Java Bean
  • message-driven bean

32
MessageDriven Bean
  • acts as a listener for the JMS, processing
    messages asynchronously
  • specialized adaptation of the JMS API used in the
    context of J2EE applications

33
JMS with EJB Example
34
Steps for Building a JMS SenderApplication
  • 1.Get ConnectionFactory and Destination object
  • through JNDI
  • 2.Create a Connection
  • 3.Create a Session to send/receive messages
  • 4.Create a MessageProducer
  • 5.Start Connection
  • 6.Send (publish) messages
  • 7.Close Session and Connection

35
(1) Locate ConnectionFactory andDestination
objects via JNDI
  • // Get JNDI InitialContext object
  • Context jndiContext new InitialContext()
  • // Locate ConnectionFactory object via JNDI
  • TopicConnectionFactory factory
  • (TopicConnectionFactory) jndiContext.lookup(
  • "MyTopicConnectionFactory")
  • // Locate Destination object (Topic or Queue)
    through //JNDI
  • Topic weatherTopic
  • (Topic) jndiContext.lookup("WeatherData")

36
(2) Create Connection Object
  • // Create a Connection object from
  • // ConnectionFactory object
  • TopicConnection topicConnection
  • factory.createTopicConnection()

37
3) Create a Session
  • // Create a Session from Connection object.
  • // 1st parameter controls transaction
  • // 2nd parameter specifies acknowledgment type
  • TopicSession session
  • topicConnection.createTopicSession (false,
  • Session.CLIENT_ACKNOWLEDGE)

38
4) Create Message Producer
  • // Create MessageProducer from Session object
  • // TopicPublisher for Pub/Sub
  • // QueueSender for Point-to-Point
  • TopicPublisher publisher
  • session.createPublisher(weatherTopic)

39
(5) Start Connection
  • // Until Connection gets started, message flow
  • // is inhibited Connection must be started
  • // before messages will be transmitted.
  • topicConnection.start()

40
(6) Publish a Message
  • // Create a Message
  • TextMessage message session.createMessage()
  • message.setText(My First JMS Application")
  • // Publish the message
  • publisher.publish(message)

41
Steps for Building a JMS ReceiverApplication
(Asynchronous)
  • 1.Get ConnectionFactory and Destination object
  • (Topic or Queue) through JNDI
  • 2.Create a Connection
  • 3.Create a Session to send/receive messages
  • 4.Create a MessageConsumer (TopicSubscriber or
  • QueueReceiver)
  • 5.Register MessageListener for non-blocking mode
  • 6.Start Connection
  • 7.Close Session and Connection

42
4) Create Message Subscriber
  • // Create Subscriber from Session object
  • TopicSubscriber subscriber
  • session.createSubscriber(weatherTopic)

43
5) Register MessageListenerobject for
Asynchronous mode
  • // Create MessageListener object
  • WeatherListener myListener
  • new WeatherListener()
  • // Register MessageListener with
  • // TopicSubscriber object
  • subscriber.setMessageListener(myListener)

44
Steps for Building a JMS ReceiverApplication
(Synchronous)
  • 1.Get ConnectionFactory and Destination
  • object (Topic or Queue) through JNDI
  • 2.Create a Connection
  • 3.Create a Session to send/receive messages
  • 4.Create a MessageConsumer
  • 5.Start Connection
  • 6.Receive message
  • 7.Close Session and Connection

45
JMS Providers
  • SunONE Message Queue (SUN)
  • a JMS provider integrated with the SunONE
    Application Server
  • http//www.sun.com
  • MQ JMS (IBM)
  • MQSeries is another messaging technology
  • can configure MQ as a JMS provider
  • (http//www7b.software.ibm.com/wsdd/library/techti
    p/0112_cox.html)

46
JMS Providers
  • WebLogic JMS (BEA)
  • enterprise-class messaging system integrated into
    WebLogic Server
  • http//dev2dev.bea.com/technologies/jms/index.jsp
  • JMSCourier (Codemesh)
  • merging C applications into a JMS environment
  • http//www.codemesh.com/en/AlignTechnologyCaseStud
    y.html

47
More JMS Vendors
  • Fiorano Software http//www.fiorano.com
  • JRUN Server http//www.allaire.com
  • GemStone http//www.gemstone.com
  • Nirvana http//www.pcbsys.com
  • Oracle http//www.oracle.com
  • A more exhaustive listing is available at
  • http//java.sun.com/products/jms/vendors.html
Write a Comment
User Comments (0)
About PowerShow.com