Persistent (PERSISTENT)
- messageProducer.setDeliverMode (DeliverMode.NON_PERSISTENT); non-persistent: when the server is down, the message does not exist
- messgageProducer.setDeliveryMode (DeliveryMode.PERSISTENT); Persistence: When the server is down, the message remains.
Queue (queue) persistence:
The default queue is persistent mode, which ensure that these messages are transmitted only once and successfully used once. For these messages, reliability is a prime consideration.
Another important aspect is to ensure the reliability of a persistent message to the target, message service before transmitting them to the consumer does not lose the messages.
Theme (topic) persistence:
Consumer Code:
package pres.zhang.persistent;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import java.io.IOException;
public class Topic_Consumer {
public static final String ACTIVEMQ_URL = "tcp://localhost:61616";
public static final String TOPIC_NAME = "topic_acton";
public static void main(String[] args) throws JMSException, IOException {
//1.创建连接工厂,按照给定的url地址,采用默认用户名和密码
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
//2.通过连接工厂,获得连接connection,并启动访问
Connection connection = activeMQConnectionFactory.createConnection();
//connection.start();
//设置订阅用户
connection.setClientID("test");
//3.创建回话session
//两个参数 1.事务 2.签收
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//4.创建目的地(具体是队列还是主题topic)
Topic topic = session.createTopic(TOPIC_NAME);
//创建持久化的订阅 参数:1.主题 2.备注
TopicSubscriber topicSubscriber = session.createDurableSubscriber(topic, "remard");
//发布订阅
connection.start();
Message message = topicSubscriber.receive();
while (null != message){
TextMessage textMessage = (TextMessage) message;
System.out.println("收到的持久化topic:" + ((TextMessage) message).getText());
//继续监听
message = topicSubscriber.receive(1000L);
}
session.close();
connection.close();
}
}
Producers Code:
package pres.zhang.persistent;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class Topic_Produce {
public static final String ACTIVEMQ_URL = "tcp://localhost:61616";
public static final String TOPIC_NAME = "topic_acton";
public static void main(String[] args) throws JMSException {
//1.创建连接工厂,按照给定的url地址,采用默认用户名和密码
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
//2.通过连接工厂,获得连接connection,并启动访问
Connection connection = activeMQConnectionFactory.createConnection();
//connection.start();
//3.创建回话session
//两个参数 1.事务 2.签收
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//4.创建目的地(具体是队列还是主题topic)
Topic topic = session.createTopic(TOPIC_NAME);
//创建消息的生产者
MessageProducer messageProducer = session.createProducer(topic);
//通过使用messageProducer生产3条消息发送到MQ的队列里面
//设置持久化主题
messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
//因为设置持久化主题,所以connect.start()在这里调用
connection.start();
for (int i = 0; i < 3; i++) {
//7.创建消息
TextMessage textMessage = session.createTextMessage("TOPIC_NAME---" + i);//可以理解为字符串
//8.通过messageProducer发送给MQ
messageProducer.send(textMessage);
}
//9.关闭资源
messageProducer.close();
session.close();
connection.close();;
System.out.println("消息发布到TOPIC完成!");
}
}
Test: run the consumer side, thematic subscription:
Display has an active subscriber, because there is no message to the consumer spending, so the program has been listening. We close the program, changes are as follows:
Because our program is terminated, so the display to the offline field, but we have successfully subscribed.
Now run news producers:
Message sent successfully. We run again Consumers:
Console Print:
收到的持久化topic:TOPIC_NAME---0
收到的持久化topic:TOPIC_NAME---1
收到的持久化topic:TOPIC_NAME---2
Successfully received the message, because we have subscribed to the topic.
to sum up:
- Must first run a consumer, equal to register with the MQ, similar to what we subscribed to this topic.
- Message producer sends then run
- At this point, regardless of whether the consumer online, will receive, not online, then connect the next time, I will never received the message receives down.