增加 spring boot rabbitMQ 用例
This commit is contained in:
39
spring-boot/spring-boot-rabbitmq/rabbitmq-consumer/pom.xml
Normal file
39
spring-boot/spring-boot-rabbitmq/rabbitmq-consumer/pom.xml
Normal file
@ -0,0 +1,39 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.heibaiying</groupId>
|
||||
<artifactId>spring-boot-rabbitmq</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>rabbitmq-consumer</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>rabbitmq-consumer</name>
|
||||
<description>RabbitMQ consumer project for Spring Boot</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.heibaiying</groupId>
|
||||
<artifactId>rabbitmq-common</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,14 @@
|
||||
package com.heibaiying.rabbitmqconsumer;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class RabbitmqConsumerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(RabbitmqConsumerApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,36 @@
|
||||
package com.heibaiying.rabbitmqconsumer.consumer;
|
||||
|
||||
|
||||
import com.heibaiying.bean.Programmer;
|
||||
import com.heibaiying.constant.RabbitBeanInfo;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.rabbit.annotation.*;
|
||||
import org.springframework.amqp.support.AmqpHeaders;
|
||||
import org.springframework.messaging.handler.annotation.Headers;
|
||||
import org.springframework.messaging.handler.annotation.Payload;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 消息是对象的消费者
|
||||
*/
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class RabbitmqBeanConsumer {
|
||||
|
||||
@RabbitListener(bindings = @QueueBinding(
|
||||
value = @Queue(value = RabbitBeanInfo.QUEUE_NAME, durable = RabbitBeanInfo.QUEUE_DURABLE),
|
||||
exchange = @Exchange(value = RabbitBeanInfo.EXCHANGE_NAME, type = RabbitBeanInfo.EXCHANGE_TYPE),
|
||||
key = RabbitBeanInfo.ROUTING_KEY)
|
||||
)
|
||||
@RabbitHandler
|
||||
public void onMessage(@Payload Programmer programmer, @Headers Map<String, Object> headers, Channel channel) throws Exception {
|
||||
log.info("programmer:{} ", programmer);
|
||||
Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
|
||||
channel.basicAck(deliveryTag, false);
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.heibaiying.rabbitmqconsumer.consumer;
|
||||
|
||||
|
||||
import com.heibaiying.constant.RabbitInfo;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.rabbit.annotation.*;
|
||||
import org.springframework.amqp.support.AmqpHeaders;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 消息消费者
|
||||
*/
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class RabbitmqConsumer {
|
||||
|
||||
@RabbitListener(bindings = @QueueBinding(
|
||||
value = @Queue(value = RabbitInfo.QUEUE_NAME, durable = RabbitInfo.QUEUE_DURABLE),
|
||||
exchange = @Exchange(value = RabbitInfo.EXCHANGE_NAME, type = RabbitInfo.EXCHANGE_TYPE),
|
||||
key = RabbitInfo.ROUTING_KEY)
|
||||
)
|
||||
@RabbitHandler
|
||||
public void onMessage(Message message, Channel channel) throws Exception {
|
||||
MessageHeaders headers = message.getHeaders();
|
||||
// 获取消息头信息和消息体
|
||||
log.info("msgInfo:{} ; payload:{} ",headers.get("msgInfo"),message.getPayload());
|
||||
// DELIVERY_TAG 是一个自增的数值
|
||||
Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
|
||||
// 第二个参数代表是否一次签收多条,当该参数为 true 时,则可以一次性确认 delivery_tag 小于等于传入值的所有消息
|
||||
channel.basicAck(deliveryTag, false);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
spring:
|
||||
rabbitmq:
|
||||
addresses: 192.168.0.108:5672
|
||||
# RabbitMQ 默认的用户名和密码都是guest 而虚拟主机名称是 "/"
|
||||
# 如果配置其他虚拟主机地址,需要预先用管控台或者图形界面创建 图形界面地址 http://主机地址:15672
|
||||
username: admin
|
||||
password: admin
|
||||
virtual-host: my_vhost
|
||||
listener:
|
||||
simple:
|
||||
# 签收模式设置为手工签收
|
||||
acknowledge-mode: manual
|
||||
# 侦听器调用者线程的最小数量
|
||||
concurrency: 10
|
||||
# 侦听器调用者线程的最大数量
|
||||
max-concurrency: 50
|
||||
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.rabbitmqconsumer;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class RabbitmqConsumerApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user