1+ package io .kimmking .springboot01 .jms ;
2+
3+ import org .apache .activemq .ActiveMQConnectionFactory ;
4+ import org .apache .activemq .command .ActiveMQQueue ;
5+ import org .apache .activemq .command .ActiveMQTopic ;
6+ import org .springframework .beans .factory .annotation .Value ;
7+ import org .springframework .context .annotation .Bean ;
8+ import org .springframework .context .annotation .Configuration ;
9+ import org .springframework .jms .config .JmsListenerContainerFactory ;
10+ import org .springframework .jms .config .SimpleJmsListenerContainerFactory ;
11+ import org .springframework .jms .core .JmsMessagingTemplate ;
12+
13+ import javax .jms .ConnectionFactory ;
14+ import javax .jms .Queue ;
15+ import javax .jms .Topic ;
16+
17+ @ Configuration
18+ public class BeanConfig {
19+
20+ @ Value ("${spring.activemq.broker-url}" )
21+ private String brokerUrl ;
22+
23+ @ Value ("${spring.activemq.user}" )
24+ private String username ;
25+
26+ @ Value ("${spring.activemq.topic-name}" )
27+ private String password ;
28+
29+ @ Value ("${spring.activemq.queue-name}" )
30+ private String queueName ;
31+
32+ @ Value ("${spring.activemq.topic-name}" )
33+ private String topicName ;
34+
35+ @ Bean (name = "queue" )
36+ public Queue queue () {
37+ return new ActiveMQQueue (queueName );
38+ }
39+
40+ @ Bean (name = "topic" )
41+ public Topic topic () {
42+ return new ActiveMQTopic (topicName );
43+ }
44+
45+ @ Bean
46+ public ConnectionFactory connectionFactory () {
47+ return new ActiveMQConnectionFactory (username , password , brokerUrl );
48+ }
49+
50+ @ Bean
51+ public JmsMessagingTemplate jmsMessageTemplate () {
52+ return new JmsMessagingTemplate (connectionFactory ());
53+ }
54+
55+ // 在Queue模式中,对消息的监听需要对containerFactory进行配置
56+ @ Bean ("queueListener" )
57+ public JmsListenerContainerFactory <?> queueJmsListenerContainerFactory (ConnectionFactory connectionFactory ) {
58+ SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory ();
59+ factory .setConnectionFactory (connectionFactory );
60+ factory .setPubSubDomain (false );
61+ return factory ;
62+ }
63+
64+ //在Topic模式中,对消息的监听需要对containerFactory进行配置
65+ @ Bean ("topicListener" )
66+ public JmsListenerContainerFactory <?> topicJmsListenerContainerFactory (ConnectionFactory connectionFactory ) {
67+ SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory ();
68+ factory .setConnectionFactory (connectionFactory );
69+ factory .setPubSubDomain (true );
70+ return factory ;
71+ }
72+ }
73+
0 commit comments