ActiveMQ Learning (1) Basic Concepts of JSM

1. The basic components of JMS:

1. Connection factory: ConnectionFactory is the object used by customers to create connections

2. Connection: Connection encapsulates a virtual connection between the client and the JMS provider

3. Session: Session is a single-threaded context for producing and consuming messages. Used to create message producers (producers), production consumers (consumers) and messages (messages), etc.

Sessions provide a transactional context in which the sending and receiving of a set of messages is combined into a single atomic operation.

4. Destination: Destination  is an object used by customers to specify the target of producing messages and the source of consuming messages.

JMS currently defines two messaging domains, which can be understood as delivery methods. One is point-to-point (PTP) messaging and publish/subscribe messaging

In the point-to-point mode, the destination is called a queue (queue), in the publish/subscribe mode, the destination is called a topic (topic)

Point-to-point (PTP) features:

(1) Each message can only have one consumer

(2) There is no temporal relativity between producers and consumers of messages. A consumer can fetch messages whether or not it is running when a producer sends a message.

Publish/subscribe model features:

(1) Each message can have multiple consumers.

(2) There is a temporal correlation between producers and consumers. A consumer who subscribes to a topic can only consume messages published by the producer after he subscribes

(3) JMS allows customers to create durable subscriptions, which allow consumers to consume messages sent when they are not active

5. Producer: Message Procedureur    The message producer is an object created by the session to send the message to a destination.

6. Consumer: Message Consumer  The message consumer is also an object created by the session to receive messages from the destination.

There are two ways to consume messages:

(1) Synchronous consumption, by calling the receive method of the consumer to extract the message displayed from the destination. The receive method can block until a message arrives.

(2) Asynchronous consumption, the customer can register a message listener for the consumer to define what action to take when the message arrives

7. Message: The massage message consists of three parts:

(1) Message header, each message header field has corresponding getter and setter methods

(2) Message attribute, if you need to eliminate the unexpected value of the message header field, you can use the message attribute

(3) Message body, that is, message content

The message types are: TestMessage, MapMessage, BytesMessage, StreamMessage, ObjectMessage

2. Message Confirmation

Only after being confirmed can it be considered to have been successfully consumed. Successful consumption of messages usually consists of three phases:

The client receives the message, the client processes the message, and the message is acknowledged.

In a transactional session, confirmation occurs automatically when a transaction is committed. In a non-transactional session, when a message is acknowledged depends on the acknowledgment mode when the session was created.

(1) Session.AUTO_ACKNOWLEDGE When the client successfully returns from the receive method, or when the MessageListener.onMessage method returns successfully

Conversation automatically acknowledges messages received by clients

(2) Session.CLIENT_ACKNOWLEDGE The client confirms the message through the acknowledge method of the message. Note: In this mode, confirmation is performed on the session layer

Acknowledging a consumed message will automatically acknowledge all messages already consumed by the session. Example: Consumer A consumes 10 messages and then confirms the 5th message, then all messages

All 10 messages are confirmed

(3) Session.DUPS_ACKNOWLEDGE This option is only the confirmation message submitted by the session. If the Provider fails, it may cause some repeated messages. If

If the message is repeated, the Provider must set the JMSRedelivered field of the message header to true.


3. Message Persistence

JMS supports two message submission modes:

PERSISTENT. Instruct the JMS Provider to persist the message to ensure that the message will not be lost due to the failure of the JMS Provider.

NON_PERSISTENT. Does not require JMS Provider to persist messages

4. Message priority

Priority Message priority can be used to instruct the JMS Provider to submit urgent messages first. There are 10 levels of priority, from 0 (lowest) to 9 (highest)

If no priority is specified, the default level is 4. It should be noted that JMS Provider does not necessarily guarantee that messages will be submitted in order of priority.

5. Message Expiration

You can set the message to expire after a certain period of time, and the default is never to expire

6. Temporary destination

Temporary destinations can be created through the createTemporaryQueue method and createTemporaryTopic method on the session , and their existence time is limited to the time maintained by the connection that created them

Only message consumers on the connection that created the temporary destination will be able to extract messages from the temporary destination

7. Durable Subscription

First the message producer must use PERSISTENT to submit the message. Clients can pass createDurableSubscriber(Topic var1, String var2) on the session

The first parameter of the method must be a topic, and the second parameter is the name of the subscription.

The JMS Provider will store messages published to the topic corresponding to the durable subscription , if the client that originally created the durable subscription or any other client uses the same connection factory and connection client ID,

Call the createDurableSubscriber method on the session again with the same topic and the same subscription name, then the durable subscription will be activated

The JMS Provider will send to the client the messages that were published when the client was inactive. A durable subscription can only have one active subscriber at a time.

Once created, a durable subscription persists until the application calls the unsubscribe method on the session.

8. Local things

In a JMS client, local transactions can be used to combine the sending and receiving of messages.

The JMS Session interface provides commit and rollback methods. Transaction commit means that all messages produced are sent and all messages consumed are confirmed .

A transaction rollback means that all messages produced are destroyed, and all messages consumed are restored and resubmitted, unless they have expired .

 Transactional sessions are always involved in transaction processing. Once the commit or rollback method is called, one transaction ends and another transaction starts . Closing a transactional session will rollback the transaction within it

Notice:

(1) If you use the request/reply mechanism, that is, send a message and hope to receive a reply to the message in the same transaction, the program will be suspended, because the sending operation will not be executed until the transaction is committed. 

(2) The production and consumption of messages cannot be included in the same transaction



JMS public peer-to-peer domain  Pub/Sub domain
ConnectionFactory (message factory) QueueConnectionFactory TopicConnectionFactory
Connection QueueConnection TopicConnection
Destination Queue Topic
Session QueueSession TopicSession
MessageProducer (message producer) QueueSender TopicPublisher
MessageConsumer (message consumer) QueueReceiver TopicSubscriber


Guess you like

Origin blog.csdn.net/u010994966/article/details/77895374