首先生产者发送了一条消息给交换机,其中包含一个key,当然队列中也包含一个key,但是在交换机发送消息到队列的时候根据key进行发送,如果交换机的key和队列的key对应上,就发送,否则就不发送。
废话不多说直接上代码
下面是生产者
public class Send {
private static final String EXCHANGE_NAME = "test_exchange_declare";
public static void main(String[] args) throws Exception{
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
//声明exchange,类型为direct
channel.exchangeDeclare(EXCHANGE_NAME, "direct");
String msg = "hello direct";
//声明一个路由键
String routingKey = "info";
//将刚刚声明的路由键添加到这里,让交换机和队列去匹配
channel.basicPublish(EXCHANGE_NAME,routingKey ,null , msg.getBytes());
System.out.println("send:"+msg);
channel.close();
connection.close();
}
}
下面是消费者1
public class Recv {
private static final String EXCHANGE_NAME="test_exchange_declare";
private static final String QUEUE_NAME="test_queue_direct_1";
public static void main(String[] args)throws IOException, TimeoutException {
Connection connection = ConnectionUtil.getConnection();
final Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//重点:设置队列接收路由键为error值
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error");
channel.basicQos(1);
//定义一个消费者
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("Recv msg:"+str);
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("Recv done");
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
//关闭自动应答
boolean auto = false;
channel.basicConsume(QUEUE_NAME, auto, consumer);
}
}
下面是消费者2
public class Recv2 {
private static final String EXCHANGE_NAME="test_exchange_declare";
private static final String QUEUE_NAME="test_queue_direct_2";
public static void main(String[] args)throws IOException, TimeoutException {
Connection connection = ConnectionUtil.getConnection();
final Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error");
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info");
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "warning");
channel.basicQos(1);
//定义一个消费者
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] Recv msg:"+str);
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("[2] Recv done");
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
//关闭自动应答
boolean auto = false;
channel.basicConsume(QUEUE_NAME, auto, consumer);
}
}
首先我们先运行一下生产者,让它先创建一个交换机,不然直接运行消费者的话会报错,报没有找到你定义的交换机,当运行了一下生产者后,再把两个消费者打开,然后在运行消费者。
结论:当我们在生产者上输入路由键为error时,消费者1和消费者2都接收到消息,当我们在生产者上设置路由键为info时,只有消费者2接收到了,因为消费者一没有绑定路由键为info。
topic主题模式
其实topic和routing的功能类似,只是topic有通配的功能,废话不多说,直接上代码。
生产者代码
public class Send {
private static final String EXCHANGE_NAME = "test_exchange_topic";
public static void main(String[] s) throws Exception{
Connection connection = ConnectionUtil.getConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "topic");
String msgString = "商品....";
//发送为goods.add给指定队列标签
channel.basicPublish(EXCHANGE_NAME, "goods.add", null, msgString.getBytes());
System.out.println("---send "+msgString);
channel.close();
connection.close();
}
}
消费者1代码
public class Recv {
private static final String EXCHANG_NAME="test_exchange_topic";
private static final String QUEUE_NAME = "test_queue_topic_1";
public static void main(String[] s) throws Exception{
Connection connection = ConnectionUtil.getConnection();
final Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//只接受goods.add的传来的数据
channel.queueBind(QUEUE_NAME, EXCHANG_NAME, "goods.add");
channel.basicQos(1);
Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body,"utf-8");
System.out.println("[1] Recv msg:"+msg);
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("[1] done");
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
channel.basicConsume(QUEUE_NAME, false, consumer);
}
}
消费者2代码
public class Recv2 {
private static final String EXCHANG_NAME="test_exchange_topic";
private static final String QUEUE_NAME = "test_queue_topic_2";
public static void main(String[] s) throws Exception{
Connection connection = ConnectionUtil.getConnection();
final Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//接收前缀为goods.所有传来的数据
channel.queueBind(QUEUE_NAME, EXCHANG_NAME, "goods.#");
channel.basicQos(1);
Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String msg = new String(body,"utf-8");
System.out.println("[2] Recv msg:"+msg);
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("[2] done");
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
};
channel.basicConsume(QUEUE_NAME, false, consumer);
}
}