增加 activemq 示例

This commit is contained in:
YunaiV
2019-12-15 22:33:35 +08:00
parent b175745200
commit e6723f16e8
12 changed files with 185 additions and 2 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-consume-retry</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,15 @@
package cn.iocoder.springboot.lab32.activemqdemo.config;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQConnectionFactoryCustomizer;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ActiveMQConnectionFactoryCustomizerImpl implements ActiveMQConnectionFactoryCustomizer {
@Override
public void customize(ActiveMQConnectionFactory factory) {
System.out.println();
}
}

View File

@@ -0,0 +1,21 @@
package cn.iocoder.springboot.lab32.activemqdemo.consumer;
import cn.iocoder.springboot.lab32.activemqdemo.message.Demo05Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class Demo01Consumer {
private Logger logger = LoggerFactory.getLogger(getClass());
@JmsListener(destination = Demo05Message.QUEUE)
public void onMessage(Demo05Message message) {
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
// <X> 注意,此处抛出一个 RuntimeException 异常,模拟消费失败
throw new RuntimeException("我就是故意抛出一个异常");
}
}

View File

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

View File

@@ -0,0 +1,22 @@
package cn.iocoder.springboot.lab32.activemqdemo.producer;
import cn.iocoder.springboot.lab32.activemqdemo.message.Demo05Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
@Component
public class Demo01Producer {
@Autowired
private JmsMessagingTemplate jmsTemplate;
public void syncSend(Integer id) {
// 创建 ClusteringMessage 消息
Demo05Message message = new Demo05Message();
message.setId(id);
// 同步发送消息
jmsTemplate.convertAndSend(Demo05Message.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,34 @@
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 Demo01ProducerTest {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private Demo01Producer producer;
@Test
public void testSyncSend() throws InterruptedException {
// 发送消息
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

@@ -11,14 +11,14 @@ import javax.annotation.Resource;
public class ClusteringProducer {
@Resource(name = ActiveMQConfig.CLUSTERING_JMS_TEMPLATE_BEAN_NAME)
private JmsMessagingTemplate rabbitTemplate;
private JmsMessagingTemplate jmsTemplate;
public void syncSend(Integer id) {
// 创建 ClusteringMessage 消息
ClusteringMessage message = new ClusteringMessage();
message.setId(id);
// 同步发送消息
rabbitTemplate.convertAndSend(ClusteringMessage.QUEUE, message);
jmsTemplate.convertAndSend(ClusteringMessage.QUEUE, message);
}
}

View File

@@ -18,6 +18,7 @@
<module>lab-32-activemq-demo-delay</module>
<module>lab-32-activemq-demo-concurrency</module>
<module>lab-32-activemq-demo-orderly</module>
<module>lab-32-activemq-demo-consume-retry</module>
</modules>