File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 55 <parent >
66 <groupId >org.springframework.boot</groupId >
77 <artifactId >spring-boot-starter-parent</artifactId >
8- <version >2.3.0 .RELEASE</version >
8+ <version >2.2.6 .RELEASE</version >
99 <relativePath /> <!-- lookup parent from repository -->
1010 </parent >
1111 <groupId >io.kimmking.javacourse.mq</groupId >
2424 <artifactId >spring-boot-starter</artifactId >
2525 </dependency >
2626
27- <dependency >
28- <groupId >org.apache.activemq</groupId >
29- <artifactId >activemq-all</artifactId >
30- <version >5.16.0</version >
31- </dependency >
32-
3327 <dependency >
3428 <groupId >org.projectlombok</groupId >
3529 <artifactId >lombok</artifactId >
4034 <artifactId >spring-boot-starter-test</artifactId >
4135 <scope >test</scope >
4236 </dependency >
37+
38+ <dependency >
39+ <groupId >org.springframework.boot</groupId >
40+ <artifactId >spring-boot-starter-activemq</artifactId >
41+ </dependency >
4342 </dependencies >
4443
4544 <build >
5857 </plugin >
5958 </plugins >
6059 </build >
61-
6260</project >
Original file line number Diff line number Diff line change 1+ package io .byk .activemq ;
2+
3+
4+ import static io .byk .activemq .config .ActiveMqConfig .ACTIVE_MQ_QUEUE ;
5+
6+ import java .util .Queue ;
7+
8+ import javax .annotation .Resource ;
9+
10+ import org .springframework .boot .ApplicationArguments ;
11+ import org .springframework .boot .ApplicationRunner ;
12+ import org .springframework .boot .SpringApplication ;
13+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
14+
15+ import io .byk .activemq .queue .QueueProducer ;
16+ import lombok .extern .slf4j .Slf4j ;
17+
18+ /**
19+ * @author boyunkai <boyunkai@kuaishou.com>
20+ * Created on 2021-02-05
21+ */
22+ @ SpringBootApplication
23+ @ Slf4j
24+ public class JmsActiveMqApplication implements ApplicationRunner {
25+ @ Resource
26+ private QueueProducer queueProducer ;
27+
28+ public static void main (String [] args ) {
29+ SpringApplication .run (JmsActiveMqApplication .class , args );
30+ }
31+
32+ @ Override
33+ public void run (ApplicationArguments args ) throws Exception {
34+ for (int i = 0 ; i < 10 ; i ++) {
35+ String message ;
36+ log .info (message = "生产消息" + i );
37+ queueProducer .sendMessage (ACTIVE_MQ_QUEUE , message );
38+ }
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ package io .byk .activemq .config ;
2+
3+ /**
4+ * 常量类
5+ *
6+ * @author boyunkai <boyunkai@kuaishou.com>
7+ * Created on 2021-02-05
8+ */
9+ public class ActiveMqConfig {
10+ // 测试队列
11+ public static final String ACTIVE_MQ_QUEUE = "test.queue" ;
12+ // 测试主题
13+ public static final String ACTIVE_MQ_TOPIC = "test.topic" ;
14+ // 目标地址,61616 端口为 JMS 协议,具体查看在 apache-activemq-5.16.1/conf/activemq.xml
15+ }
Original file line number Diff line number Diff line change 1+ package io .byk .activemq .queue ;
2+
3+ import static io .byk .activemq .config .ActiveMqConfig .ACTIVE_MQ_QUEUE ;
4+
5+ import org .springframework .jms .annotation .JmsListener ;
6+ import org .springframework .stereotype .Service ;
7+
8+ import lombok .extern .slf4j .Slf4j ;
9+
10+ /**
11+ * 队列消费者
12+ *
13+ * @author boyunkai <boyunkai@kuaishou.com>
14+ * Created on 2021-02-05
15+ */
16+ @ Service
17+ @ Slf4j
18+ public class QueueConsumer {
19+ @ JmsListener (destination = ACTIVE_MQ_QUEUE )
20+ public void receiveMessage (String message ) {
21+ log .info ("<=========== 收到消息" + message );
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ package io .byk .activemq .queue ;
2+
3+ import javax .annotation .Resource ;
4+ import javax .jms .Destination ;
5+
6+ import org .apache .activemq .command .ActiveMQQueue ;
7+ import org .springframework .jms .core .JmsMessagingTemplate ;
8+ import org .springframework .stereotype .Service ;
9+
10+ import lombok .Getter ;
11+ import lombok .extern .slf4j .Slf4j ;
12+
13+ /**
14+ * 队列生产者
15+ * @author boyunkai <boyunkai@kuaishou.com>
16+ * Created on 2021-02-05
17+ */
18+ @ Service
19+ @ Slf4j
20+ @ Getter
21+ public class QueueProducer {
22+ // JMS 模板
23+ @ Resource
24+ private JmsMessagingTemplate jmsMessagingTemplate ;
25+
26+ public void sendMessage (String destinationName , String message ) {
27+ log .info ("============> 发送 Queue 消息" + message );
28+ Destination destination = new ActiveMQQueue (destinationName );
29+ jmsMessagingTemplate .convertAndSend (destination , message );
30+ }
31+
32+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ spring :
2+ activemq :
3+ broker-url : tcp://127.0.0.1:61616
4+ user : admin
5+ password : admin
6+ pool :
7+ enabled : false
You can’t perform that action at this time.
0 commit comments