Springboot 整合activeMq,使用@jmsListener

1、编写连接工厂

package com.example.test.conf;

import org.apache.activemq.ActiveMQConnectionFactory;
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 javax.jms.ConnectionFactory;
import javax.jms.JMSException;


/**
 * @author wanjiadong
 * @description
 * @date Create in 9:38 2018/12/6
 */
@Configuration
@EnableJms
public class ActiveMqConf {


    @Bean
    public javax.jms.Connection activeConnection() throws JMSException {
        ConnectionFactory connectionFactory = this.connectionFactory();

        return connectionFactory.createConnection();
    }

    public ConnectionFactory connectionFactory(){

        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL("tcp://10.10.82.37:61616");
        connectionFactory.setUserName("admin");
        connectionFactory.setPassword("admin");
        return connectionFactory;

    }

    @Bean
    public DefaultJmsListenerContainerFactory jmsTopicListenerContainerFactory() {

        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

        factory.setConnectionFactory(connectionFactory());
        //true为topic,false为queue
        factory.setPubSubDomain(true);
//        factory.setConcurrency("3-10");
        factory.setRecoveryInterval(1000L);
        return factory;
    }
}

2、建立消费者

package com.example.test.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.TextMessage;

/**
 * @author wanjiadong
 * @description
 * @date Create in 10:16 2018/12/6
 */
@Slf4j
@Component
public class TestConsumer {

    @JmsListener(destination = "test1", containerFactory = "jmsTopicListenerContainerFactory")
    public void handler(String msg) {
        log.info("revice msg = " + msg);
    }
}

猜你喜欢

转载自blog.csdn.net/baidu_20608025/article/details/84849278