增加 activemq 示例

This commit is contained in:
YunaiV
2019-12-15 18:41:13 +08:00
parent c9f864c813
commit 69ccbe1787
13 changed files with 171 additions and 3 deletions

View File

@@ -12,7 +12,7 @@ public class Demo03Consumer {
private Logger logger = LoggerFactory.getLogger(getClass());
@JmsListener(destination = Demo03Message.QUEUE,
concurrency = "10")
concurrency = "2")
public void onMessage(Demo03Message message) {
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
}

View File

@@ -22,7 +22,7 @@ public class Demo03Message implements Serializable {
@Override
public String toString() {
return "Demo01Message{" +
return "Demo03Message{" +
"id=" + id +
'}';
}

View File

@@ -22,7 +22,7 @@ public class Demo02Message implements Serializable {
@Override
public String toString() {
return "Demo01Message{" +
return "Demo02Message{" +
"id=" + id +
'}';
}

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-orderly</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,20 @@
package cn.iocoder.springboot.lab32.activemqdemo.consumer;
import cn.iocoder.springboot.lab32.activemqdemo.message.Demo4Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class Demo04Consumer {
private Logger logger = LoggerFactory.getLogger(getClass());
@JmsListener(destination = Demo4Message.QUEUE,
concurrency = "2")
public void onMessage(Demo4Message 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 Demo4Message implements Serializable {
public static final String QUEUE = "QUEUE_DEMO_04";
/**
* 编号
*/
private Integer id;
public Demo4Message setId(Integer id) {
this.id = id;
return this;
}
public Integer getId() {
return id;
}
@Override
public String toString() {
return "Demo04Message{" +
"id=" + id +
'}';
}
}

View File

@@ -0,0 +1,22 @@
package cn.iocoder.springboot.lab32.activemqdemo.producer;
import cn.iocoder.springboot.lab32.activemqdemo.message.Demo4Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
@Component
public class Demo03Producer {
@Autowired
private JmsMessagingTemplate jmsTemplate;
public void syncSend(Integer id) {
// 创建 Demo03Message 消息
Demo4Message message = new Demo4Message();
message.setId(id);
// 同步发送消息
jmsTemplate.convertAndSend(Demo4Message.QUEUE, message);
}
}

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,35 @@
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 Demo03ProducerTest {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private Demo03Producer producer;
@Test
public void testSyncSend() throws InterruptedException {
for (int i = 0; i < 10; i++) {
int id = (int) (System.currentTimeMillis() / 1000);
producer.syncSend(id);
// logger.info("[testSyncSend][发送编号:[{}] 发送成功]", 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

@@ -17,6 +17,7 @@
<module>lab-32-activemq-demo-message-model</module>
<module>lab-32-activemq-demo-delay</module>
<module>lab-32-activemq-demo-concurrency</module>
<module>lab-32-activemq-demo-orderly</module>
</modules>