Springboot 整合activeMq,使用@jmsListener
阿新 • • 發佈:2018-12-28
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); } }