Rabbitmqjava Series 4-publish/subscribe

Source: Internet
Author: User
Tags rabbitmq

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

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.