- Topic Forwarder (Topic)
- Points to Note
topic forwarder :
channel.exchangeDeclare("topic_logs", "topic"); channel.basicPublish("topic_logs", severity , null, SerializationUtils.serialize(object));
The binding between the forwarder and the queue (specifying the routingkey), the difference from the direct forwarder is that the routingkey can use *, # wildcard fuzzy matching :
//Specify routingkey when binding temporary queue and forwarder channel.queueBind(queueName, "topic_logs", severity);
- message sending class
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; /** * Send message class * @author sheungxin * */ public class Send{ /** * Send message, topic repeater, fuzzy matching corresponding queue by routingKey * @param object message body * @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 !"); } }
- message receiving class
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; /** * Receive message class * @author sheungxin * */ public class Recv { /** * Used to receive messages, create a temporary queue, bind to the repeater topic, and vaguely specify routingKey * @param queue * @throws Exception */ public static void recvAToB() throws Exception{ Connection conn=MqManager.newConnection(); Channel channel=conn.createChannel(); channel.exchangeDeclare("topic_logs", "topic"); //create a temporary queue String queueName=channel.queueDeclare().getQueue(); //Bind temporary queue and forwarder 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"); } }; //Turn off the automatic response mechanism, which is enabled by default; at this time, you need to manually perform the response 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 (); } }