Thursday, October 05, 2006

Java Messaging Service - An Overview

Required components of a JMS Application:
1. Administered Object
2. Connection
3. Session

Administered Object:
Created by administrator of the JMS provider application.
Placed in a Java Naming and Directory Interface namespace-Administered from either a command line program, GUI or an HTML based management console.

Two types of administered object: Destination and Connection Factory.

Destination:
Contains configuration information supplied by the JMS provider.
Client uses this object to specify a destination for messages that it wishes to send and /or a location from which to receive messages.


Two types of Destination:
1. Queue – PTP model
2. Topic – Publish / Subscribe model

Connection Factory:
Obtained thro JNDI lookup.
Contains connection configuration information or handle containing the IP address, enabling a JMS client application to create a connection with the JMS Server.

Connection:
Provides the physical connection to the JMS Server

Session:
Responsible for sending and receiving messages, managing transactions and handling acknowledgements.

Objects used to create and receive messages in a JMS Client Application:
Four objects are used to create and receive messages in a JMS client application:
1. MessageProducer.
2. MessageConsumer.
3. MessageListener.
4. MessageSelector.

MessageProducer:
• Created by session
• Used to send messages to a destination.
• Can specify the default delivery mode using setDeliveryMode [NON_PERSISTENT – lower overhead-message not logged/ PERSISTENT – logged-typically to a database].
• Can specify the priority of the Message using setPriority.[0 to 9 - 4 is default – 0 to 4 normal – 5-9 higher]


MessageConsumer:
• Created using session
• Used to receive message sent to the destination.
• Message received in 2 ways – Asynchronously or Synchronously.
• Synchronously – where the client calls one of the receive methods (receive and receiveNoWait) after the consumer is started.
• Asynchronously – where the client registers a MessageListener and then starts the consumer.

NOTE:
All messages in JMS are exchanged asynchronously between the clients, in that the producer does not receive acknowledgement from the consumer that it has processed the message. As soon as the message is sent or published, the producer is not blocked from sending or publishing another message immediately.


MessageListener:
Interface that needs to be implemented to process messages in an asynchronous fashion.
Must do the following:
• Create an object that implements the MessageListener interface. This includes coding the onMessage () method.
• Register the object with the session via the setMessageListener () method.
• Call the start () method on the Connection object to begin receiving messages.


MessageSelector:
• Java.lang.String object specified by client by createSubscriber () method.
• Filters the message based on the header and properties field using the expression contained in the String.
• The syntax of this expression is based on a subset of SQL92 conditional expression syntax.


NOTE:
A Message digest is a digital fingerprint value that is computed from a message, file or byte stream.

How the Point-to-Point works:
• Sends messages to a receiver on a one-to-one basis.
• Examples:
Instant Messaging.
Receiving transaction from other systems.
Sending an order to another System.
Supply Chain Processing.
• Message delivered to destinations named ‘queue’.
• Messages are processed in FIFO.
• Receiver is able to browse thro the messages but can able to process the messages in FIFO.
• Steps to be followed :
1. Obtain a QueueConnectionFactory using JNDI lookup.
2. Obtain a QueueConnection to the provider via the QueueConnectionFactory.
3. Obtain a QueueSession with the provider via the QueueConnection.
4. Obtain the queue via the JNDI lookup
5. Create either a QueueSender or a QueueReceiver via the QueueSession for the required queue.
6. Send and /or receive messages.
7. Close the QueueConnection – This will also close the QueueSender or QueueReceiver and the QueueSession.


How the publish / Subscribe works:
• Allows an application to publish message on a one-to-many or a many-to-many basis.
• Examples:
Sending sales forecast to various people in an organization.
Sending new items to interested parties.
Sending Stock prices to traders on the trading floor.
• Messages are published to a topic.
• One or more publisher can publish messages.
• Interested Clients must subscribe to the topic.
• Multiple clients can subscribe to the same topic.
• Non-Durable Subscription model: Subscriber must be connected at the time the message was published. If no subscribers are online, the message will be published and destroyed soon thereafter.
• Durable Subscription Model: Message will be received when the subscriber is reconnected to the topic. Greater overhead – Requires additional resources to persist the messages until they can be delivered to all the known durable subscribers.
• Steps to be followed:
1. Obtain the TopicConnectionFactory object [Using JNDI lookup].
2. Obtain the TopicConnection to the provider [Using TopicConnectionFactory].
3. Obtain a TopicSession with the provider [Using TopicConnection].
4. Obtain the topic [Using JNDI look up].
5. Create either a TopicPublisher or a TopicSubscriber [Using TopicSession].
6. Publish and/or receive messages.
7. Close the TopicPublisher or TopicSubscriber, the session and the connection.

0 Comments:

Post a Comment

<< Home