所有配置基于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
#集群配置
#spring.activemq.broker-url=failover:(tcp://172.18.1.188:61616,tcp://172.18.1.18:61616)
//activeMQ用户名,根据实际情况配置
#spring.activemq.user=
//activeMQ密码,根据实际情况配置
#spring.activemq.password=
//是否启用内存模式(也就是不安装MQ,项目启动时同时也启动一个MQ实例)
spring.activemq.in-memory=false
//是否替换默认的connectionFactory
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
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管理界面
扩展:自定义消息转换器
在前面的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