The routing mode only needs to define the routing key to
Code implementation:
Producers
Import java.io.IOException;
Import java.util.concurrent.TimeoutException;
Import Com.rabbitmq.client.BuiltinExchangeType;
Import Com.rabbitmq.client.Channel;
Import com.rabbitmq.client.Connection;
Import Com.zy.rabbitmq.util.ConnectionUtil;
/**
* Routing Mode
* Message Producers
*
* @author Zy
*
*/
public class Send {
private static final String Exchagne_name = "Test_exchange_direct";
public static void Main (string[] args) throws IOException, TimeoutException {
Get connections
Connection Connection = Connectionutil.getconnection ();
Get a channel from the connection
Channel channel = Connection.createchannel ();
Switch declaration
Channel.exchangedeclare (Exchagne_name, builtinexchangetype.direct);
String msg = "routing message!";
Binds the Oragne routing key and sends
Channel.basicpublish (Exchagne_name, "Oragne", NULL, Msg.getbytes ());
System.out.println ("Send Routing msg:" + msg);
Channel.close ();
Connection.close ();
}
}
Consumer 1
Import java.io.IOException;
Import java.util.concurrent.TimeoutException;
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;
Import Com.zy.rabbitmq.util.ConnectionUtil;
/**
* Consumer Consumer News
*
* @author Zy
*
*/
public class Receive {
private static final String queue_name = "Test_queue_direct1";
private static final String Exchagne_name = "Test_exchange_direct";
public static void Main (string[] args) throws IOException, TimeoutException {
Get connections
Connection Connection = Connectionutil.getconnection ();
Get a channel from the connection
Final Channel channel = Connection.createchannel ();
Queue Declaration
Channel.queuedeclare (Queue_name, True, False, false, NULL);
Bind a queue to a switch
Channel.queuebind (queue_name, Exchagne_name, "Oragne");
Channel.basicqos (1);
Event-based
Consumer Consumer = new Defaultconsumer (channel) {
@Override
public void Handledelivery (String consumertag, Envelope Envelope, AMQP. Basicproperties Properties,
Byte[] body) throws IOException {
String message = new String (Body, "UTF-8");
System.out.println ("Receive Routing Oragne msg:" + message);
Channel.basicack (Envelope.getdeliverytag (), false);
}
};
Listening queue
Channel.basicconsume (Queue_name, false, consumer);
}
}
Consumer 2
Import java.io.IOException;
Import java.util.concurrent.TimeoutException;
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;
Import Com.zy.rabbitmq.util.ConnectionUtil;
/**
* Consumer Consumer News
*
* @author Zy
*
*/
public class Receive2 {
private static final String queue_name = "Test_queue_direct2";
private static final String Exchagne_name = "Test_exchange_direct";
public static void Main (string[] args) throws IOException, TimeoutException {
Get connections
Connection Connection = Connectionutil.getconnection ();
Get a channel from the connection
Final Channel channel = Connection.createchannel ();
Queue Declaration
Channel.queuedeclare (Queue_name, True, False, false, NULL);
Bind the queue to the switch and bind the error routing key
Channel.queuebind (queue_name, Exchagne_name, "Black");
Channel.queuebind (queue_name, Exchagne_name, "green");
Channel.basicqos (1);
Event-based
Consumer Consumer = new Defaultconsumer (channel) {
@Override
public void Handledelivery (String consumertag, Envelope Envelope, AMQP. Basicproperties Properties,
Byte[] body) throws IOException {
String message = new String (Body, "UTF-8");
System.out.println ("Receive Routing Black or green msg:" + message);
Channel.basicack (Envelope.getdeliverytag (), false);
}
};
Listening queue
Channel.basicconsume (Queue_name, false, consumer);
}
}
Producer specifies the route key for Orange
Consumer 1 Specifies the route key for Orange, so it can receive messages
Consumer 2 Specifies the route key for black and green and therefore cannot receive messages
Reference: http://www.rabbitmq.com/tutorials/tutorial-four-java.html
Rabbitmqjava Series 5-routing Mode