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"
)
;
}
}
application.properties修改
#
如果为
True
,则是
Topic
;如果是
false
或者默认则是
queue
。
spring.jms.pub-sub-domain
=
true
(消费服务)
####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
(生产服务)
####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
####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>
######################################################################
2.生产者双向队列进行修改
//
双向队列
@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(
"---
结束
--"
)
;
}
3.消费者中修改
@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
;
}
}