2.3 RabbitMQ高级特效-自定义消费者

之前的demo都是通过QueueingConsumer对象来消费消息,可以通过继承DefaultCunsumer类来处理消息。

自定义消费者代码

package com.star.movie.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;

/**
 * @Description:
 * @author:kaili
 * @Date: 2019-04-22 18:05
 **/
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.err.println("-----------consume message----------");
        System.err.println("consumerTag: " + consumerTag);
        System.err.println("envelope: " + envelope);
        System.err.println("properties: " + properties);
        System.err.println("body: " + new String(body));
    }
}

通过handleDelivery方法来处理消息,打印该方法的参数看都是那些信息。

消费者代码

package com.star.movie.consumer;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.star.movie.common.Constant;

/**
 * @Description:自定义消费者
 * @author:kaili
 * @Date: 2019-04-22 18:03
 **/
public class ExtendConsumer {
    public static void main(String[] args) throws Exception{
        Connection connection = Constant.getConnection();
        Channel channel = connection.createChannel();

        String exchangeName = "test_consumer_exchange";
        String routingKey = "consumer.#";
        String queueName = "test_consumer_queue";

        channel.exchangeDeclare(exchangeName, "topic", true, false, null);
        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, routingKey);

        channel.basicConsume(queueName,true,new MyConsumer(channel));
    }
}

生产者代码

package com.star.movie.consumer;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.star.movie.common.Constant;

/**
 * @Description:
 * @author:kaili
 * @Date: 2019-04-22 18:03
 **/
public class ConsumerProducer {
    public static void main(String[] args) throws Exception{
        Connection connection = Constant.getConnection();
        Channel channel = connection.createChannel();

        String exchange = "test_consumer_exchange";
        String routingKey = "consumer.save";

        String msg = "Hello RabbitMQ Consumer Message";

        for(int i =0; i<5; i ++){
            channel.basicPublish(exchange, routingKey, true, null, msg.getBytes());
        }
    }
}

step 1 启动消费者代码
生成类型为topic的交换机test_consumer_exchange,绑定test_consumer_queue队列,绑定健为consumer.#
在这里插入图片描述
step 2 启动生产者代码
观察消费者端控制台打印的信息

Connected to the target VM, address: '127.0.0.1:56342', transport: 'socket'
-----------consume message----------
consumerTag: amq.ctag-odY05-Srxf-XuSTO1TNsDg
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-odY05-Srxf-XuSTO1TNsDg
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
-----------consume message----------
consumerTag: amq.ctag-odY05-Srxf-XuSTO1TNsDg
envelope: Envelope(deliveryTag=3, 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-odY05-Srxf-XuSTO1TNsDg
envelope: Envelope(deliveryTag=4, 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-odY05-Srxf-XuSTO1TNsDg
envelope: Envelope(deliveryTag=5, 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

生产者端投递的5条消息都在日志中,在envelope中的deliveryTag标识消息记录,
body是消息的实体类容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值