File tree Expand file tree Collapse file tree
09mq/activemq-demo/src/main/java/io/byk/activemq Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33
44import static io .byk .activemq .config .ActiveMqConfig .ACTIVE_MQ_QUEUE ;
5+ import static io .byk .activemq .config .ActiveMqConfig .ACTIVE_MQ_TOPIC ;
56
67import java .util .Queue ;
78
1314import org .springframework .boot .autoconfigure .SpringBootApplication ;
1415
1516import io .byk .activemq .queue .QueueProducer ;
17+ import io .byk .activemq .topic .TopicPublisher ;
1618import lombok .extern .slf4j .Slf4j ;
1719
1820/**
2426public class JmsActiveMqApplication implements ApplicationRunner {
2527 @ Resource
2628 private QueueProducer queueProducer ;
29+ @ Resource
30+ private TopicPublisher topicPublisher ;
2731
2832 public static void main (String [] args ) {
2933 SpringApplication .run (JmsActiveMqApplication .class , args );
@@ -32,9 +36,13 @@ public static void main(String[] args) {
3236 @ Override
3337 public void run (ApplicationArguments args ) throws Exception {
3438 for (int i = 0 ; i < 10 ; i ++) {
35- String message ;
36- log .info (message = "生产消息" + i );
39+ String message = "队列消息" + i ;
3740 queueProducer .sendMessage (ACTIVE_MQ_QUEUE , message );
3841 }
42+
43+ for (int i = 0 ; i < 10 ; i ++) {
44+ String message = "主题消息" + i ;
45+ topicPublisher .sendMessage (ACTIVE_MQ_TOPIC , message );
46+ }
3947 }
4048}
Original file line number Diff line number Diff line change 1+ package io .byk .activemq .topic ;
2+
3+ import javax .jms .ConnectionFactory ;
4+
5+ import org .springframework .beans .factory .annotation .Qualifier ;
6+ import org .springframework .context .annotation .Bean ;
7+ import org .springframework .context .annotation .Configuration ;
8+ import org .springframework .jms .config .JmsListenerContainerFactory ;
9+ import org .springframework .jms .config .SimpleJmsListenerContainerFactory ;
10+
11+ /**
12+ * @author boyunkai <boyunkai@kuaishou.com>
13+ * Created on 2021-02-05
14+ */
15+ @ Configuration
16+ public class JmsContainerConfig {
17+
18+ @ Bean
19+ public JmsListenerContainerFactory <?> myJmsContainerFactory (@ Qualifier ("jmsConnectionFactory" )
20+ ConnectionFactory connectionFactory ) {
21+ SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory ();
22+ factory .setConnectionFactory (connectionFactory );
23+ factory .setPubSubDomain (true );
24+ return factory ;
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package io .byk .activemq .topic ;
2+
3+ import javax .annotation .Resource ;
4+ import javax .jms .Destination ;
5+
6+ import org .apache .activemq .command .ActiveMQQueue ;
7+ import org .apache .activemq .command .ActiveMQTopic ;
8+ import org .springframework .jms .core .JmsMessagingTemplate ;
9+ import org .springframework .stereotype .Service ;
10+
11+ import lombok .Getter ;
12+ import lombok .extern .slf4j .Slf4j ;
13+
14+ /**
15+ * 队列生产者
16+ * @author boyunkai <boyunkai@kuaishou.com>
17+ * Created on 2021-02-05
18+ */
19+ @ Service
20+ @ Slf4j
21+ @ Getter
22+ public class TopicPublisher {
23+ // JMS 模板
24+ @ Resource
25+ private JmsMessagingTemplate jmsMessagingTemplate ;
26+
27+ public void sendMessage (String destinationName , String message ) {
28+ log .info ("============> 发送 Topic 消息" + message );
29+ Destination destination = new ActiveMQTopic (destinationName );
30+ jmsMessagingTemplate .convertAndSend (destination , message );
31+ }
32+
33+ }
Original file line number Diff line number Diff line change 1+ package io .byk .activemq .topic ;
2+
3+ import static io .byk .activemq .config .ActiveMqConfig .ACTIVE_MQ_QUEUE ;
4+ import static io .byk .activemq .config .ActiveMqConfig .ACTIVE_MQ_TOPIC ;
5+
6+ import org .springframework .jms .annotation .JmsListener ;
7+ import org .springframework .stereotype .Service ;
8+
9+ import lombok .extern .slf4j .Slf4j ;
10+
11+ /**
12+ * 队列消费者
13+ *
14+ * @author boyunkai <boyunkai@kuaishou.com>
15+ * Created on 2021-02-05
16+ */
17+ @ Service
18+ @ Slf4j
19+ public class TopicSubscriber {
20+ @ JmsListener (destination = ACTIVE_MQ_TOPIC ,containerFactory = "myJmsContainerFactory" )
21+ public void receiveMessage (String message ) {
22+ log .info ("<=========== 收到消息" + message );
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments