This article is a mirror article of machine translation, please click here to jump to the original article.

View: 13505|Reply: 2

ActiveMQ persistent subscription settings

[Copy link]
Posted on 1/9/2017 11:12:10 AM | | |

ActiveMQ persistent subscription settings
By setting the PERSISTENT delivery mode for the publisher, you can use persistent subscriptions for subscribers, which guarantees that the Pub/Sub program receives all published messages.
Message subscriptions are divided into non-durable subscriptions and durable subscriptions, and non-durable subscriptions can only receive messages sent to a topic when the client is active, that is, when the client is offline, messages sent to the topic during this period will be lost and never received. When the client is offline, ActiveMQ will save all messages sent to the subject according to its ID, and when the client connects to ActiveMQ again, it will get all the messages sent to the subject when it is offline. Persistent subscriptions increase overhead and have only one active user in a persistent subscription at a time. Steps to establish a persistent subscription:
1. Set up a customer ID for the connection;
2. Specify a subscription name for the topic of the subscription;
The above combination must be unique.
Code implements persistent subscriptions

(1) When using queue, that is, when queuing, there is only one consumer per message, so persistence is very simple, just save it to the database

。 Then, any consumer can pick it up and dispose of it. It doesn't matter if a consumer turns it off for a while.

(2) When using topic, that is, when subscribing, each message can have multiple consumers, which is more troublesome.

First, assuming that consumers are ordinary consumers,
------------------------
After the launch of <1> activemq was launched, news 1 was released, but unfortunately, no consumers are starting now, that is, no consumers have subscribed. So

, the news was abandoned.

<2> Consumer 1 started, connected to activemq, subscribed, and waited for a message~~

activemq posts message 2, OK, consumer 1 receives, and processes it. Message abandonment.

<3> Consumer 2 is also launched, connected to activemq, subscribed, and is waiting for a message~~

activemq publishes message 3, OK, consumer 1, consumer 2 are all received and processed. Message abandonment.

<4> Consumer 1 is turned off.

activemq posts message 4, OK, consumer 2 receives, and processes it. Message abandonment.

<5> Consumer 1 started again.

activemq publishes messages 5, OK, consumer 1, and consumer 2 all receive and process them. Message abandonment.
-----------------------------
To sum up:
activemq simply sends messages to the currently launched consumer.
Consumers who turn off will miss a lot of messages and will not be able to receive them again.

If the message sent is important user sync data and misses it, the user data is not synchronized.

So, how do you get consumers to receive missed messages when they restart?

The answer is a persistent subscription.
-----------------------------

Ordinary subscriptions, without distinguishing between consumers, if there are a few heads in the venue, throw a few steamed buns.
For persistent subscriptions, you need to record the name of the consumer.
Zhang San said, I am Zhang San, there are steamed buns for me, I will come back to get them.
Li Si said, I am Li Si, there are steamed buns for me, I will come back to get them.
activemq wrote down the names of Zhang San and Li Si.

Then, when dividing steamed buns, one head still gives a steamed bun.
After the division, when he saw that Zhang San didn't speak, it meant that he was not there, so leave him one.
Li Si spoke, so there was no need to stay.

Zhang San came back, looked for activemq, and took a look, this is not Zhang San, bring his steamed buns quickly.
It may be one steamed bun, or it may be 100 steamed buns, it depends on how many steamed buns Zhang San has divided since he left for a while.

activemq distinguishes consumers by clientID and subscriber name.
-----------------------------
Create a connection
connection = connectionFactory.createConnection();
connection.setClientID("bbb"); Persistent subscriptions require setting this.
connection.start();

Create a session
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

Create a destination
Topic topic = session.createTopic("userSyncTopic"); Topic name

//MessageConsumer consumer = session.createConsumer(topic); Regular subscription
MessageConsumer consumer = session.createDurableSubscriber(topic,"bbb"); Persistent subscriptions


Another point is that the producer of the message uses persistent mode when sending messages
MessageProducer producer = ...;
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
If not, it is persistent by default

(5) If the same "clientID" is used, it is considered to be the same consumer. If two programs use the same "clientID", only one can connect to activemq at the same time, and the second connection will report an error.

(6) The settings of activemq are in conf/activemq.xml, and the default messages are saved in data/kahadb, and the messages will not be lost when restarting activemq.

You can access http://localhost:8161/admin/index.jsp: view information about current queues, topics, and persistent subscribers, send messages, and more.

You can copy the content in the activemq-jdbc.xml and modify it, and you can save the message in other databases.




Previous:360 ticket grabbing fifth-generation VIP channel cracked, quickly enter the VIP line
Next:. .Net platform
 Landlord| Posted on 1/9/2017 1:41:53 PM |
1 Comparison of technical characteristics of queue and topic
name
Topic
Queue
Overview
Publish Subscribe messaging Publish subscription messages
Point-to-Point Point-to-Point
There is no state
topic data is stateless by default.
Queue data is saved as a file on the MQ server by default, for example, Active MQ is usually stored under $AMQ_HOME\data\kr-store\data. It can also be configured as a DB store.
Integrity guarantee
There is no guarantee that every piece of data published by the publisher will be accepted by the subscriber.
Queue guarantees that every piece of data can be received by the receiver.
whether the message will be lost
Generally speaking, when a publisher publishes a message to a topic, only the sub that is listening to the topic address can receive the message. If there is no sub listening, the topic is lost.
The Sender sends a message to the target Queue, and the receiver can receive messages on this Queue asynchronously. Messages on the Queue will not be lost if there is no receiver to pick them up for the time being.
Message release reception policy
One-to-many message publishing and receiving policy, multiple subs listening to the same topic address can receive messages sent by the publisher. The sub receives the notification to the mq server
One-to-one message publishing and receiving policies, messages sent by a sender can only be received by one receiver. After the receiver receives, the MQ server notifies the MQ server that it has been received, and the MQ server deletes or takes other actions on the messages in the queue.

 Landlord| Posted on 1/9/2017 1:42:15 PM |
The biggest difference between topic and queue is that topic is in the form of broadcasting, notifying all online listening clients that there is a new message, and the client without monitoring will not receive the message; Queue, on the other hand, notifies one of multiple listening clients in a peer-to-peer manner.

2 Comparison of message processing efficiency between topic and queue methods
        By increasing the number of concurrency of the listening client, verify whether the message push of the topic will decrease significantly due to the increase in concurrency of the listening client.
        From the measured results, there is no significant difference in the efficiency of sending and receiving messages sent by the topic method under the premise of one subscriber and 100 subscribers, but under the premise of 500 subscribers (threads) concurrency, the efficiency difference is obvious (due to the concurrency of 500 threads, the CPU usage rate of my machine is as high as 70-90%, so it is impossible to confirm whether it is a performance bottleneck caused by my local test or a performance bottleneck in the topic message sending method, resulting in such a significant decrease in efficiency).
        There is no significant difference in the efficiency of sending and receiving messages sent by topic method and queue mode under the premise of one subscriber and 100 subscribers, but under the premise of 500 subscribers concurrency, the efficiency of topic mode is significantly lower than that of queue.
        The efficiency of sending and receiving messages sent in the Queue method does not change significantly under the premise of one subscriber, 100 subscribers and 500 subscribers.
Topic measured data:


The total number of messages sent by the sender
The total number of messages received by all subscribers
The average time it takes to send and receive messages
Single subscriber
100
100
101ms
100 subscribers
100
10000
103ms
500 subscribers
100
50000
14162ms

Queue measured data:


The total number of messages sent by the sender
The total number of messages received by all subscribers
The average time it takes to send and receive messages
Single subscriber
100
100
96ms
100 subscribers
100
100
96ms
500 subscribers
100
100
100ms

Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com