所有配置基于spring boot 1.4.2
1.引入相关的maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2.application.properties中定义相关配置项
//activeMQ地址
spring.activemq.broker-url=tcp://172.18.1.18:61616
3.指定消息消费者
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
@Service
public class MQConsumerService {
@JmsListener(destination = "sample.queue") // 监听指定消息队列
public void receiveQueue(String message) {
System.out.println(message);
}
}
监听订阅主题
application.properties中定义相关配置项
spring.jms.pub-sub-domain=true
指定消息消费者
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
@Service
public class MQConsumerService {
@JmsListener(destination = "sample.topic") // 监听指定消息主题
public void receiveTopic(String message) {
System.out.println(message);
}
}
Queue与Topoic并存
1.application.properties中定义相关配置项
spring.jms.pub-sub-domain=true
spring.activemq.broker-url=tcp://172.18.1.18:61616
#spring.activemq.user=按实际情况配置
#spring.activemq.password=按实际情况配置
spring.activemq.in-memory=false
spring.activemq.pool.enabled=false
spring.activemq.pool.maxConnections=2
spring.activemq.pool.expiryTimeout=0
spring.activemq.pool.idleTimeout=30000
spring.activemq.packages.trust-all=true
2.定义配置类
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.config.JmsListenerContainerFactory;
@Configuration
@EnableJms
public class JmsConfiguration {
// topic模式的ListenerContainer
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setPubSubDomain(true);
bean.setConnectionFactory(activeMQConnectionFactory);
return bean;
}
// queue模式的ListenerContainer
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setConnectionFactory(activeMQConnectionFactory);
return bean;
}
}
3.定义监听器实现
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
@Service
public class MQConsumerService {
@JmsListener(destination = "portal.admin.topic",containerFactory = "jmsListenerContainerTopic") // 监听指定消息主题
public void receiveTopic(String message) {
System.out.println(message);
}
@JmsListener(destination = "portal.admin.queue",containerFactory = "jmsListenerContainerQueue") // 监听指定消息主题
public void receiveQueue(String message) {
System.out.println(message);
}
}
4.查看activeMQ管理界面
![](https://i-blog.csdnimg.cn/blog_migrate/794ec8b299727d5731cbbc96c39e04a9.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/d2c7acfc73d8e5c3a8a294c52f6cc2f8.webp?x-image-process=image/format,png)
扩展:自定义消息转换器
在前面的JmsConfiguration基础上添加JmsConfiguration定义,并在ListenerContainer的定义中设置消息转换器
import javax.jms.ConnectionFactory;
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.config.JmsListenerContainerFactory;
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.MessageType;
@Configuration
@EnableJms
public class JmsConfiguration {
/**
* topic模式的ListenerContainer
* @param activeMQConnectionFactory
* @return
*/
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setPubSubDomain(true);
bean.setConnectionFactory(activeMQConnectionFactory);
/**
* 使用消息转换器
*/
bean.setMessageConverter(jacksonJmsMessageConverter());
return bean;
}
/**
* queue模式的ListenerContainer
* @param activeMQConnectionFactory
* @return
*/
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setConnectionFactory(activeMQConnectionFactory);
/**
* 使用消息转换器
*/
bean.setMessageConverter(jacksonJmsMessageConverter());
return bean;
}
/**
* 消息转换器
* @return
*/
@Bean
public MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
return converter;
}
}
后续更新消息发送部分
参考链接:
https://github.com/spring-projects/spring-boot/pull/6356
http://serve.3ezy.com/stackoverflow.com/questions/40144561/how-to-listen-to-topic-using-spring-boot-jms
</div>