标签:ESS row key icc 发送消息 null end dir tin
消费者接收指定路由关键字的消息,这时就要用到直连类型交换器
通过直连交换器,生产者发送不同路由关键字的信息,消费者端通过绑定自己感兴趣的路由关键字来接收消息。
生产者
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; public class Producer { private static final String EXCHANGE_NAME = "myexchange"; // 路由关键字 private static final String[] routingKeys = new String[] { "keys1", "keys2", "keys3" }; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); // 声明交换器 channel.exchangeDeclare(EXCHANGE_NAME, "direct"); // 发送消息 for (String severity : routingKeys) { String message = "Send the message routingKeys:" + severity; channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes()); System.out.println("p Sent ‘" + severity + "‘:‘" + message + "‘"); } channel.close(); connection.close(); } }
消费者1
import java.io.IOException; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.Consumer; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; public class Consumer1 { // 交换器名称 private static final String EXCHANGE_NAME = "myexchange"; // 路由关键字 private static final String[] routingKeys = new String[] { "keys1", "keys2" }; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); // 声明交换器 channel.exchangeDeclare(EXCHANGE_NAME, "direct"); // 获取匿名队列名称 String queueName = channel.queueDeclare().getQueue(); // 根据路由关键字进行多重绑定 for (String severity : routingKeys) { channel.queueBind(queueName, EXCHANGE_NAME, severity); System.out.println("ReceiveDirect1 exchange:" + EXCHANGE_NAME + ", queue:" + queueName + ", BindRoutingKey:" + severity); } System.out.println("Consumer1 Waiting for messages..."); 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("Consumer1 Received ‘" + envelope.getRoutingKey() + "‘:‘" + message + "‘"); } }; channel.basicConsume(queueName, true, consumer); } }
消费者2
import java.io.IOException; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.Consumer; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; public class Consumer2 { // 交换器名称 private static final String EXCHANGE_NAME = "myexchange"; // 路由关键字 private static final String[] routingKeys = new String[] { "keys3"}; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); // 声明交换器 channel.exchangeDeclare(EXCHANGE_NAME, "direct"); // 获取匿名队列名称 String queueName = channel.queueDeclare().getQueue(); // 根据路由关键字进行多重绑定 for (String severity : routingKeys) { channel.queueBind(queueName, EXCHANGE_NAME, severity); System.out.println("ReceiveDirect2 exchange:" + EXCHANGE_NAME + ", queue:" + queueName + ", BindRoutingKey:" + severity); } System.out.println("Consumer2 Waiting for messages..."); 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("Consumer2 Received ‘" + envelope.getRoutingKey() + "‘:‘" + message + "‘"); } }; channel.basicConsume(queueName, true, consumer); } }
依次运行消费者1和2,生产者
可以看到消费者1接收到了路由关键字为keys1和keys2的消息,消费者2则是keys3。
标签:ESS row key icc 发送消息 null end dir tin
原文地址:https://www.cnblogs.com/zengnansheng/p/10389656.html