ZhaoYM_jz 2017-04-25 08:35 采纳率: 50%
浏览 5951
已结题

ActiveMQ 有时候接收不到消息

使用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());
        }
    }

}

[图片说明

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-04-27 01:05
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出