Springboot整合rabbitmq实现消息的发送接收

1、需要引入spring-boot-starter-amqp:

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-amqp</artifactId>

</dependency>

2、进行相关的配置:

RabbitMQ配置

spring.rabbitmq.host=localhost

spring.rabbitmq.port=5672

虚拟主机配置

spring.rabbitmq.virtual-host=/

开启发送端消息抵达Broker确认

spring.rabbitmq.publisher-confirms=true

开启发送端消息抵达Queue确认

spring.rabbitmq.publisher-returns=true

只要消息抵达Queue,就会异步发送优先回调returnfirm

spring.rabbitmq.template.mandatory=true

手动ack消息,不使用默认的消费端确认

spring.rabbitmq.listener.simple.acknowledge-mode=manual

3、使用,4个重要的注解:

(1)@AmqpAdmin:管理组件,可以用来声明交换机、队列、以及绑定交接机和队列

(2)RabbitTemplate:消息发送处理组件

(3)@RabbitListener 监听消息的方法可以有三种参数(不分数量,顺序):Object content, Message message, Channel channel ,可用于类上和方法上。

(4)@RabbitHandler 监听消息,用于方法上。

使用示例:

java 复制代码
public class GulimallOrderApplicationTests {

    public static final String EXCHANGE="hello-java-exchange";
    public static final String QUEUE="hello-java-queue";
    public static final String ROUTINGKEY="hello-java";

     @Autowired
     private AmqpAdmin amqpAdmin;
     @Autowired
     private RabbitTemplate rabbitTemplate;

     //发送字符串对象消息
     @Test
     public void  sengMessageStrTest(){
         String message="Hello,RabbitMQ";
         rabbitTemplate.convertAndSend(EXCHANGE,ROUTINGKEY,message);
         log.info("消息:{}发送完成",message);
     }

    //发送对象消息
    @Test
    public void  sengMessageTest(){
        OrderReturnReasonEntity returnReasonEntity = new OrderReturnReasonEntity();
        returnReasonEntity.setName("hahaha");
        returnReasonEntity.setId(1L);
        returnReasonEntity.setCreateTime(new Date());
        rabbitTemplate.convertAndSend(EXCHANGE,ROUTINGKEY,returnReasonEntity);
        log.info("消息:{}发送完成",returnReasonEntity);
    }

    @Test
    public void  sengMessageReceiveOnly(){
        for (int i = 0; i < 10; i++) {
            if(i%2==0){
                OrderReturnReasonEntity returnReasonEntity = new OrderReturnReasonEntity();
                returnReasonEntity.setName("hahaha:"+i);
                returnReasonEntity.setId(1L);
                returnReasonEntity.setCreateTime(new Date());
                rabbitTemplate.convertAndSend(EXCHANGE,ROUTINGKEY,returnReasonEntity);
                log.info("消息:{}发送完成",returnReasonEntity);
            }else{
                OrderEntity order=new OrderEntity();
                order.setOrderSn(UUID.randomUUID().toString());
                rabbitTemplate.convertAndSend(EXCHANGE,ROUTINGKEY,order);
                log.info("消息:{}发送完成",order);
            }

        }

    }

    @Test
    void createExchange() {
        DirectExchange directExchange = new DirectExchange(EXCHANGE, true, false);
        amqpAdmin.declareExchange(directExchange);
        log.info("exchange:{}创建成功",directExchange.getName());

    }

    @Test
    void createQueue() {
        Queue queue = new Queue(QUEUE, true, false,false);
        amqpAdmin.declareQueue(queue);
        log.info("队列:{}创建成功",queue.getName());
    }

    @Test
    void createBinding(){
        Binding binding = new Binding(QUEUE, Binding.DestinationType.QUEUE, EXCHANGE,ROUTINGKEY , null);
        amqpAdmin.declareBinding(binding);
        log.info("创建绑定成功");
    }

}

监听接收消息:

java 复制代码
@Service("orderService")
@RabbitListener(queues = {"hello-java-queue"})
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {


    @RabbitHandler
    public void receiveMessage(Message message, OrderReturnReasonEntity content, Channel channel) throws InterruptedException {
        //接收到的消息体
        byte[] body = message.getBody();
        //MessageProperties properties = message.getMessageProperties();
        System.out.println("接收到的消息:"+content);
        //Thread.sleep(3000);
        System.out.println("消息处理完成:"+content.getName());
    }

    @RabbitHandler
    public void receiveMessage2(OrderEntity content) throws InterruptedException {

        System.out.println("接收到的消息:"+content);
    }

}

@RabbitListener 的@RabbitHandler可以组合使用,@RabbitListener用于类上,@RabbitHandler用于方法上,用于区分接收到不同消息或者不同队列的消息。

相关推荐
像少年啦飞驰点、2 分钟前
零基础入门 Spring Boot:从“Hello World”到可上线的 Web 应用(附完整实操指南)
spring boot·编程入门·后端开发·java web·零基础教程
她说..6 分钟前
验签实现方案整理(签名验证+防篡改+防重放)
java·经验分享·spring boot·java-ee·bladex
爱吃山竹的大肚肚11 分钟前
异步导出方案
java·spring boot·后端·spring·中间件
没有bug.的程序员18 分钟前
Spring Boot 与 Redis:缓存穿透/击穿/雪崩的终极攻防实战指南
java·spring boot·redis·缓存·缓存穿透·缓存击穿·缓存雪崩
草履虫建模18 分钟前
Java 基础到进阶|专栏导航:路线图 + 目录(持续更新)
java·开发语言·spring boot·spring cloud·maven·基础·进阶
福赖29 分钟前
《微服务即使通讯中RabbitMQ的作用》
c++·微服务·架构·rabbitmq
岁岁种桃花儿44 分钟前
构建SpringBoot项目Docker镜像并发布到k8s集群中进行运行
spring boot·docker·kubernetes
九转苍翎1 小时前
掌控消息全链路(3)——RabbitMQ/Spring-AMQP高级特性详解之TTL、死信和延迟
spring boot·java-rabbitmq
sheji34161 小时前
【开题答辩全过程】以 基于Spring Boot的化妆品销售系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
VX:Fegn08959 小时前
计算机毕业设计|基于ssm + vue超市管理系统(源码+数据库+文档)
前端·数据库·vue.js·spring boot·后端·课程设计