RabbitMQ usage scenario exercise: publish/subscribe (3)

  • Publish/Subscribe
     That is, single-point sending of messages and multi-point receiving are realized. Use a fanout repeater to broadcast to all queues it knows about

  • Points to Note

The routingKey is not required in the fanout repeater, and the specification is invalid

to create a fanout repeater :
channel.exchangeDeclare("fanout_logs", "fanout");
channel.basicPublish("fanout_logs", "" , null, SerializationUtils.serialize(object));

Creation of temporary queue :
//Create a temporary queue and return the queue name
String queueName=channel.queueDeclare().getQueue();

Binding of forwarder and queue :
//Bind temporary queue and forwarder logs
channel.queueBind(queueName, "fanout_logs", "");

  • message sending class

package com.demo.mq.rabbitmq.example03;

import java.io.IOException;
import java.io.Serializable;

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, fanout repeater, broadcast to all queues on this repeater
	 * @param object message body
	 * @throws IOException
	 */
	public static void sendAToB(Serializable object) throws Exception{
		Connection conn=MqManager.newConnection();
		Channel channel=conn.createChannel();
		channel.exchangeDeclare("fanout_logs", "fanout");
		channel.basicPublish("fanout_logs", "" , null, SerializationUtils.serialize(object));
		System.out.println("A Send :'"+object+"'");
		channel.close();
		conn.close();
	}
	
	public static void main(String[] args) throws Exception {
		for(int i=0;i<4;i++){
			sendAToB("Hello World "+i+"!");
		}
	}
}

  • message receiving class

package com.demo.mq.rabbitmq.example03;

import java.io.IOException;
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, bound to the repeater fanout
	 * @throws Exception
	 */
	public static void recvAToB() throws Exception{
		Connection conn=MqManager.newConnection();
		Channel channel=conn.createChannel();
		channel.exchangeDeclare("fanout_logs", "fanout");
		//create a temporary queue
		String queueName=channel.queueDeclare().getQueue();
		//Bind temporary queue and forwarder logs
		channel.queueBind(queueName, "fanout_logs", "");
		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("B Received :'"+mes+"'...");
			}
		};
		//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=326931942&siteId=291194637