增加 activemq 示例

This commit is contained in:
YunaiV
2019-12-15 16:46:54 +08:00
parent 66c2c17147
commit 0f1dcfa82f
10 changed files with 186 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
<?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-32-activemq-demo-delay</artifactId>
<dependencies>
<!-- 实现对 ActiveMQ 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!-- 方便等会写单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,13 @@
package cn.iocoder.springboot.lab32.activemqdemo;
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);
}
}

View File

@@ -0,0 +1,19 @@
package cn.iocoder.springboot.lab32.activemqdemo.consumer;
import cn.iocoder.springboot.lab32.activemqdemo.message.Demo02Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class Demo02Consumer {
private Logger logger = LoggerFactory.getLogger(getClass());
@JmsListener(destination = Demo02Message.QUEUE)
public void onMessage(Demo02Message message) {
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
}
}

View File

@@ -0,0 +1,30 @@
package cn.iocoder.springboot.lab32.activemqdemo.message;
import java.io.Serializable;
public class Demo02Message implements Serializable {
public static final String QUEUE = "QUEUE_DEMO_02";
/**
* 编号
*/
private Integer id;
public Demo02Message setId(Integer id) {
this.id = id;
return this;
}
public Integer getId() {
return id;
}
@Override
public String toString() {
return "Demo01Message{" +
"id=" + id +
'}';
}
}

View File

@@ -0,0 +1,32 @@
package cn.iocoder.springboot.lab32.activemqdemo.producer;
import cn.iocoder.springboot.lab32.activemqdemo.message.Demo02Message;
import org.apache.activemq.ScheduledMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class Demo02Producer {
@Autowired
private JmsMessagingTemplate jmsTemplate;
public void syncSend(Integer id, Integer delay) {
// 创建 ClusteringMessage 消息
Demo02Message message = new Demo02Message();
message.setId(id);
// 创建 Header
Map<String, Object> headers = null;
if (delay != null && delay > 0) {
headers = new HashMap<>();
headers.put(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);
}
// 同步发送消息
jmsTemplate.convertAndSend(Demo02Message.QUEUE, message, headers);
}
}

View File

@@ -0,0 +1,8 @@
spring:
# ActiveMQ 配置项,对应 ActiveMQProperties 配置类
activemq:
broker-url: tcp://127.0.0.1:61616 # RabbitMQ Broker 的地址
user: admin # 账号
password: admin # 密码
packages:
trust-all: true # 可信任的反序列化包

View File

@@ -0,0 +1 @@
package cn.iocoder.springboot.lab32.activemqdemo;

View File

@@ -0,0 +1,44 @@
package cn.iocoder.springboot.lab32.activemqdemo.producer;
import cn.iocoder.springboot.lab32.activemqdemo.Application;
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 Demo02ProducerTest {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private Demo02Producer producer;
@Test
public void testSyncSend01() throws InterruptedException {
// 不设置消息的过期时间
this.testSyncSendDelay(null);
}
@Test
public void testSyncSend02() throws InterruptedException {
// 设置发送消息的过期时间为 5000 毫秒
this.testSyncSendDelay(5000);
}
private void testSyncSendDelay(Integer delay) throws InterruptedException {
int id = (int) (System.currentTimeMillis() / 1000);
producer.syncSend(id, delay);
logger.info("[testSyncSendDelay][发送编号:[{}] 发送成功]", id);
// 阻塞等待,保证消费
new CountDownLatch(1).await();
}
}

View File

@@ -0,0 +1,8 @@
spring:
# ActiveMQ 配置项,对应 ActiveMQProperties 配置类
activemq:
broker-url: tcp://127.0.0.1:61616 # RabbitMQ Broker 的地址
user: admin # 账号
password: admin # 密码
packages:
trust-all: true # 可信任的反序列化包

View File

@@ -15,6 +15,7 @@
<module>lab-32-activemq-native</module>
<module>lab-32-activemq-demo</module>
<module>lab-32-activemq-demo-message-model</module>
<module>lab-32-activemq-demo-delay</module>
</modules>