SpringBoot学习-part62 @RabbitListener & @EnableRabbit

开启基于注解的RabbitMQ模式

@EnableRabbit // 开启基于注解的RabbitMQ模式
@SpringBootApplication
public class Springboot02AmqpApplication {

    public static void main(String[] args)
    {
        SpringApplication.run(Springboot02AmqpApplication.class, args);
    }

}

监听队列消息

@Service
public class CarService {

    //只要这个消息队列有内容进来,这个方法就会被调用
    @RabbitListener(queues = {"ezerbel.news"})
    public void receive(Car car){
        System.out.println("ezerbel.news 队列中有消息进来了! car == " + car);
    }

    @RabbitListener(queues = {"lilith.news"})
    public void receiveMsg(Message msg){
        System.out.println("lilith.news 队列中有消息进来了! msg.prop == " + msg.getMessageProperties()
        +" msg.body == " + msg.getBody());
    }
}

发布消息

    @Test
    public void testPubBroadCast()
    {
        Car car = new Car("Losless",3050000);
        rabbitTemplate.convertAndSend("exchange.fanout","",car);
    }

得到结果

lilith.news 队列中有消息进来了! msg.prop == MessageProperties [headers={__TypeId__=com.ezerbelcn.amqp.bean.Car}, contentType=application/json, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=exchange.fanout, receivedRoutingKey=, deliveryTag=1, consumerTag=amq.ctag-18MYR1bSu3W1UgLkzrPfRA, consumerQueue=lilith.news] msg.body == [B@2942f01e
ezerbel.news 队列中有消息进来了! car == Car{name='Losless', price=3050000}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值