RabbitMQ高级特性-消费端自定义监听

消费端自定义监听

  • 在之前的代码演示中, consumer进行消费时 ,都是使用while循环进行消息消费, 然后使用consumer.nextDelivery()方法获取下一条消息
  • 但是在实际工作中, 使用自定义的Consumer更加的方便, 解耦性也更加的强, 实现自定义的Consumer可以实现Consumer接口, 或者更常用的是继承默认的DefaultConsumer

代码演示

自定义消费者(替换QueueingConsumer)

package com.qiyexue.api.consumer;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;

/**
 * 自定义消费者
 * @author 七夜雪
 * @create 2018-12-16 8:20
 */
public class MyConsumer extends DefaultConsumer {
   

    public MyConsumer(Channel channel) {
   
        super(channel);
    }

    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
   
        System.out.println("-------------自定义消费者------------");
        System.out.println(

你可能感兴趣的:(中间件,分布式,RabbitMQ,消息队列,RabbitMQ,RabbitMQ高级特性,消费端自定义监听)