Translate

Wednesday, December 11, 2013

JMS

Q1. Why do we need Message-based systems ex: JMS?
Ans. Message-based solutions are playing an increasingly important role nowadays, as more and more systems need to be integrated together and need to communicate with each other in a loosely-coupled manner.

In a message-based system, the communication among application components (producers and consumers) is mediated by a message service provider instead of direct communication b/w application components in a client-server model. As a result of this, a message service provider is sometimes referred to as a Message Oriented Middleware (MOM). The producer creates a message that contains relevant data, and sends it to a destination resource maintained by the message service provider. The message is then routed to consumer components that are registered to the specific destination.

Another distinct feature of a message-based system is that the message service provider effectively makes the communication between producers and consumers asynchronous. When a producer sends a message, consumers do not need to be available because the message service provider can persist the message until the consumer becomes available. In fact, the producer and consumer do not need to have any knowledge of each other, as long as the message is in a format that is agreed upon. This feature further decouples the producers and consumers.

Q2. What is JMS?
Ans. JMS defines a standard Java API for application components to create, send, receive, and process messages. It also provides a standard API that simplifies how application components interact with a JMS-compliant message service provider (JMS provider for short).

It defines the connection factory and destinations of the JMS provider in the form of administered objects, which can be registered in a JNDI naming service. An application component can use the standard JNDI API to perform a lookup operation to retrieve these resources.

JMS defines the two messaging approaches as follows:

  • Point-to-Point (PTP): A producer sends messages to a queue destination, which retains the messages until they are successfully routed to a consumer. Furthermore, each message is received by only one consumer. 
  • Publish/Subscribe (Pub/Sub): A producer sends messages to a topic destination. By default, the topic does not retain these messages. Instead, it immediately routes them to all the consumers registered for the topic. Due to this, if a message arrives at a topic when there is no active consumer, the message will be lost. To address this, JMS allows a consumer to be registered to the topic as durable subscriber. In this case, the JMS provider will retain the messages for this consumer until it becomes available. 
The JMS API became an integral part of the Java EE since version 1.3 as the standard to integrate with messaging systems. Subsequent Java EE revisions further enhanced the JMS API and messaging system integration in the following areas:
  • Message-Driven Beans (MDB): An MDB is a container-managed component that can asynchronously consume messages sent to a JMS destination. Due to this, we can consider an MDB as an asynchronous message listener component. The application server's MDB container provides services for MDBs such as life cycle management, instance pooling and naming services. These services eliminate a lot of boiler-plate code necessary to create a messae listener object. Due to this, when implementing an MDB, the developer only needs to focus on the logic about how to process messages delivered to the MDB. Furthermore, the MDB container's instance pooling service can significantly improve the throughput of processing messages routed to a specific MDB. 
  • Java Connector Architecture (JCA): Through JCA resource adapters, JMS providers can be easily integrated into different application servers. Once integrated, application components can treat them as standard JCA resources, and interact with the resources in a provider-independent manner. 
Setting up GlassFish (Oracle Java EE implementation) for JMS
Before we start writing code to take advantage of the JMS API, we need to set up a JMS connection factory, a message queue, and a message topic which we will do using GlassFish server.


1. Setting up a JMS connection factory
The easiest way to set up a JMS connection factory is via GlashFish's web console. Web console can be accessed by starting our domain, by entering the following command in the command line:

$> asadmin start-domain domain1
Then point the browser to http://localhost:4848




A connection factory can be added by expanding the Resources node in the tree at the left-hand side of the web console, expanding the JMS Resources node and clicking on the Connection Factories node, then clicking on the New... button in the main area of the web console.



Only thing we need to do is enter a Pool Name and pick a Resource Type for our connection factory. It is always a good idea to use a Pool Name starting with "jms/" when picking a name for JMS resources. This way JMS resources can be easily identified when browsing a JNDI tree.

In the text field labeled Pool Name, enter jms/JMSPool. Our code examples later in this chapter will use this JNDI name to obtain a reference to this connection factory.

The Resource Type drop-down menu has three options:

  • javax.jms.TopicConnectionFactory - used to create a connection factory that creates JMS topics for JMS clients using the pub/sub messaging domain 
  • javax.jms.QueueConnectionFactory - used to create a connection factory that creates JMS queues for JMS clients using the PTP messaging domain 
  • javax.jms.ConnectionFactory - used to create a connection factory that creates either JMS topics or JMS queues 
For our example, we will select javax.jms.ConnectionFactory. This way we can use the same connection factory for all our examples, those using the PTP messaging domain and those using the pub/sub messaging domain. Enter description for our connection factory if you wish and then we must click on the OK button for the changes to take effect. We should then see our newly created connection factory listed in the main area of the GlashFish web console.

2. Setting up a JMS message queue
A JMS message queue can be added by expanding the Resources node in the tree at the left-hand side of the web console, expanding the JMS Resources node and clicking on the Destination Resources node, then clicking on the New... button in the main area of the web console.





In our example, the JNDI name of the message queue is jms/Queue. The resource type for message queues must be javax.jms.Queue. Additionally, a Physical Destination Name must be entered. In this example, we use Queue1 as the value for this field. After clicking on the New... button, entering the appropriate information for our message queue, and clicking on the OK button, we should see the newly created queue.



3. Setting up a JMS message topic
Setting up a JMS message topic in GlashFish is very similar to setting up a message queue.

In the GlashFish web console, expand the Resources node in the tree at the left hand side, then expand the JMS Resouces node and click on the Destination Resources node, then click on the New... button in the main area of the web console.


Our examples will use a JNDI Name of jms/Topic. As this is a message topic, Resource Type must be javax.jms.Topic. The Description field is optional. The Physical Destination Name property is required. For our example, we will use Topic1 as the value for this property.

After clicking on the OK button, we can see our newly created message topic.



Now that we have set up a connection factory, a message queue, and a message topic, we are ready to start writing code using the JMS API.

Sample PTP Program
Prequisites:
1. Make sure that you have started the server. (asadmin start-domain domain1)
2. ConnectionFactory and Queue have been created.


http://www.freefilehosting.net/simplequeuereceiver
http://www.freefilehosting.net/simplequeuesender

Sample Pub/Sub Program
Prequisites:
1. Make sure that you have started the server. (asadmin start-domain domain1)
2. ConnectionFactory and Topic have been created.

http://www.freefilehosting.net/simpletopicpublisher
http://www.freefilehosting.net/simpletopicsubscriber
http://www.freefilehosting.net/textlistener

Alternatively Complete project is available at
http://www.freefilehosting.net/jms

No comments:

Post a Comment