Basic Concepts of Messaging System

JMS (Java Messaging System) was developed to cater the need of flexible integration design among various application or within application components.

In simple Java application, if we need to integrate one component/application with other, we have to have the reference of target with source. It means that one object must be aware about other object. It results into a tight coupling. It can be made flexible further by using different patterns like Factory, Service Locators and Directories but still objects used to be aware about each other type with a defined integration protocol like method signature. Now when architects want more flexibility, where one object is completely unaware about existence of other object and can simply say that I need this type of message. This is an ultimate flexibility in integration.

This type of flexible integration can be achieved using a messaging based simple design in java.  If we want to achieve this in Simple Java implementation, we can make a Message broker kind of component. This component can provide following facilities: 
  1. Any object can register with it as message producer by specifying a specific type of message, which can be a plain string. Message broker will maintain an internal data structure to keep track of such message producers corresponding to their message types.  
  2. Any object can register with message broker as message consumer. It will also specify a type of message for which it is interested to consume the message. Message broker will maintain an internal data structure to keep track of all message consumers corresponding to message types for which these wants the messages. 
  3. Now whenever any message producer produce a message, it calls message broker API and hand over the new message to it. Message broker knows the type of message, so it searches all the message consumers registered for this message type, iterate over this list and hand over the message to each consumer. 

Now see how beautifully this design has facilitated more than one components to integrate with each other and that also in quite flexible manner i.e. no one is aware about other, but just everyone is known to message broker only and the type of message. The use of this design is, that any party can be replaced with other object any time without changing the semantic of system. Now suppose there is a car production unit, which on consuming a limit of inventory send a message to inventory system. Both of these systems are connected through this message broker loosely. Now if we want to replace the inventory system anytime with any new system, it can be replaced easily as both parts are integrated by just message type. So the new system again start consuming the same message type produced by car production unit.

Message itself can be made very dynamic using XML, or HashMap based implementation. 

So this is the concept of messaging. Above example is for publisher and subscriber topology. Now there is another type of messaging system where message produced by any producer is consumed by any one consumer only. It is called point to point messaging system. You can visualize easily that how this system would work. In the above mentioned design, the only difference would be that message will be delivered to first consumer who will request for it. 

Now above design can be extended further for many advance features. Like if we want to make sure that message once produced should not be lost even if system goes down or even if no consumer is active till now. To enable this, we can start persisting the messages with some age limits. Similarly, an acknowledgement facility can be easily introduced, where message broker will inform the producer whenever the message is consumed. Other feature is to enable synchronous or asynchronous message processing depending upon some configuration or type of consumer. If type supports asynchronous processing, message can be handed over to the consumer in a new thread. Further, a filter criteria can be supported for consumer. Consumer can mention a filter criteria while registering itself with broker. Broker can have implementation to only pass those messages to consumer which are of specified type and also qualify the filter criteria. This criteria can be made very extensive.

As you have all the data with message broker and hence it can be used easily to provide any such advance features. 

It is all about very basic concepts of a messaging system. Of course, any level of advance features can be introduced with these basics. JMS is an implementation of these concepts with many advanced features added to it, like it can help two remote applications to integrate with each other using messaging and much more.  

In next article, we shall discuss more about JMS and its working. Till then, any comment / question is welcome. Try to make a simple message broker with Core Java only.

People who read this post also read :



0 comments:

Post a Comment