mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 spring boot rocketmq 示例
This commit is contained in:
31
lab-31/lab-31-rocketmq-ons/pom.xml
Normal file
31
lab-31/lab-31-rocketmq-ons/pom.xml
Normal file
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.1.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-31-rocketmq-ons</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 实现对 RocketMQ 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.rocketmq</groupId>
|
||||
<artifactId>rocketmq-spring-boot-starter</artifactId>
|
||||
<version>2.0.4</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 方便等会写单元测试 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springboot.lab31.rocketmqdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.iocoder.springboot.lab31.rocketmqdemo.consumer;
|
||||
|
||||
import cn.iocoder.springboot.lab31.rocketmqdemo.message.Demo01Message;
|
||||
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
||||
import org.apache.rocketmq.spring.core.RocketMQListener;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@RocketMQMessageListener(
|
||||
topic = Demo01Message.TOPIC,
|
||||
consumerGroup = "GID_CONSUMER_GROUP_YUNAI_TEST"
|
||||
)
|
||||
public class Demo01Consumer implements RocketMQListener<Demo01Message> {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Override
|
||||
public void onMessage(Demo01Message message) {
|
||||
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package cn.iocoder.springboot.lab31.rocketmqdemo.message;
|
||||
|
||||
/**
|
||||
* 示例 01 的 Message 消息
|
||||
*/
|
||||
public class Demo01Message {
|
||||
|
||||
public static final String TOPIC = "TOPIC_YUNAI_TEST";
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
private Integer id;
|
||||
|
||||
public Demo01Message setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Demo01Message{" +
|
||||
"id=" + id +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package cn.iocoder.springboot.lab31.rocketmqdemo.producer;
|
||||
|
||||
import cn.iocoder.springboot.lab31.rocketmqdemo.message.Demo01Message;
|
||||
import org.apache.rocketmq.client.producer.SendCallback;
|
||||
import org.apache.rocketmq.client.producer.SendResult;
|
||||
import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Demo01Producer {
|
||||
|
||||
@Autowired
|
||||
private RocketMQTemplate rocketMQTemplate;
|
||||
|
||||
public SendResult syncSend(Integer id) {
|
||||
// 创建 Demo01Message 消息
|
||||
Demo01Message message = new Demo01Message();
|
||||
message.setId(id);
|
||||
// 同步发送消息
|
||||
return rocketMQTemplate.syncSend(Demo01Message.TOPIC, message);
|
||||
}
|
||||
|
||||
public void asyncSend(Integer id, SendCallback callback) {
|
||||
// 创建 Demo01Message 消息
|
||||
Demo01Message message = new Demo01Message();
|
||||
message.setId(id);
|
||||
// 异步发送消息
|
||||
rocketMQTemplate.asyncSend(Demo01Message.TOPIC, message, callback);
|
||||
}
|
||||
|
||||
public void onewaySend(Integer id) {
|
||||
// 创建 Demo01Message 消息
|
||||
Demo01Message message = new Demo01Message();
|
||||
message.setId(id);
|
||||
// oneway 发送消息
|
||||
rocketMQTemplate.sendOneWay(Demo01Message.TOPIC, message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
# rocketmq 配置项,对应 RocketMQProperties 配置类
|
||||
rocketmq:
|
||||
name-server: http://onsaddr.mq-internet-access.mq-internet.aliyuncs.com:80 # 阿里云 RocketMQ Namesrv
|
||||
access-channel: CLOUD # 设置使用阿里云
|
||||
# Producer 配置项
|
||||
producer:
|
||||
group: GID_PRODUCER_GROUP_YUNAI_TEST # 生产者分组
|
||||
access-key: # 设置阿里云的 RocketMQ 的 access key !!!这里涉及到隐私,所以这里艿艿没有提供
|
||||
secret-key: # 设置阿里云的 RocketMQ 的 secret key !!!这里涉及到隐私,所以这里艿艿没有提供
|
||||
@@ -0,0 +1 @@
|
||||
package cn.iocoder.springboot.lab31.rocketmqdemo;
|
||||
@@ -0,0 +1,66 @@
|
||||
package cn.iocoder.springboot.lab31.rocketmqdemo.producer;
|
||||
|
||||
import cn.iocoder.springboot.lab31.rocketmqdemo.Application;
|
||||
import org.apache.rocketmq.client.producer.SendCallback;
|
||||
import org.apache.rocketmq.client.producer.SendResult;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class Demo01ProducerTest {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Autowired
|
||||
private Demo01Producer producer;
|
||||
|
||||
@Test
|
||||
public void testSyncSend() throws InterruptedException {
|
||||
int id = (int) (System.currentTimeMillis() / 1000);
|
||||
SendResult result = producer.syncSend(id);
|
||||
logger.info("[testSyncSend][发送编号:[{}] 发送结果:[{}]]", id, result);
|
||||
|
||||
// 阻塞等待,保证消费
|
||||
new CountDownLatch(1).await();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testASyncSend() throws InterruptedException {
|
||||
int id = (int) (System.currentTimeMillis() / 1000);
|
||||
producer.asyncSend(id, new SendCallback() {
|
||||
|
||||
@Override
|
||||
public void onSuccess(SendResult result) {
|
||||
logger.info("[testASyncSend][发送编号:[{}] 发送成功,结果为:[{}]]", id, result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onException(Throwable e) {
|
||||
logger.info("[testASyncSend][发送编号:[{}] 发送异常]]", id, e);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
// 阻塞等待,保证消费
|
||||
new CountDownLatch(1).await();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnewaySend() throws InterruptedException {
|
||||
int id = (int) (System.currentTimeMillis() / 1000);
|
||||
producer.onewaySend(id);
|
||||
logger.info("[testOnewaySend][发送编号:[{}] 发送完成]", id);
|
||||
|
||||
// 阻塞等待,保证消费
|
||||
new CountDownLatch(1).await();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,6 +13,7 @@
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>lab-31-rocketmq-demo</module>
|
||||
<module>lab-31-rocketmq-ons</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user