Publish subscription Mode
X: Switch (forwarder)
The producer sends the message to the switch, the switch sends the message to the queue, and the queue needs to be bound to the switch.
1, one producer, multiple consumers
2, each consumer has its own queue
Java 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;
/**
* Publish/Subscribe mode
* Message Producers
*
* @author Zy
*
*/
public class Send {
private static final String Exchagne_name = "Test_exchange_fanot";
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.fanout);
String msg = "Publish/Subscribe message!";
Send
Channel.basicpublish (Exchagne_name, "", NULL, Msg.getbytes ());
SYSTEM.OUT.PRINTLN ("Send 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_fanout_email";
private static final String Exchagne_name = "Test_exchange_fanot";
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, False, False, false, NULL);
Bind a queue to a switch
Channel.queuebind (queue_name, Exchagne_name, "");
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 email 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_fanout_sms";
private static final String Exchagne_name = "Test_exchange_fanot";
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, False, False, false, NULL);
Bind a queue to a switch
Channel.queuebind (queue_name, Exchagne_name, "");
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 SMS msg:" + message);
Channel.basicack (Envelope.getdeliverytag (), false);
}
};
Listening queue
Channel.basicconsume (Queue_name, false, consumer);
}
}
Summary: The Publish/subscribe pattern requires the queue to be bound to the switch and does not define a routing key
Reference: http://www.rabbitmq.com/tutorials/tutorial-four-java.html
Rabbitmqjava Series 4-publish/subscribe