RabbitMQ 消费端自定义监听

消费端自定义监听
创建自定义监听类 MyConsumer,继承 DefaultConsumer

生产者代码:

package com.example.rabbitmq.consumer;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 生产者代码
 */
public class producer {

    public static void main(String[] args) throws IOException, TimeoutException {

        //1,创建工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        //2, 获取连接
        Connection connection = connectionFactory.newConnection();
        //3, 通过connection创建一个channel
        Channel channel = connection.createChannel();

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

        //5, 发送消息
        String msg = "hello rabbitmq send retuen message!";
        for (int i = 0; i < 5; i++) {
            channel.basicPublish(exchangeName, routingKey, true,null, msg.getBytes());
        }
//        channel.close();
//        connection.close();

    }
}

消费者代码:

package com.example.rabbitmq.consumer;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 消费者
 */
public class Consumer {

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        //1,创建工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        //2,获取连接
        Connection connection = connectionFactory.newConnection();
        //3,通过connection创建一个channel
        Channel channel = connection.createChannel();
        //4,exchange, routingkey, queue, 并将queue和交换机绑定
        //设置交换机
        String exchangeName = "test_consumer_exchange";
        String routingKey = "consumer.#";
        String queueName = "test_consumer_queue";

        //声明一个交换机
        channel.exchangeDeclare(exchangeName, "topic", true);
        //声明一个队列
        channel.queueDeclare(queueName, true, false, false, null);
        //建立关系
        channel.queueBind(queueName, exchangeName, routingKey);
        //5,创建消费者//
        channel.basicConsume(queueName, true, new MyConsumer(channel));

    }
}

自定义消费者监听类:

package com.example.rabbitmq.consumer;
import com.rabbitmq.client.*;
import com.rabbitmq.client.Consumer;

/**
 * 自定义消费者监听类
 */
public class MyConsumer extends DefaultConsumer {

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

    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties basicProperties,  byte[] body){
        System.out.println("----- consumer message -----");
        System.out.println("consumerTag = " + consumerTag);
        System.out.println("envelope = " + envelope);
        System.out.println("basicProperties = " + basicProperties);
        System.out.println("body = " + new String(body));
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值