[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();
}
}