In-depth introduction to JMS (II)-Composition of JMS, in-depth introduction to composition of jms

Source: Internet
Author: User
Tags ibm mq

In-depth introduction to JMS (II)-Composition of JMS, in-depth introduction to composition of jms
JMS Provider

The message system that implements the JMS specification also provides all-round functions required for management and control, as shown in ActiveMQ.

Administered Objects

Administered Objects is a pre-configured JMS object created by the system administrator for clients using JMS, such as ConnectionFactory and Destination. The objects managed by JMS are objects that contain the JMS configuration information. These objects are created by the JMS administrator and used by the JMS client.

Many JMS messaging systems have different underlying implementation technologies, such as Sun MQ, ibm mq, bea mq, and Apache ActiveMQ, so how can the JMS client program to isolate the changes of these products and have cross-platform features? It is implemented by defining managed objects. The managed objects are created and customized by the Administrator using the management tools provided by the JMS system and then used by the JMS client. The JMS client uses interfaces to call these managed objects, so as to have cross-platform features.

There are two main Managed Objects: ConnectionFactory and Destination.


This is the object used by the client to create a connection with the JMS service provider.


This object is used by the client to specify the destination of the message to be sent and the source of the message received by the client. The managed object is generally put in the JNDI namespace by the Administrator. the JMS managed object is usually described in the document of the JMS client application, and what kind of JNDI name should be used to provide these JMS managed objects.

JMS Client

These client programs use APIs provided by JMS to create Java language programs for sending and receiving messages, that is, message producers and consumers.

Message producer

The client uses MessageProducer to send messages to the destination. Use the Queue or Topic object as the parameter to call the createProducer method of the session object to create MessageProducer.

The client can also create producers without destinations. In this case, the destination object must be passed to each sending operation. A typical usage of this method is to use the JMSReplyTo destination of the request when the producer is used to send a reply request.

The client can specify the default transmission mode, priority, and lifecycle of messages sent by the producer. Each time a client creates a MessageProducer, it defines a new message series. These messages are irrelevant to the previously sent messages.

Message consumer

The client uses MessageConsumer to receive messages from the destination. MessageConsumer creates a message by passing Queue or Topic to the createConsumer method of the Session.

A consumer can be created with a message selector. This allows the client to restrict that messages sent to consumers must match the selector. The client can simultaneously obtain the consumer's message, or asynchronously transmit the message when the message arrives.

Synchronous DeliverySynchronous Transmission

The client can use the receive method of MessageConsumer to request the next message from MessageConsumer. There are several changes to Receive that allow the client to poll or wait the Next message.

Asynchronous DeliveryAsynchronous transmission

The client can register an object that uses MessageConsumer to implement the JMS MessageListener interface. When a message reaches the consumer, the provider sends it by calling the onMessage method of the listener. The listener may throw a RuntimeException, but this mainly takes into account client program errors. A good listener should capture these exceptions and try to send these messages to some form of "unprocessable message" Destination specified by the application.

The result of RuntimeException thrown by the listener depends on the session validation mode:


The message will be resent immediately. The number of resends before giving up depends on the provider. In this case, the JMSRedelivered message header field is set in the resending message.


The next message of the listener will be sent. If the client wants to resend an unconfirmed message, it must resume the session manually.

N Transacted Session

The next message of the listener is sent. The client can submit or roll back the session. (In other words, RuntimeException does not cause automatic session rollback.) The JMS service provider should mark the client that throws an exception from the message listener as "possible obstacle ".


A message used for communication between clients. The core of the message system is of course a message. JMS provides several Message types for different types of content, but all messages are derived from the Message interface.

Message Type


Overview of common methods


Text message

GetText, setText


Ing message

SetString, getString


Byte message

WriteBytes, readBytes


Stream message

WriteString, readString


Object message

SetObject, getObject

Table common message classes

Message is divided into three parts:


Is a set of standard fields, which are used by clients and providers to identify and route messages.


Provides a utility for adding optional title fields to messages. If the application needs to classify or classify a message using a method not provided by the standard title field, you can add an attribute for the message to implement this classification and classification; provides setProperty (...) and getProperty (...) methods To set and obtain attributes of various Java types, including objects. JMS defines a set of standard attributes that the provider can selectively provide.


Contains the content that will be sent to the receiving application. Each message interface is dedicated to the content types it supports.

The following lists the names of each header field of Message, its corresponding Java type and field description:

JMSMessageID --Type:String

Uniquely identifies each message sent by the provider. This field is set by the provider during the sending process. The client can determine the message JMSMessageID only after the message is sent.

JMSDestination -- type: Destination

The Destination of message sending, which is set by the provider during the sending process.


The value is DeliveryMode. PERSISTENT or DeliveryMode. NON_PERSISTENT. A persistent message is transmitted only once, and a non-persistent message is transmitted once at most. You must know that "at most once" includes no transmission at all. Non-persistent messages are lost by the provider when the application or system fails. Therefore, be extremely careful to ensure that persistent messages are not affected by faults. This overhead is generally considered to be the overhead for sending persistent messages. When determining the message sending mode, you must carefully consider the overhead to balance the reliability and performance.

JMSTimestamp --Type:Long

The time when the provider sends the message, which is set by the provider during the sending process.

JMSExpiration --Type:Long

The time when the message expires. This value is calculated during the sending process and is the sum of the time-to-live value and the current time value of the sending method. The provider should not send expired messages. The value 0 indicates that the message does not expire.

JMSPriority --Type:Int

The priority of a message, which is set by the provider during sending. The priority of priority 0 is the lowest, and priority of priority 9 is the highest.

JMSCorrelationID --Type:String

It is usually used to link the Response Message and request message, which is set by the JMS program that sends the message. The JMS program responding to a message from another JMS program copies the JMSMessageID of the positive response message to this field. In this way, a program that is making a response can be associated with the response of a specific request it sends.


The request program uses it to indicate where the reply message should be sent, which is set by the JMS program that sends the message.


The JMS program uses it to indicate the type of the message. Some providers maintain a Message Type warehouse and use this field to reference the definition type in the warehouse. In this case, the JMS program should not use this field.

JMSRedelivered --Type:Boolean

It indicates that the message is sent to the JMS program too early, and the program does not know who the message receiver is; it is set by the provider during the receiving process.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.