springboot activemq(二) 同时使用jms的Queue(队列)和Topic(发布订阅)

1.在启动类Application.java中加入

@Configuration

@EnableJms

public class JmsConfig {

@Bean

public JmsListenerContainerFactory<?> topicListenerFactory(ConnectionFactory connectionFactory) {

DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

factory.setPubSubDomain(true);

factory.setConnectionFactory(connectionFactory);

//并发消费

factory.setTaskExecutor(Executors.newFixedThreadPool(6));

factory.setConcurrency("6");

return factory;

}

@Bean

public JmsListenerContainerFactory<?> queueListenerFactory(ConnectionFactory connectionFactory) {

DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();

factory.setPubSubDomain(false);

factory.setConnectionFactory(connectionFactory);

//并发消费

factory.setTaskExecutor(Executors.newFixedThreadPool(6));

factory.setConcurrency("6");

return factory;

}

@Bean

public Queue queue() {

return new ActiveMQQueue("queue");

}

@Bean

public Topic topic() {

return new ActiveMQTopic("topic");

}

}

2.application.properties修改

#如果为True,则是Topic;如果是false或者默认则是queue

spring.jms.pub-sub-domain=true

 

3.(消费服务)的application.properties

####activemq######

spring.activemq.broker-url=tcp://192.168.23.130:61616?jms.prefetchPolicy.all=2

#spring.activemq.broker-url=failover:(tcp://localhost:61616,tcp://localhost:61617)

#如果为 true,则是Topic;如果是false或者默认,则是queue。

spring.jms.pub-sub-domain=true

# 在考虑结束之前等待的时间

spring.activemq.close-timeout=5000

# 默认代理URL是否应该在内存中。如果指定了显式代理,则忽略此

spring.activemq.in-memory=true

# 是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。

spring.activemq.non-blocking-redelivery=false

# 是否用Pooledconnectionfactory代替普通的ConnectionFactory。

spring.activemq.pool.enabled=true

# 连接池最大连接数

spring.activemq.pool.max-connections=10

4.生产服务的application.properties

####activemq#####

# 消息发送端,需要采用 AsyncSend模式

spring.activemq.broker-url=tcp://192.168.23.130:61616?jms.useAsyncSend=true

#spring.activemq.broker-url=failover:(tcp://localhost:61616,tcp://localhost:61617)

#如果为 true,则是Topic;如果是 false 或者默认则是queue。

spring.jms.pub-sub-domain=true

# 在考虑结束之前等待的时间

spring.activemq.close-timeout=5000

# 默认代理URL是否应该在内存中。如果指定了显式代理,则忽略此值

spring.activemq.in-memory=false

# 是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。

spring.activemq.non-blocking-redelivery=false

# 是否用Pooledconnectionfactory代替普通的ConnectionFactory。

spring.activemq.pool.enabled=true

# 连接池最大连接数

spring.activemq.pool.max-connections=10

#空闲的连接过期时间,默认为30秒

#spring.activemq.pool.idle-timeout=30000

#强制的连接过期时间,与idleTimeout的区别在于:idleTimeout是在连接空闲一段时间失效,而expiryTimeout不管当前连接的情况,只要达到指定时间就失效。默认为0,never

#spring.activemq.pool.expiry-timeout=0

# 等待消息发送响应的时间。设置为0等待永远。

#spring.activemq.send-timeout=3000

#spring.activemq.user=admin

#spring.activemq.password=admin

#如果此处设置为true,需要加如下的依赖包,否则会自动配置失败,报JmsMessagingTemplate注入失败

#<dependency>

#<groupId>org.apache.activemq</groupId>

#<artifactId>activemq-pool</artifactId>

#<!-- <version>5.7.0</version> -->

#</dependency>

######################################################################

 

5.生产者双向队列进行修改

// 双向队列

@JmsListener(destination = "SendTo_one.queue",containerFactory="queueListenerFactory")

public void consumerMessage(String text) {

System.out.println("out.queue队列收到的回复报文为:" + text);

System.out.println("---结束--");

}

@JmsListener(destination = "SendTo_mytwo.queue",containerFactory="queueListenerFactory")//必须加

public void consumerMessagetwo(String text) {

System.out.println("omytwo.queue队列收到的回复报文为:" + text);

System.out.println("---结束--");

}

@JmsListener(destination = "SendTo_mytwo.topic")//,containerFactory="topicListenerFactory"//不加也可以接受

public void consumerMessagetwotopic(String text) {

System.out.println("mytwo.topic队列收到的回复报文为:" + text);

System.out.println("---结束--");

}

@JmsListener(destination = "SendTo_mythree.queue",containerFactory="queueListenerFactory")//必须加

public void consumerMessageythree(String text) {

System.out.println("three.queue队列收到的回复报文为:" + text);

System.out.println("---结束--");

}

@JmsListener(destination = "SendTo_mythree.topic")//,containerFactory="topicListenerFactory"//不加也可以接受

public void consumerMessageythreetopic(String text) {

System.out.println("three.topic队列收到的回复报文为:" + text);

System.out.println("---结束--");

}

 

 

6.消费者中修改

 
 

@Component

public class TwoConsumer {

@JmsListener(destination = "mytwo.queue",containerFactory="queueListenerFactory")//必须加

@SendTo("SendTo_mytwo.queue") //为了实现双向队列

public String receiveQueue(String text) {

System.out.println(Thread.currentThread().getName()+"---mytwo.queue收到的报文为:" + text);

return "return message" + text;

}

@JmsListener(destination = "mytwo.topic")

@SendTo("SendTo_mytwo.topic") //为了实现双向队列

public String receiveTopic(String text) {

System.out.println(Thread.currentThread().getName()+"---mytwo.topic收到的报文为:" + text);

return "return message" + text;

}

}

@Component

public class ThreeConsumer {

@JmsListener(destination = "mytwo.queue",containerFactory="queueListenerFactory")//必须加

@SendTo("SendTo_mythree.queue") //为了实现双向队列

public String receiveQueue(String text) {

System.out.println(Thread.currentThread().getName()+"---mythree.queue收到的报文为:" + text);

return "return message" + text;

}

@JmsListener(destination = "mytwo.topic")

@SendTo("SendTo_mythree.topic") //为了实现双向队列

public String receiveTopic(String text) {

System.out.println(Thread.currentThread().getName()+"---mythree.topic收到的报文为:" + text);

return "return message" + text;

}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值