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. |