使用ActiveMQ做个小例子的时候, 有时候消息接收不到,有时候可以,没有被消费的消息,再重启后又能全部接收到,不知道问题出在哪。谢谢各位大神指点
Spring ActiveMQ 配置如下
package com.zym.robot.config;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.jms.connection.CachingConnectionFactory;
import com.zym.robot.constant.Constant;
@Configuration
@Lazy(false)
public class ConnectionFactoryConfig{
@Autowired
private ApplicationUtil applicationUtil;
@Bean(name="activeMQConnectionFactory")
public ActiveMQConnectionFactory activeMQConnectionFactory(){
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
activeMQConnectionFactory.setBrokerURL(Constant.activemq_brokerurl);
activeMQConnectionFactory.setUserName(Constant.activemq_username);
activeMQConnectionFactory.setPassword(Constant.activemq_password);
activeMQConnectionFactory.setTrustAllPackages(true);
return activeMQConnectionFactory;
}
@Bean(name="connectionFactory")
public CachingConnectionFactory connectionFactory(){
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
cachingConnectionFactory.setTargetConnectionFactory((ActiveMQConnectionFactory)applicationUtil.getBean("activeMQConnectionFactory"));
cachingConnectionFactory.setSessionCacheSize(10);
return cachingConnectionFactory;
}
}
package com.zym.robot.config;
import javax.jms.Destination;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
@Configuration
@Import(ConnectionFactoryConfig.class)
@Lazy(false)
public class MQTemplateFactory {
@Autowired
@Qualifier("connectionFactory")
private CachingConnectionFactory connectionFactory;
@Autowired
private ApplicationUtil applicationUtil;
/**
* @author ZhaoYM
* @description 微信消息队列
* @return
*/
@Bean(name="messageQueueTemplate")
public JmsTemplate messageQueueTemplate(){
JmsTemplate template = new JmsTemplate();
template.setPubSubNoLocal(false);
template.setConnectionFactory(connectionFactory);
template.setDefaultDestination((ActiveMQQueue)applicationUtil.getBean("messageQueue"));
return template;
}
/**
* @author ZhaoYM
* @description 登录消息队列
* @return
*/
@Bean(name="loginQueueTemplate")
public JmsTemplate loginQueueTemplate(){
JmsTemplate template = new JmsTemplate();
template.setPubSubNoLocal(false);
template.setConnectionFactory(connectionFactory);
template.setDefaultDestination((ActiveMQQueue)applicationUtil.getBean("loginQueue"));
return template;
}
@Bean(name="messageQueue")
public Destination messageQueue(){
ActiveMQQueue activeMQQueue = new ActiveMQQueue();
activeMQQueue.setPhysicalName("messageQueue");
return activeMQQueue;
}
@Bean(name="loginQueue")
public Destination loginQueue(){
ActiveMQQueue activeMQQueue = new ActiveMQQueue();
activeMQQueue.setPhysicalName("loginQueue");
return activeMQQueue;
}
}
发送消息代码
package com.zym.robot.activemq;
import java.io.Serializable;
import org.springframework.jms.core.JmsTemplate;
public class SendMessage {
/**
* @author ZhaoYM
* @description 发送消息
* @param object
* @param jmsTemplate
*/
public static void sendMessage(Serializable object, JmsTemplate jmsTemplate){
SimpleMessageCreatorImpl messageCreator = new SimpleMessageCreatorImpl();
messageCreator.setMessage(object);
jmsTemplate.send(messageCreator);
}
}
package com.zym.robot.activemq;
import java.io.Serializable;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.core.MessageCreator;
/**
* @author ZhaoYM
* @description message 简单实现
*/
public class SimpleMessageCreatorImpl implements MessageCreator{
private Serializable message;
public Serializable getMessage() {
return message;
}
public void setMessage(Serializable message) {
this.message = message;
}
@Override
public Message createMessage(Session session) throws JMSException {
if (message == null) {
return session.createTextMessage("默认消息");
}else {
return session.createObjectMessage(getMessage());
}
}
}