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]
  • 相关阅读:
    Salt-ssh批量自动安装被控端salt-mini
    Saltstack配置管理
    gitlab的安装和基本维护
    Git的杀手级功能之 一 远程仓库
    分布式版本控制系统-git
    Linux查看服务器公网ip的方法
    vmware fusion 10序列号
    python3.6.4的importlib模块重载用法
    设置PyCharm中的Python代码模版
    MacOs执行SQL出错(mysql)
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12294724.html
Copyright © 2011-2022 走看看