Rabbitmqjava Series 5-routing Mode

Source: Internet
Author: User
Tags rabbitmq

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

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.