- Routing
Use the direct repeater, bind multiple queues with the same routingkey on the repeater, send messages according to the routingkey, and all bound queues will receive the message
- Points to Note
The direct forwarder forwards the message directly to the queue bound to the routingkey :
channel.exchangeDeclare("direct_logs", "direct"); channel.basicPublish("direct_logs", severity , null, SerializationUtils.serialize(object));
Binding of forwarder and queue (specifying routingkey) :
//Specify routingkey when binding temporary queue and forwarder channel.queueBind(queueName, "direct_logs", severity);
The default repeater used in the work queue, the repeater is also direct, the work queue is to declare multiple consumers in parallel on the same queue; Routing is used to bind multiple queues with the same routingkey on the repeater (I didn't understand it at first)
- message sending class
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; /** * Send message class * @author sheungxin * */ public class Send{ /** * Send a message, direct the forwarder, and match the 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("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 !"); } }
- message receiving class
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; /** * Receive message class * @author sheungxin * */ public class Recv { /** * Used to receive messages, create a temporary queue, bind to the forwarder direct, and specify routingKey * @param queue * @throws Exception */ public static void recvAToB() throws Exception{ Connection conn=MqManager.newConnection(); Channel channel=conn.createChannel(); channel.exchangeDeclare("direct_logs", "direct"); //create a temporary queue String queueName=channel.queueDeclare().getQueue(); //Bind temporary queue and forwarder 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"); } }; //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"}; Random random=new Random(); return severities[random.nextInt(3)]; } public static void main(String[] args) throws Exception { recvAToB (); } }