Skip to content

Commit 54853b1

Browse files
committed
Spring Boot 中使用 RocketMQ
1 parent c2bdc2e commit 54853b1

8 files changed

Lines changed: 281 additions & 0 deletions

File tree

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
target
3+
*.iml

‎spring-boot-rocketmq/.gitignore‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/

‎spring-boot-rocketmq/pom.xml‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>io.ymq.example</groupId>
7+
<artifactId>spring-boot-rocketmq</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-rocketmq</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.10.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
33+
<!-- RocketMq客户端相关依赖 -->
34+
<dependency>
35+
<groupId>org.apache.rocketmq</groupId>
36+
<artifactId>rocketmq-client</artifactId>
37+
<version>4.1.0-incubating</version>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.apache.rocketmq</groupId>
42+
<artifactId>rocketmq-common</artifactId>
43+
<version>4.1.0-incubating</version>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-test</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
52+
</dependencies>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-maven-plugin</artifactId>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
63+
64+
</project>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package io.ymq.example.rocketmq;
2+
3+
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
4+
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
5+
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
6+
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
7+
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
8+
import org.apache.rocketmq.common.message.MessageExt;
9+
import org.springframework.beans.factory.annotation.Value;
10+
import org.springframework.stereotype.Component;
11+
12+
import javax.annotation.PostConstruct;
13+
import java.util.List;
14+
15+
/**
16+
* 描述: 消息消费者
17+
*
18+
* @author yanpenglei
19+
* @create 2018-02-01 18:11
20+
**/
21+
@Component
22+
public class Consumer {
23+
24+
/**
25+
* 消费者的组名
26+
*/
27+
@Value("${apache.rocketmq.consumer.PushConsumer}")
28+
private String consumerGroup;
29+
30+
/**
31+
* NameServer地址
32+
*/
33+
@Value("${apache.rocketmq.namesrvAddr}")
34+
private String namesrvAddr;
35+
36+
@PostConstruct
37+
public void defaultMQPushConsumer() {
38+
39+
//消费者的组名
40+
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(consumerGroup);
41+
42+
//指定NameServer地址,多个地址以 ; 隔开
43+
consumer.setNamesrvAddr(namesrvAddr);
44+
try {
45+
//订阅PushTopic下Tag为push的消息
46+
consumer.subscribe("PushTopic", "push");
47+
48+
//设置Consumer第一次启动是从队列头部开始消费还是队列尾部开始消费
49+
//如果非第一次启动,那么按照上次消费的位置继续消费
50+
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
51+
consumer.registerMessageListener(new MessageListenerConcurrently() {
52+
53+
@Override
54+
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext context) {
55+
try {
56+
for (MessageExt messageExt : list) {
57+
58+
System.out.println("messageExt: " + messageExt);//输出消息内容
59+
60+
String messageBody = new String(messageExt.getBody(), "utf-8");
61+
62+
System.out.println("消费响应:Msg: " + messageExt.getMsgId() + ",msgBody: " + messageBody);//输出消息内容
63+
64+
}
65+
} catch (Exception e) {
66+
e.printStackTrace();
67+
return ConsumeConcurrentlyStatus.RECONSUME_LATER; //稍后再试
68+
}
69+
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; //消费成功
70+
}
71+
72+
73+
});
74+
consumer.start();
75+
} catch (Exception e) {
76+
e.printStackTrace();
77+
}
78+
}
79+
80+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package io.ymq.example.rocketmq;
2+
3+
import org.apache.rocketmq.client.producer.DefaultMQProducer;
4+
import org.apache.rocketmq.client.producer.SendResult;
5+
import org.apache.rocketmq.common.message.Message;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.stereotype.Component;
8+
9+
import javax.annotation.PostConstruct;
10+
11+
/**
12+
* 描述: 消息生产者
13+
*
14+
* @author yanpenglei
15+
* @create 2018-02-01 18:09
16+
**/
17+
@Component
18+
public class Producer {
19+
20+
/**
21+
* 生产者的组名
22+
*/
23+
@Value("${apache.rocketmq.producer.producerGroup}")
24+
private String producerGroup;
25+
26+
/**
27+
* NameServer 地址
28+
*/
29+
@Value("${apache.rocketmq.namesrvAddr}")
30+
private String namesrvAddr;
31+
32+
@PostConstruct
33+
public void defaultMQProducer() {
34+
35+
//生产者的组名
36+
DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
37+
38+
//指定NameServer地址,多个地址以 ; 隔开
39+
producer.setNamesrvAddr(namesrvAddr);
40+
41+
try {
42+
43+
/**
44+
* Producer对象在使用之前必须要调用start初始化,初始化一次即可
45+
* 注意:切记不可以在每次发送消息时,都调用start方法
46+
*/
47+
producer.start();
48+
49+
for (int i = 0; i < 100; i++) {
50+
51+
String messageBody = "我是消息内容:" + i;
52+
53+
String message = new String(messageBody.getBytes(), "utf-8");
54+
55+
//构建消息
56+
Message msg = new Message("PushTopic" /* PushTopic */, "push"/* Tag */, "key_" + i /* Keys */, message.getBytes());
57+
58+
//发送消息
59+
SendResult result = producer.send(msg);
60+
61+
System.out.println("发送响应:MsgId:" + result.getMsgId() + ",发送状态:" + result.getSendStatus());
62+
63+
}
64+
65+
} catch (Exception e) {
66+
e.printStackTrace();
67+
} finally {
68+
producer.shutdown();
69+
}
70+
71+
}
72+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.ymq.example.rocketmq;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringBootRocketmqApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringBootRocketmqApplication.class, args);
11+
}
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
# 消费者的组名
3+
apache.rocketmq.consumer.PushConsumer=PushConsumer
4+
5+
# 生产者的组名
6+
apache.rocketmq.producer.producerGroup=Producer
7+
8+
# NameServer地址
9+
apache.rocketmq.namesrvAddr=192.168.252.121:9876
10+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.ymq.example.rocketmq;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.boot.test.context.SpringBootTest;
6+
import org.springframework.test.context.junit4.SpringRunner;
7+
8+
@RunWith(SpringRunner.class)
9+
@SpringBootTest
10+
public class SpringBootRocketmqApplicationTests {
11+
12+
@Test
13+
public void contextLoads() {
14+
}
15+
16+
}

0 commit comments

Comments
 (0)