`
sheungxin
  • 浏览: 105484 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

RabbitMQ使用场景练习:路由选择Routing(四)

    博客分类:
  • MQ
阅读更多
  • 路由选择(Routing)
     Routing即按照某条路线把消息发送出去,所有这条路线上的队列都将接收到消息。
     使用direct转发器,在转发器上使用同一routingkey绑定多个队列,根据routingkey发送消息,所有绑定的队列都将收到消息

  • 注意要点

direct转发器,把消息直接转发到绑定routingkey的队列
channel.exchangeDeclare("direct_logs", "direct");
channel.basicPublish("direct_logs", severity , null, SerializationUtils.serialize(object));

转发器与队列的绑定(指定routingkey)
 //在绑定临时队列和转发器时指定routingkey
 channel.queueBind(queueName, "direct_logs", severity);

工作队列中使用的默认转发器,转发器也是direct,工作队列是同一队列上声明多个consumer并行;Routing是用在转发器上同一routingkey绑定多个队列(刚开始没有理解)

  • 消息发送类

package com.demo.mq.rabbitmq.example04;

import java.io.IOException;
import java.io.Serializable;
import java.util.Random;

import org.apache.commons.lang3.SerializationUtils;

import com.demo.mq.rabbitmq.MqManager;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * 发送消息类
 * @author sheungxin
 *
 */
public class Send{

	/**
	 * 发送消息,direct转发器,通过routingKey匹配相应的queue
	 * @param object 消息主体
	 * @throws IOException
	 */
	public static void sendAToB(Serializable object) throws Exception{
		Connection conn=MqManager.newConnection();
		Channel channel=conn.createChannel();
		channel.exchangeDeclare("direct_logs", "direct");
		for(int i=0;i<4;i++){
			String severity=getSeverity();
			channel.basicPublish("direct_logs", severity , null, SerializationUtils.serialize(object));
			System.out.println(severity+":Send '"+object+"'");
		}
		channel.close();
		conn.close();
	}
	
	private static String getSeverity(){
		String[] severities=new String[]{"info","warn","error"};
		Random random=new Random();
		return severities[random.nextInt(3)];
	}
	
	public static void main(String[] args) throws Exception {
		sendAToB("Hello World !");
	}
}

  • 消息接收类

package com.demo.mq.rabbitmq.example04;

import java.io.IOException;
import java.util.Random;

import org.apache.commons.lang3.SerializationUtils;

import com.demo.mq.rabbitmq.MqManager;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

/**
 * 接收消息类
 * @author sheungxin
 *
 */
public class Recv {
	
	/**
	 * 用于接收消息,创建一个临时队列,绑定在转发器direct上,并指定routingKey
	 * @param queue
	 * @throws Exception
	 */
	public static void recvAToB() throws Exception{
		Connection conn=MqManager.newConnection();
		Channel channel=conn.createChannel();
		channel.exchangeDeclare("direct_logs", "direct");
		//创建一个临时队列
		String queueName=channel.queueDeclare().getQueue();
		//绑定临时队列和转发器logs
		String severity=getSeverity();
		channel.queueBind(queueName, "direct_logs", severity);
		System.out.println(severity+":Received ...");
		Consumer consumer=new DefaultConsumer(channel){
			@Override
			public void handleDelivery(String consumerTag,Envelope envelope,AMQP.BasicProperties properties,byte[] body) throws IOException{
				String mes=SerializationUtils.deserialize(body);
				System.out.println(envelope.getRoutingKey()+":Received :'"+mes+"' done");
			}
		};
		//关闭自动应答机制,默认开启;这时候需要手动进行应该
		channel.basicConsume(queueName, true, consumer);
	}
	
	private static String getSeverity(){
		String[] severities=new String[]{"info","warn","error"};
		Random random=new Random();
		return severities[random.nextInt(3)];
	}
	
	public static void main(String[] args) throws Exception {
		recvAToB();
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics