美文网首页
RabbitMQ自定义消费者使用

RabbitMQ自定义消费者使用

作者: 若兮缘 | 来源:发表于2019-03-19 08:38 被阅读0次
消费端自定义监听
  • 我们之前呢都是在代码中编写while循环,进行 consumer.nextDelivery 方法进行获取下一条消息,然后进行消费处理!
  • 其实我们还可以使用自定义的Consumer,它更加的方便,解耦性更加的强,也是在实际工作中最常用的使用方式!
  • 自定义消费端实现只需要继承 DefaultConsumer 类,重写 handleDelivery 方法即可
自定义消费端演示
生产端
public class Producer {
    
    public static void main(String[] args) throws Exception {
        //1 创建ConnectionFactory
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.43.157");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        //2 获取Connection
        Connection connection = connectionFactory.newConnection();
        //3 通过Connection创建一个新的Channel
        Channel channel = connection.createChannel();
        
        String exchange = "test_consumer_exchange";
        String routingKey = "consumer.save";
        
        String msg = "Hello RabbitMQ Consumer Message";
        //4 发送消息
        for(int i =0; i<5; i ++){
            channel.basicPublish(exchange, routingKey, true, null, msg.getBytes());
        }
    }
}
自定义消费者
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 {
        //consumerTag: 内部生成的消费标签  properties: 消息属性  body: 消息内容  
        System.err.println("-----------consume message----------");
        System.err.println("consumerTag: " + consumerTag);
        //envelope包含属性:deliveryTag(标签), redeliver, exchange, routingKey
        //redeliver是一个标记,如果设为true,表示消息之前可能已经投递过了,现在是重新投递消息到监听队列的消费者
        System.err.println("envelope: " + envelope);
        System.err.println("properties: " + properties);
        System.err.println("body: " + new String(body));
    }
}
消费端
public class Consumer {

    public static void main(String[] args) throws Exception {
        //1 创建ConnectionFactory
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.43.157");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        //2 获取Connection
        Connection connection = connectionFactory.newConnection();
        //3 通过Connection创建一个新的Channel
        Channel channel = connection.createChannel();
        
        String exchangeName = "test_consumer_exchange";
        String routingKey = "consumer.#";
        String queueName = "test_consumer_queue";
        //4 声明交换机和队列,然后进行绑定设置路由Key
        channel.exchangeDeclare(exchangeName, "topic", true, false, null);
        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, routingKey);
        
        //5 设置channel,使用自定义消费者
        channel.basicConsume(queueName, true, new MyConsumer(channel));
    }
}
运行说明

先启动消费端,访问管控台:http://192.168.43.157:15672,检查Exchange和Queue是否设置OK,然后启动生产端。消费端打印内容如下

-----------consume message----------
consumerTag: amq.ctag-SeOnTFp7GPkfrYTmB-FIUA
envelope: Envelope(deliveryTag=1, redeliver=false, exchange=test_consumer_exchange, routingKey=consumer.save)
properties: #contentHeader<basic>(content-type=null, content-encoding=null, headers=null, delivery-mode=null, priority=null, correlation-id=null, reply-to=null, expiration=null, message-id=null, timestamp=null, type=null, user-id=null, app-id=null, cluster-id=null)
body: Hello RabbitMQ Consumer Message
-----------consume message----------
consumerTag: amq.ctag-SeOnTFp7GPkfrYTmB-FIUA
envelope: Envelope(deliveryTag=2, redeliver=false, exchange=test_consumer_exchange, routingKey=consumer.save)
properties: #contentHeader<basic>(content-type=null, content-encoding=null, headers=null, delivery-mode=null, priority=null, correlation-id=null, reply-to=null, expiration=null, message-id=null, timestamp=null, type=null, user-id=null, app-id=null, cluster-id=null)
body: Hello RabbitMQ Consumer Message

可以看到,在 handleDelivery 方法内我们基本可以获取到消息的全部重要内容,以上就是自定义消费者的实现。

相关文章

网友评论

      本文标题:RabbitMQ自定义消费者使用

      本文链接:https://www.haomeiwen.com/subject/gxpmmqtx.html