1. switch type
rabbitmq There are four common types of switches: direct, topic, fanout, headers.
General headers do not work is used more fanout, it will bind all messages pushed to switch on this queue, the efficiency of these types of switches is the highest.
Switch is what? I felt almost gateway is routing, forwarding messages.
A specific use of several talk switch
2. Use the switch
2.1 direct switch
direct: 1 to 1 corresponds directly connected.
Producers ----> direct exchange ---> queue ----> consumer
// production side code public static void main (String [] args) throws Exception { //1 创建ConnectionFactory ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("127.0.0.1"); connectionFactory.setPort(5672); connectionFactory.setVirtualHost("/"); // 2 Create Connection Connection Connection = connectionFactory.newConnection (); // 3 Creating Channel Channel Channel = connection.createChannel (); // 4 statement String exchangeName = "direct_exchange" ; // 5. routing key, its consumption must end consistent String routingKey = "direct.routingKey" ; // 5 send String msg = "Direct the this IS the Exchange the Test ..." ; channel.basicPublish(exchangeName, routingKey , null , msg.getBytes()); }
// consumer side code public static void main (String [] args) throws Exception { ConnectionFactory connectionFactory = new ConnectionFactory() ; connectionFactory.setHost("127.0.0.1"); connectionFactory.setPort(5672); connectionFactory.setVirtualHost("/"); connectionFactory.setAutomaticRecoveryEnabled(true); connectionFactory.setNetworkRecoveryInterval(3000); Connection connection = connectionFactory.newConnection(); Channel Channel = connection.createChannel (); // 4 statement switch name, note that it must be consistent with the production side String exchangeName = "direct_exchange" ; ExchangeType String = "Direct" ; // queue name taken lightly, the meaning of meaning, binding on the line String queueName = "direct_queue" ; // must be consistent with the production side String routingKey = "direct.routingKey" ; // represents a switch statement channel.exchangeDeclare (exchangeName, ExchangeType, to true , false , false , null ); // declaration was a queue channel.queueDeclare (queueName, false , false , false , null ); // establish a binding relationship: channel.queueBind (queueName, exchangeName, routingKey); // Durable whether persistent message QueueingConsumer Consumer = new new QueueingConsumer (Channel); // parameters: queue name, whether automatically the ACK, Consumer channel.basicConsume (queueName, to true , Consumer); // cycle message acquiring the while ( to true ) { / / get the message, if no message, this step will have been blocking Delivery Delivery = consumer.nextDelivery (); String msg = new String(delivery.getBody()); System.out.println ( "receive the message:" + MSG); } }
Message can be routed to a queue. . . .
2.2 topic Switch
direct one to one type of switch can deliver the message, and the type of topic switches Niubi, it supports gooey match , what does it mean?
In the direct types of switches, the production side and routingKey end of the message must be the same as, or can not get the message. While the topic is not the same type, such as: the production is terminated routingKey zheng.qin.feng, routingKey message inflicted end may have the following lines:
zheng. #
#.feng
zheng.qin. *
In short, for the topic type of switch, everything is looking routingKey, routingKey have some small-minded consultations relationship with routingKey the production side of the switch when bound to end if the message queue and switch to bind, the message will eventually be delivered to the queue in.
Note: The messages are stored in the queue, the switch will only forward messages are not stored
#: Any word
*: Word Match
/** * Production end */ public static void main(String[] args) throws Exception { //1 创建ConnectionFactory ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("127.0.0.1"); connectionFactory.setPort(5672); connectionFactory.setVirtualHost("/"); //2 创建Connection Connection connection = connectionFactory.newConnection(); //3 创建Channel Channel channel = connection.createChannel(); //4 声明 String exchangeName = "topic_exchange"; String routingKey1 = "user.mmp"; String routingKey2 = "user.exchange"; String routingKey3 = "user.test"; //5 发送 String msg = "abc lfkdfjdlfjdlfkdlkfdlf"; channel.basicPublish(exchangeName, routingKey1 , null , msg.getBytes()); channel.basicPublish(exchangeName, routingKey2 , null , msg.getBytes()); channel.basicPublish(exchangeName, routingKey3 , null , msg.getBytes()); channel.close(); connection.close(); }
/** * Consumers */ public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory = new ConnectionFactory() ; connectionFactory.setHost("127.0.0.1"); connectionFactory.setPort(5672); connectionFactory.setVirtualHost("/"); connectionFactory.setAutomaticRecoveryEnabled(true); connectionFactory.setNetworkRecoveryInterval(3000); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); //4 声明 String exchangeName = "topic_exchange"; String exchangeType = "topic"; String queueName = "topic_queue"; RoutingKey String = "User *." ; // . 1 switch statement channel.exchangeDeclare (exchangeName, ExchangeType, to true , to false , to false , null ); // 2 Statement queue channel.queueDeclare (queueName, to false , to false , to false , null ) ; // 3 bond from switches and queue: channel.queueBind (queueName, exchangeName, routingKey); // Durable whether persistent message QueueingConsumer Consumer = new new QueueingConsumer (Channel); // parameters: queue name, whether automatically the ACK, Consumer channel.basicConsume (queueName, to true , Consumer); // cycle message acquiring the while ( to true ) { / / get the message, if no message, this step will have been blocking Delivery Delivery = consumer.nextDelivery (); String msg = new String(delivery.getBody()); System.out.println ( "receive the message:" + MSG); } }
The message is routed to routingKey to decide which queue. . . . .
2.3 fanout switch
This nothing to say, with routingKey wood has a relationship, as long queues bound to the fanout switch type, can get the message.
Simply put: that is one relationship more than the topic ,, type of switch or melodramatic, after all topic switches despise ugly, fanout it, a woman will do.
// Manufacturer public static void main (String [] args) throws Exception { //1 创建ConnectionFactory ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost("127.0.0.1"); connectionFactory.setPort(5672); connectionFactory.setVirtualHost("/"); //2 创建Connection Connection connection = connectionFactory.newConnection(); //3 创建Channel Channel channel = connection.createChannel(); //4 声明 String exchangeName = "test_fanout_exchange"; //5 发送 for(int i = 0; i < 10; i ++) { String msg = "hahahah"; channel.basicPublish(exchangeName, "", null , msg.getBytes()); } channel.close(); connection.close(); }
// 消息者 public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory = new ConnectionFactory() ; connectionFactory.setHost("127.0.0.1"); connectionFactory.setPort(5672); connectionFactory.setVirtualHost("/"); connectionFactory.setAutomaticRecoveryEnabled(true); connectionFactory.setNetworkRecoveryInterval(3000); Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); //4 声明 String exchangeName = "fanout_exchange"; String exchangeType = "fanout"; String queueName = "test_fanout_queue"; RoutingKey String = ""; // not set routing keys can also be set, it does not matter, anyway, all right channel.exchangeDeclare (exchangeName, ExchangeType, to true , false , false , null ); channel.queueDeclare(queueName, false, false, false, null); channel.queueBind(queueName, exchangeName, routingKey); // Durable whether persistent message QueueingConsumer Consumer = new new QueueingConsumer (Channel); // parameters: queue name, whether automatically the ACK, Consumer channel.basicConsume (queueName, to true , Consumer); // cycle message acquiring the while ( to true ) { Delivery delivery = consumer.nextDelivery(); String msg = new String(delivery.getBody()); System.out.println ( "receive the message:" + MSG); } }
Nima, bound to this queue as long as the switch, are routed. . . .