今天写一下关于Activemq发布订阅模式下的消息持久化。在发布订阅模式下想要实现消息持久化分为两部分,一部分是发布者设置消息持久化,另一部分是订阅者的订阅持久化。
1.发布者:
默认情况下,Activemq就是消息持久化的,是持久化到文件的,在conf目录下activemq.xml中有相关配置。
package com.tgb.SpringActivemq.mq.producer.topic;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
import com.tgb.model.User;
@Component("topicSender")
public class TopicSender {
@Autowired
@Qualifier("jmsTopicTemplate")
private JmsTemplate jmsTemplate;
/**
* 发送一条消息到指定的队列(目标)
* @param queueName 队列名称
* @param message 消息内容
*/
public void send1(String topicName,final User u){
jmsTemplate.send(topicName, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createObjectMessage(u);
}
});
}
}
具体的ActiveMQ.xml配置可以参考我的第一篇文章关于ActiveMQ的简单整理(1)。如果想设置为非持久化可以使用jmsTemplate调用其持久化方法设置。
2.订阅者:
订阅者采用java代码实现。
package com.satx.persistence;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicSession;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import com.tgb.model.User;
public class TopicReceiver {
public static void main(String[] args) {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ActiveMQConnectionFactory connectionFactory;
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
// Session: 一个发送或接收消息的线程
final Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// 消费者,消息接收者
// MessageConsumer 接口(消息消费者) 由会话创建的对象,用于接收发送到目标的消息。
// 消费者可以同步地(阻塞模式),或异步(非阻塞)接收队列和主题类型的消息。
MessageConsumer consumer;
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://10.0.0.61:61616");
try {
connectionFactory.setTrustAllPackages(true);
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
connection.setClientID("client_2");
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
//queue_name跟sender的保持一致,一个创建一个来接收
Topic topic = session.createTopic("test.topic1");
consumer = session.createDurableSubscriber(topic, "client_2");
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message arg0) {
// TODO Auto-generated method stub
User user;
try {
user = (User) ((ObjectMessage)arg0).getObject();
System.out.println("发送成功:"+user.getUsername()+"_"+user.getMobile());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
其中设置订阅持久化相关的代码:
connection.setClientID("client_2");
consumer = session.createDurableSubscriber(topic, "client_2");
其中,clientID必须一致。
有一点需要注意,以下代码中的false是设置消息自动签收的。
session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
若改为手动签收,则需要改为以下代码:
session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
session.commit();