RabbitMq06——路由模式

    在实际工作中有时候需要的是根据实际情况来分发消息,也就是路由模式。生产者在发送消息时为消息指定路由键,队列在与交换机绑定时也约定路由键,这样当消息发送到交换机的时候,交换机就会根据约定的路由键来向特定的队列发送消息。具体的我们来看示例代码,其中也有详细的注释。

生产者

package com.mmr.rabbitmq.direct;

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

import com.mmr.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class Send {
	
	private static final String EXCHANGE_NAME = "exchange_topic";
	
	public static void main(String[] args) throws IOException, TimeoutException {
		
		Connection connection = ConnectionUtils.getConnection();
		Channel channel = connection.createChannel();
		//声明交换机,并将交换机的类型设置为direct
		channel.exchangeDeclare(EXCHANGE_NAME, "direct");
		
		String message = "this is topic message--->";
		
		for (int i = 0; i < 50; i++) {
			//发送消息,并注明该消息的路由键,即第二个参数
			channel.basicPublish(EXCHANGE_NAME, "delete", null, (message + i).getBytes());
//			channel.basicPublish(EXCHANGE_NAME, "insert", null, (message + i).getBytes());
			System.out.println(message + i);
		}
		channel.close();
		connection.close();
		
	}

}

消费者1

接收约定路由键为insertdelete的消息

package com.mmr.rabbitmq.direct;

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

import com.mmr.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;

public class RecvAll {
	
	private static final String EXCHANGE_NAME = "exchange_topic";
	private static final String QUEUE_NAME = "queue_topic_delete";
	
	public static void main(String[] args) throws IOException, TimeoutException {
		
		Connection connection = ConnectionUtils.getConnection();
		final Channel channel = connection.createChannel();
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		channel.basicQos(1);
		
		//绑定队列与交换机,并指定绑定的路由键为insert和delete
		//同一个队列绑定交换机时可以指定多个路由键
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "delete");
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "insert");
		
		DefaultConsumer consumer = new DefaultConsumer(channel){
			
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				try {
					String msg = new String(body, "utf-8");
					Thread.sleep(2000);
					System.out.println(msg);
				} catch (Exception e) {
					e.printStackTrace();
				}finally {
					//向MQ发送确认,告诉MQ该条消息已经消费,可以重新发送了
					//第二个参数为false表示只确认当前这一条消息,
					//如果为 true,则额外将比第一个参数指定的 delivery tag 小的消息一并确认(批量确认针对的是整个信道)
					channel.basicAck(envelope.getDeliveryTag(), false);
				}
			}
		};
		
		channel.basicConsume(QUEUE_NAME, false, consumer);
		
	}

}

消费者2

只接收约定路由键为insert的消息

package com.mmr.rabbitmq.direct;

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

import com.mmr.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;

public class RecvInsert {
	
	private static final String EXCHANGE_NAME = "exchange_topic";
	private static final String QUEUE_NAME = "queue_topic_insert";
	
	public static void main(String[] args) throws IOException, TimeoutException {
		
		Connection connection = ConnectionUtils.getConnection();
		final Channel channel = connection.createChannel();
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		channel.basicQos(1);
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "insert");
		
		DefaultConsumer consumer = new DefaultConsumer(channel){
			
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				try {
					String msg = new String(body, "utf-8");
					Thread.sleep(2000);
					System.out.println(msg);
				} catch (Exception e) {
					e.printStackTrace();
				}finally {
					channel.basicAck(envelope.getDeliveryTag(), false);
				}
			}
		};
		
		channel.basicConsume(QUEUE_NAME, false, consumer);
			
	}

}

    运行两个消费者,然后让生产者以不同的路由键来发送消息,从控制台的打印信息便能更加直观的看出来该模式的应用情况。

猜你喜欢

转载自blog.csdn.net/yangsheng0111/article/details/80964178