RABBITMQ of four Exchange types Fanout (Java) __RABBITMQ

Source: Internet
Author: User
Tags rabbitmq stringbuffer

RABBITMQ has four exchange types, namely direct, Fanout, Topic, HeadersExchange Features: fanoutDoes not process routing keys. You just need to simply bind the queue to the switch. A message sent to the type switch is broadcast to all queues that are bound to the switch. The following figure:
TopicMatches a routing key to a pattern. The queue needs to be bound to a pattern at this time. The symbol "#" matches one or more words, and the symbol "*" can only match one word. Therefore "logs.#" can match to "Logs.error", "Logs.info.toc", but "logs.*" can only match to "logs.error" and cannot match to "Logs.info.toc". The following figure:
DirectProcessing a routing key requires that a queue be bound to the switch, requiring that the message exactly match a specific routing key. This is a complete match. If a queue is bound to the switch requiring the routing key to be "logs", only messages that have a routing key of "logs" are forwarded, the routing key is "Logs.error" is not forwarded, and only the routing key is "logs". The following figure:
Headers  does not process the routing key, but rather matches it based on the headers attribute in the message content being sent. Specifies a set of key-value pairs when the queue is bound to exchange and when the message is sent to RABBITMQ, the headers of the message is matched to the key-value pair specified by the Exchange binding, or the message is routed to the queue if it is fully matched, otherwise it is not routed to the queue. The Headers property is a key-value pair, which can be hashtable, and the value of a key-value pair can be of any type. And Fanout,direct,topic's routing keys need to be in string form. However, headers is less used, the following is the official documentation of headers: A headers Exchange is designed to for routing on multiple attributes of that are more Easil Y expressed as message headers than a routing key. Headers exchanges ignore the routing key attribute. Instead, the attributes used for routing are taken attribute. A is considered matching if the value of the header equals the value specified upon binding. It is possible to bind a queue to a headers exchange using the more than one header for matching. In this case, the broker needs one more piece of information to application developer, namely, should it consider me Ssages with any of the headers matching, or all of them? This is what the "x-match" binding argument are for. When the ' x-match ' argument is set to ' any ', jUST one matching header value is sufficient. Alternatively, setting ' X-match ' to ' all ' mandates that all the values must match. Headers exchanges can be looked upon as "direct exchanges on steroids". Because they route based on header values, they can is used as direct exchanges where the routing key does not have a string; It could be a-integer or a hash (dictionary) for example.

With the above introduction, the following directly on the code bar.

Consumer:

Package fanout;
Import java.io.IOException;
Import Com.rabbitmq.client.AMQP;
Import Com.rabbitmq.client.Channel;
Import com.rabbitmq.client.Connection;
Import Com.rabbitmq.client.ConnectionFactory;
Import Com.rabbitmq.client.Consumer;
Import Com.rabbitmq.client.DefaultConsumer;


Import Com.rabbitmq.client.Envelope;
	
	public class Fanoutconsumer {private static final String Exchange_name = "Logs";
		public static void Main (string[] argv) throws Exception {ConnectionFactory factory = new ConnectionFactory ();
		RABBITMQ Monitor IP factory.sethost ("192.168.249.128");
		RABBITMQ listens to the default port Factory.setport (5672);
		Set access to the user factory.setusername ("test");
		Factory.setpassword ("test");
		Connection Connection = Factory.newconnection ();
		Channel Channel = Connection.createchannel ();
		Declares the routing name and type Channel.exchangedeclare (Exchange_name, "fanout");
		Gets the random queue name String queuename = Channel.queuedeclare (). Getqueue ();
		Create Queues Channel.queuedeclare (QueueName, False, False, true, NULL); //bind the queue to the route channel.queuebind (QueueName, Exchange_name, "");


		System.out.println ("Waiting for msg ...");  Consumer Consumer = new Defaultconsumer (channel) {@Override public void Handledelivery (String consumertag, Envelope Envelope, AMQP.
				Basicproperties properties, byte[] throws IOException {String message = new String (Body, "UTF-8");
			System.out.println ("Received msg is '" + Message + "");
		}
		};
	Channel.basicconsume (QueueName, true, consumer);
 }
}

Producer:

Package fanout;
Import Com.rabbitmq.client.Channel;
Import com.rabbitmq.client.Connection;

Import Com.rabbitmq.client.ConnectionFactory;

	public class Fanoutproducer {private static final String Exchange_name = "Logs";
		public static void Main (string[] argv) throws exception{connectionfactory factory = new ConnectionFactory ();
		RABBITMQ Monitor IP factory.sethost ("192.168.249.128");
		RABBITMQ listens to the default port Factory.setport (5672);
		Set access to the user factory.setusername ("test");
		Factory.setpassword ("test");
		Connection Connection = Factory.newconnection ();

		Channel Channel = Connection.createchannel ();
		Declares the routing name and type Channel.exchangedeclare (Exchange_name, "fanout");
		
		String message = Makemessage (argv);
		Channel.basicpublish (Exchange_name, "", NULL, Message.getbytes ());

		System.out.println ("Sent msg is '" + Message + "");
		Channel.close ();
	Connection.close (); private static String Makemessage (string[] strings) {if (Strings.length < 1) {return "This is the default message ...
		"; }else{
			StringBuffer buffer= new StringBuffer ();
			for (int i = 0; i < strings.length i++) {buffer.append (strings[i]);
		return buffer.tostring ();
 }
	}

}

Run Consumer:


Run Producer:


OK, here we go ~ wish you a happy life ...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.