Generic JMS usage
Putting a message onto a message queue using JMS requires a number of components. The figure below depicts how the PingServletToMDBQueue servlet places a message on a queue.
For this example the following are needed:
- JNDI Name service
- Queue Connection Factory (QCF)
Encapsulates the settings necessary to connect to a queue-based messaging system.
- Queue destination
A reference to the point-to-point messaging queue.
- Queue manager
A queue manager is a WebSphere MQ term. It refers to the component that is responsible for looking after a number of queues within one install of WebSphere MQ server.
- A message queue
The actual queue where messages are held until removed by an application.
Component interactions to send a message using JMS
- The servlet is invoked by the user and the doGet method is called by the Web container.
- The application needs to locate the correct queue to place the message on. The JNDI namespace is used to house a link to the Java objects that will perform communication with the messaging system. The PingServletToMDBQueue servlet uses resource references within the code. These are linked at application deployment time to the JNDI entries that can be used to communicate with the messaging system.
- The code performs a JNDI lookup for the queue connection factory using the resource reference.
- The resource reference is matched to the JNDI entry that contains the queue connection factory. The queue connection factory (QCF) object is returned, it will be used to get a connection to the queue manager that is responsible for the target queue.
- A second JNDI lookup is performed and resolved to the correct JNDI entry . This time it is for the queue destination, this will be used to locate the required queue from the queue manager.
- To be able to communicate with the queue in the messaging system, the application must first create a QueueConnection from the QCF object. WAS maintains a pool of QueueConnection objects per QCF for use by any application in the appserver. This request to create a new QueueConnection will obtain an existing QueueConnection from this pool if there are any available, otherwise one will be created and added to the pool.
- When using the Embedded JMS server or WebSphere MQ as the underlying messaging system, the creation of the QueueConnection will attempt a physical connection with the queue manager to verify that it is available.
- The next step is to create a QueueSession object using the QueueConnection method - createQueueSession. This is the stage where a physical connection will be established to the queue manager which will eventually be used to transmit the message to the queue. WAS maintains a pool of QueueSession objects for each established QueueConnection on the QCF. This request to create a new QueueSession will obtain an existing QueueSession from this pool if there are any available, otherwise one will be created and added to the pool.
- With a connection now established to the messaging system through the QueueSession object, the application now specifies what sort of action is going to be performed, in this case it is to send a message. The queue definition that was taken from the JNDI name service is passed to the QueueSession object and this tells the queue manager the specific queue on to which the message will be placed.
- The message is constructed and sent to the message queue.
WebSphere is a trademark of the IBM Corporation in the United States, other countries, or both.
IBM is a trademark of the IBM Corporation in the United States, other countries, or both.