strugglm
10/9/2018 - 2:17 PM

springboot activemq的@configuration类

import com.springboot.plus.service.Impl.TopicListener;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;

import javax.jms.Topic;

/**
 * Descritpion:
 *
 * @author Jeason
 * @date 2018/10/8
 */
@Configuration
@EnableJms
public class TopicConfig {
    /**
     * application.yml中的话题
     */
    @Value("${spring.activemq.topic1}")
    private String topic;
    @Value("${spring.activemq.topic2}")
    private String topic2;

    /**
     * 实现MessageListener接口的监听器
     */
    @Autowired
    private TopicListener topicListener;

    /**
     * 基本配置信息
     */
    @Value("${spring.activemq.broker-url}")
    private String broker;
    @Value("${spring.activemq.user}")
    private String user;
    @Value("${spring.activemq.password}")
    private String password;


    public Topic topic(String topicName) {
        return new ActiveMQTopic(topicName);
    }


    /**
     * 连接工厂
     */
    @Bean
    public ActiveMQConnectionFactory connectionFactory() {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(broker);
        connectionFactory.setPassword(user);
        connectionFactory.setUserName(password);
        return connectionFactory;
    }

    /**
     * 发布订阅模式的JmsTemplate:
     */
    @Bean(name = "jmsTemplate_topic")
    public JmsTemplate jmsTemplate() {
        JmsTemplate template = new JmsTemplate();
        template.setConnectionFactory(connectionFactory());
        template.setPubSubDomain(true);
        return template;
    }

    /**
     * JMS监听容器工厂(使用@JmsListener注解必须配置)
     */
    @Bean(name = "jmsTopicListener")
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
        //创建JMS监听容器工厂(使用@JmsListenr需要配置JMS监听容器工厂)
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        //连接工厂
        factory.setConnectionFactory(connectionFactory());
        //支持事务
        factory.setSessionTransacted(true);
        //发布订阅模式
        factory.setPubSubDomain(true);
        //并发量
        factory.setConcurrency("3-10");
        return factory;
    }

    /**
     * MessageListenerContainer:
     * 监听MessageListener
     */
    @Bean
    public DefaultMessageListenerContainer messageListenerContainer() {
        DefaultMessageListenerContainer messageListener = new DefaultMessageListenerContainer();
        //连接工厂
        messageListener.setConnectionFactory(connectionFactory());
        //消息监听器
        messageListener.setMessageListener(topicListener);
        //监听的topic
        messageListener.setDestination(topic(topic2));
        return messageListener;
    }
}