zoukankan      html  css  js  c++  java
  • springboot消息之@RabbitListener和@EnableRabbit

    接上一节。

    使用上述两个注解可以实现监听消息队列。

    1、我们在主配置类中加上@EnableRabbit开启基于注解的RabbitMq

    2、我们建立一个BookService.java并为相关方法加上@RabbitListener注解,启动服务器:

    package com.gong.springbootrabbitmq.service;
    
    import com.gong.springbootrabbitmq.bean.Book;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Service;
    
    @Service
    public class BookService {
    
        @RabbitListener(queues = "gong.news")
        public void recieve(Book book){
            System.out.println("收到消息:"+book);
        }
    
    }

    服务器启动时我们就收到了消息,因为上一节我们往gong.news队列中添加了一条消息。

    我们如果继续添加一条消息:也收到了

    3、第二种收消息的模式

        @RabbitListener(queues = "gong")
        public void recieve2(Message message){
            System.out.println(message.getBody());
            System.out.println(message.getMessageProperties());
        }

    启动服务器:

    [B@3c45f2e3
    MessageProperties [headers={__TypeId__=com.gong.springbootrabbitmq.bean.Book}, contentType=application/json, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=exchange.fanout, receivedRoutingKey=, deliveryTag=1, consumerTag=amq.ctag-Y2Y5J6jRFYdOOzkrxz9VLA, consumerQueue=gong]
  • 相关阅读:
    vue学习(十四) 条件搜索框动态查询表中数据 数组的新方法
    vue学习(十三) 删除对象数组中的某个元素
    数据库管理
    PHP基础
    PHP基础之常量与变量
    Cobalt Strike简单使用
    phpstudy后门利用复现
    DNS劫持
    远程控制(远控Bin)
    php基础
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12294724.html
Copyright © 2011-2022 走看看