Friday, October 3, 2008

Understanding JMS with a Spring Perspective

Making some notes from the official JMS tutorial.

Why Messaging?
Messaging enables distributed communication that is loosely coupled. Messaging is used for communication between software applications or software components.

What is NOT Messaging?
Messaging is different from RMI, RPC or email

What is JMS?
The Java Message Service is a Java API to help you with your messaging needs

When to use JMS/ Messaging?
Use JMS if
  • you do not want to depend on any information about other components’ interfaces( so they are easily replaceable)
  • you want your app to run whether or not all components are up and running simultaneously
  • you want an app design that allows a component to send information to another and to continue to operate without receiving an immediate response
  • you want a guaranteed delivery, not necessarily immediately

What is the support for JMS in J2EE?
  • Message driven beans support JMS
  • JMS messages can participate in distributed transactions

Where can I get an open-source implementation for JMS API?
ActiveMQ from Apache is a great open source implementation. You can see others at java-source.net


What is a Messaging Domain?
Messaging domain denotes the style of messaging. There are basically two types of messaging domains: Point-to-Point(PTP) and Publish/Subscribe(pub/sub)

How is PTP different for pub/sub?
PTP style of messaging has following features:
• Each message has only one consumer.
• A sender and a receiver of a message have no timing dependencies. The receiver can fetch the message whether or not it was running when the client sent the message.
• The receiver acknowledges the successful processing of a message.

Use PTP messaging when every message you send must be processed successfully
by one consumer.

pub/sub is characterized by:
• Each message may have multiple consumers
• Publishers and subscribers have a timing dependency. A client that subscribes to a topic can consume only messages published after the client has created a subscription, and the subscriber must continue to be active in order for it to consume messages.

Use pub/sub messaging when each message can be processed by zero, one, or many consumers.

What does JMS support out of these two?

JMS supports both. In fact it even provides something called durable subscriptions which allow you to combine flexibility and reliability of queues of PTP with the ability to allow clients to send messages to many recipients of pub/sub

Using JMS will the messages be consumed asynchronously or synchronously?
JMS supports both. However the biggest advantages of JMS arise out of asynchronous communication

What are the main parts of a JMS application?
  • Administred Objects (connection factories, destinations)
  • Connections
  • Sessions
  • Message producers
  • Message consumers
  • Messages
A connection factory is used to create a connection. A connection is used to create a session. A session can create a producer, consumer or a message. Producer send messages to a destination while consumers receive messages from the destination.

Does Spring support JMS?
Absolutely. You can read it here. If you are using ActiveMQ, you may want to see this too.

Why should I use spring to manage interactions with JMS?
For pretty much the same reason you would use spring to manage your database, cache etc. etc. : it makes your life easier. Seriously. Helps you cut down the "boiler-plate" code and gives you more specific exceptions besides the regular dependency injection benefits.

1 comment:

Krishna said...

Your article is nice and very informative. here you will find another interesting article.
Spring and JMS