RabbitMQ usage scenario exercise: Headers (6)

  • Headers repeater
     When sending a message, you can define some key-value pairs in the header. When the receiving message queue is bound to the headers forwarder, you can specify the key-value pair. There are two ways: all and any (the key-value pair specified when the queue is bound to the forwarder and the headers The stored key-value pair matches), and the message can be received if it matches

  • Points to Note

headers forwarder :
//declare the headers forwarder
channel.exchangeDeclare("header_exchange", BuiltinExchangeType.HEADERS);

Add header information when publishing a message :
//Define the key-value pair stored by headers
Map<String, Object> headers=new HashMap<String, Object>();
headers.put("key", "123456");
headers.put("token", "654321");
// put the key-value pair in properties
Builder properties=new BasicProperties.Builder();
properties.headers(headers);
properties.deliveryMode(2);//Persistence
channel.basicPublish("header_exchange", "" , properties.build(), SerializationUtils.serialize(object));

The binding between the forwarder and the queue (specifying the header) is matched by key-value pairs, and there are two types: any and all :
//Specify the matching type of headers (all, any), key-value pair
Map<String, Object> headers=new HashMap<String, Object>();
headers.put("x-match", "all");//all any (as long as there is a key-value pair matching)
headers.put("key", "123456");
//headers.put("token", "6543211");
//Bind the temporary queue and forwarder header_exchange
channel.queueBind(queueName, "header_exchange", "", headers);


  • message sending class

package com.demo.mq.rabbitmq.example07;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.SerializationUtils;
import com.demo.mq.rabbitmq.MqManager;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.AMQP.BasicProperties.Builder;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * Send message class
 * @author sheungxin
 *
 */
public class Send{

	/**
	 * Send a message, HEADERS forwarder, match the corresponding queue through the key-value pair in HEADERS
	 * @param object message body
	 * @throws IOException
	 */
	public static void sendAToB(Serializable object) throws Exception{
		Connection conn=MqManager.newConnection();
		Channel channel=conn.createChannel();
		//declare the headers forwarder
		channel.exchangeDeclare("header_exchange", BuiltinExchangeType.HEADERS);
		//Define the key-value pair stored by headers
		Map<String, Object> headers=new HashMap<String, Object>();
		headers.put("key", "123456");
		headers.put("token", "654321");
		// put the key-value pair in properties
		Builder properties=new BasicProperties.Builder();
		properties.headers(headers);
		properties.deliveryMode(2);//Persistence
		channel.basicPublish("header_exchange", "" , properties.build(), SerializationUtils.serialize(object));
		System.out.println("Send '"+object+"'");
		channel.close();
		conn.close();
	}
	
	public static void main(String[] args) throws Exception {
		sendAToB("Hello World !");
	}
}

  • message receiving class

package com.demo.mq.rabbitmq.example07;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.SerializationUtils;

import com.demo.mq.rabbitmq.MqManager;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
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 HEADERS, and vaguely specify key-value pairs
	 * @param queue
	 * @throws Exception
	 */
	public static void recvAToB() throws Exception{
		Connection conn=MqManager.newConnection();
		Channel channel=conn.createChannel();
		channel.exchangeDeclare("header_exchange", BuiltinExchangeType.HEADERS);
		//create a temporary queue
		String queueName=channel.queueDeclare().getQueue();
		//Specify the matching type of headers (all, any), key-value pair
		Map<String, Object> headers=new HashMap<String, Object>();
		headers.put("x-match", "all");//all any (as long as there is a key-value pair matching)
		headers.put("key", "123456");
//		headers.put("token", "6543211");
		//Bind the temporary queue and forwarder header_exchange
		channel.queueBind(queueName, "header_exchange", "", headers);
		System.out.println("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);
	}
	
	public static void main(String[] args) throws Exception {
		recvAToB ();
	}

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326860733&siteId=291194637