springboot2.x整合activemq

依赖:

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.11.RELEASE</version>
		<relativePath />
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-activemq</artifactId>
		</dependency>
	</dependencies>

配置:

spring:
  jms: 
     pub-sub-domain: true
     template:
        delivery-mode: NON_PERSISTENT
        #default
        #delivery-mode: PERSISTENT
  activemq:
    user: admin
    password: admin123
     #true 表示使用内置的MQ,false则连接服务器
    in-memory: true
      # 给java用的tcp端口是61616
    broker-url: tcp://localhost:61616
      #需要加入配置文件,支持发布订阅模型,默认只支持点对点
logging:
   level: 
      root: info

topic监听

@Component
public class ActiveMqHandler {
	@JmsListener(destination = "test1111.12")
	public void l1(String content) {
		System.out.println(content);
	}
}
	@Autowired
	private JmsTemplate jmsTemplate;
	
	
	public void run() throws Exception {
		jmsTemplate.convertAndSend(new ActiveMQQueue("xxx1"), "12345");
	}

 

这就可以了.

其中spring做了一些事情.

比如说:如果没有配置,spring启动了默认的DefaultJmsListenerContainerFactoryConfigurer可用于初始化的实例

其中spring.jms.pub-sub-domain默认是false,如果是false,JmsListener默认监听的destination 是queue.如果是true,则监听的是topic

 

如果要进一步配置clientid的话,目前没有spring.application.properties供此,那么需要这么做:

注意,新的factory会顶替掉默认的DefaultJmsListenerContainerFactoryConfigurer,所以application.yml里配置的spring.jms.pub-sub-domain失效了,那么依然要bean.setPubSubDomain(true);

@Configuration
public class ActiveMqConfig {
	
	{
		
	}
	@Bean(name="myFactory")
    public DefaultJmsListenerContainerFactory  jmsListenerContainerTopic(ConnectionFactory factory) {
		SingleConnectionFactory singleConnectionFactory = (SingleConnectionFactory) factory;
		singleConnectionFactory.setClientId("123123123");
		
		
		
		DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(singleConnectionFactory);
        bean.setAutoStartup(true);
        return bean;
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值