java连接RabbitMQ--路由模式-05
路由模式(routing) 在订阅模式的基础上进行队列路由,在定义交换机的时候设置routingKey(路由key),消费者绑定交换机也定义routingKey,只有两个Key相同才能消费到这条消息.
想要使用路由模式,声明交换机的时候得设置成direct模式,发布订阅为fanout模式.
例如生产者发送消息的的时候设置了routingKey为error的时候,队列1绑定交换机设置的key为error,队列2绑定交换机的时候设置了三个key,但是包含error,这时生产者发布一条消息的时候,队列1和队列2都可以消费这条消息.
当生产者发送消息的的时候设置了routingKey为info的时候,只有队列2才能消费这条消息.
生产者
package com.buba.routing;
import com.buba.utils.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
public class Send {
private static final String EXCHANGE_NAME="text_exchange_direct";
public static void main(String[] args)throws Exception {
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel();
//声明交换机
channel.exchangeDeclare(EXCHANGE_NAME,"direct");
String msg = "hello direct";
String routingKey = "haha";
channel.basicPublish(EXCHANGE_NAME,routingKey,null,msg.getBytes());
channel.basicPublish(EXCHANGE_NAME,"info",null,msg.getBytes());
System.out.println("send:"+msg);
channel.close();
connection.close();
}
}
消费者1
package com.buba.routing;
import com.buba.utils.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
public class Recv1 {
private static final String QUEUE_NAME="test_queue_direct_1";
private static final String EXCHANGE_NAME="text_exchange_direct";
public static void main(String[] args)throws Exception {
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel();
//队列声明
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
channel.basicQos(1);
//绑定队列到交换机 转发器
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"error");
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"haha");
//定义一个消费者
Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String str = new String(body,"utf-8");
System.out.println("[1]:"+str);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
System.out.println("[1] done");
//给消息队列反馈一个消息,表示执行完成了可以发送下一条了
channel.basicAck(envelope.getDeliveryTag(),false);
}
}
};
boolean autoAck=false;//自动应答是true,手动应答为false
channel.basicConsume(QUEUE_NAME,autoAck,consumer);
}
}
消费者2
package com.buba.routing;
import com.buba.utils.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;
public class Recv2 {
private static final String QUEUE_NAME="test_queue_direct_2";
private static final String EXCHANGE_NAME="text_exchange_direct";
public static void main(String[] args)throws Exception {
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel();
//队列声明
channel.queueDeclare(QUEUE_NAME,false,false,false,null);
channel.basicQos(1);
//绑定队列到交换机 转发器
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"error");
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"info");
channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,"warning");
//定义一个消费者
Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String str = new String(body,"utf-8");
System.out.println("[2]:"+str);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
System.out.println("[2] done");
//给消息队列反馈一个消息,表示执行完成了可以发送下一条了
channel.basicAck(envelope.getDeliveryTag(),false);
}
}
};
boolean autoAck=false;//自动应答是true,手动应答为false
channel.basicConsume(QUEUE_NAME,autoAck,consumer);
}
}