Skip to content

Commit 5508eca

Browse files
author
boyunkai
committed
[feature]添加基于 JMS 的 ActiveMQ 实例
1 parent f5efa43 commit 5508eca

7 files changed

Lines changed: 123 additions & 9 deletions

File tree

09mq/activemq-demo/pom.xml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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>
@@ -24,12 +24,6 @@
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>
@@ -40,6 +34,11 @@
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>
@@ -58,5 +57,4 @@
5857
</plugin>
5958
</plugins>
6059
</build>
61-
6260
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

09mq/activemq-demo/src/main/resources/application.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
spring:
2+
activemq:
3+
broker-url: tcp://127.0.0.1:61616
4+
user: admin
5+
password: admin
6+
pool:
7+
enabled: false

0 commit comments

Comments
 (0)