RabbitMQ使用场景练习:主题Topic(五)

[list][*][b]主题转发器(Topic)[/b][/list]
Topic转发器的功效包含fanout、direct类型的转发器,其特殊的地方在于转发器与队列的绑定时routingkey可以使用通配符*、#,使用*就相当于fanout,不使用通配符就相当于direct,否则就是模糊匹配,匹配上的队列都可以收到消息

[list]
[*][b]注意要点[/b]
[/list]
[i]topic转发器[/i]:

channel.exchangeDeclare("topic_logs", "topic");
channel.basicPublish("topic_logs", severity , null, SerializationUtils.serialize(object));


[i]转发器与队列的绑定(指定routingkey),与direct转发器的差异在于routingkey可以使用*、#通配符模糊匹配[/i]:

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

 


[list]
[*][b]消息发送类[/b]
[/list]

 

 

package com.demo.mq.rabbitmq.example05;

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{

	/**
	 * 发送消息,topic转发器,通过routingKey模糊匹配相应的queue
	 * @param object 消息主体
	 * @throws IOException
	 */
	public static void sendAToB(Serializable object) throws Exception{
		Connection conn=MqManager.newConnection();
		Channel channel=conn.createChannel();
		channel.exchangeDeclare("topic_logs", "topic");
		for(int i=0;i<9;i++){
			String severity=getSeverity();
			channel.basicPublish("topic_logs", severity , null, SerializationUtils.serialize(object));
			System.out.println(severity+":Send '"+object+"'");
		}
		channel.close();
		conn.close();
	}

	private static String getSeverity(){
		String[] severities=new String[]{"kernal.info","kernal.warn","kernal.error","auth.info","auth.warn","auth.error"};
		Random random=new Random();
		return severities[random.nextInt(6)];
	}

	public static void main(String[] args) throws Exception {
		sendAToB("Hello World !");
	}
}


[list]
[*][b]消息接收类[/b]
[/list]

 

 

package com.demo.mq.rabbitmq.example05;

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 {

	/**
	 * 用于接收消息,创建一个临时队列,绑定在转发器topic上,并模糊指定routingKey
	 * @param queue
	 * @throws Exception
	 */
	public static void recvAToB() throws Exception{
		Connection conn=MqManager.newConnection();
		Channel channel=conn.createChannel();
		channel.exchangeDeclare("topic_logs", "topic");
		//创建一个临时队列
		String queueName=channel.queueDeclare().getQueue();
		//绑定临时队列和转发器logs
		String severity=getSeverity();
		channel.queueBind(queueName, "topic_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","kernal.*","auth.*"};
		Random random=new Random();
		return severities[random.nextInt(5)];
	}

	public static void main(String[] args) throws Exception {
		recvAToB();
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值