版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/love905661433/article/details/85256296
消费端自定义监听
- 在之前的代码演示中, 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("consumerTag : " + consumerTag);
System.out.println("envelope : " + envelope);
System.out.println("properties : " + properties);
System.out.println("body : " + new String(body));
}
}
Producer
package com.qiyexue.api.consumer;
import com.rabbitmq.client.*;
import java.io.IOException;
/**
* 生产者
*
* @author 七夜雪
* @create 2018-12-15 19:56
*/
public class Producer {
public static void main(String[] args) throws Exception {
// 1. 创建ConnectionFactory, 并设置属性
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.72.138");
factory.setPort(5672);
factory.setVirtualHost("/");
// 2. 创建连接
Connection connection = factory.newConnection();
// 3. 创建channel
Channel channel = connection.createChannel();
String exchangeName = "test_consumer_exchange";
String routingKey = "consumer.qiye";
// 发送消息
String msg = "自定义消费者, 消息发送 : Hello, 七夜雪";
channel.basicPublish(exchangeName, routingKey, true, null, msg.getBytes());
// 关闭连接
channel.close();
connection.close();
}
}
Consumer
package com.qiyexue.api.consumer;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
/**
* Return Listener模式消费者
*
* @author 七夜雪
* @create 2018-12-15 20:07
*/
public class Consumer {
public static void main(String[] args) throws Exception {
// 1. 创建连接工厂并设置属性
ConnectionFactory factory = new ConnectionFactory();;
factory.setHost("192.168.72.138");
factory.setPort(5672);
factory.setVirtualHost("/");
// 2. 创建连接
Connection connection = factory.newConnection();
// 3. 创建channel
Channel channel = connection.createChannel();
// 4. 声明Exchange
String exchangeName = "test_consumer_exchange";
String exchangeType = "topic";
String routingKey = "consumer.*";
channel.exchangeDeclare(exchangeName, exchangeType, true, false, null);
// 5. 声明消息队列
String queueName = "test_consumer_queue";
channel.queueDeclare(queueName, true, false, false, null);
// 6. 绑定队列和Exchange
channel.queueBind(queueName, exchangeName, routingKey);
// 7. 设置消费者为自定义的消费者
channel.basicConsume(queueName, true, new MyConsumer(channel));
}
}