05 rabbitmq之Routing

在介绍了rabbitmq的Publish/subscrige模式之后,这一节我们将阐述它的Routing模式的用法。

1、前提约束

  • 已经完成rabbitmq的第一个简单的测试程序
    https://www.jianshu.com/p/77bfc4fe5a1a
  • 2、操作步骤
    我们马上要测试的rabbitmq的工作模式如下:
    Routing
  • 在src/main/java文件夹下创建包net.wanho.rabbitmq.routing
  • 在net.wanho.rabbitmq.routing创建Producer.java
package net.wanho.rabbitmq.routing;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Producer {

    public static void main(String[] argv) throws Exception {
        String queue_inform_email = "queue_inform_email";
        String queue_inform_sms = "queue_inform_sms";
        String exchange_routing_inform = "exchange_routing_inform";
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setHost("localhost");
        //创建一个连接
        Connection connection = factory.newConnection();
        //创建与交换机的通道,每个通道代表一个会话
        Channel channel = connection.createChannel();
        /**
         声明交换机 String exchange, BuiltinExchangeType type
         参数明细
         1、交换机名称
         2、交换机类型,fanout、topic、direct、headers
         */
        channel.exchangeDeclare(exchange_routing_inform, BuiltinExchangeType.DIRECT);
        channel.queueDeclare(queue_inform_email, true, false, false, null);
        channel.queueDeclare(queue_inform_sms, true, false, false, null);
        channel.queueBind(queue_inform_email, exchange_routing_inform, queue_inform_email);
        channel.queueBind(queue_inform_sms, exchange_routing_inform, queue_inform_sms);
        for (int i = 0; i < 10; i++) {
            String message = "email inform to user" + i;
            channel.basicPublish(exchange_routing_inform, queue_inform_email, null,
                    message.getBytes());
            System.out.println("Send Message is:'" + message + "'");
        }
        for (int i = 0; i < 10; i++) {
            String message = "sms inform to user" + i;
            channel.basicPublish(exchange_routing_inform, queue_inform_sms, null, message.getBytes());
            System.out.println("Send Message is:'" + message + "'");
        }
    }
}
  • 在net.wanho.rabbitmq.routing创建Consumer1.java
package net.wanho.rabbitmq.routing;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer1 {
    public static void main(String[] args) throws Exception {
        String QUEUE_INFORM_EMAIL = "queue_inform_email";
        String EXCHANGE_ROUTING_INFORM = "inform_exchange_routing";
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_ROUTING_INFORM, BuiltinExchangeType.DIRECT);
        channel.queueDeclare(QUEUE_INFORM_EMAIL, true, false, false, null);
        channel.queueBind(QUEUE_INFORM_EMAIL, EXCHANGE_ROUTING_INFORM, QUEUE_INFORM_EMAIL);
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                long deliveryTag = envelope.getDeliveryTag();
                String exchange = envelope.getExchange();
                String message = new String(body, "utf-8");
                System.out.println(message);

            }
        };
        channel.basicConsume(QUEUE_INFORM_EMAIL, true, defaultConsumer);
    }
}
  • 在net.wanho.rabbitmq.routing创建Consumer2.java
package net.wanho.rabbitmq.routing;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer2 {
    public static void main(String[] args) throws Exception {
        String QUEUE_INFORM_SMS = "queue_inform_sms";
        String EXCHANGE_ROUTING_INFORM = "inform_exchange_routing";
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_ROUTING_INFORM, BuiltinExchangeType.DIRECT);
        channel.queueDeclare(QUEUE_INFORM_SMS, true, false, false, null);
        channel.queueBind(QUEUE_INFORM_SMS, EXCHANGE_ROUTING_INFORM, QUEUE_INFORM_SMS);
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                long deliveryTag = envelope.getDeliveryTag();
                String exchange = envelope.getExchange();
                String message = new String(body, "utf-8");
                System.out.println(message);

            }
        };
        channel.basicConsume(QUEUE_INFORM_SMS, true, defaultConsumer);
    }
}
  • 测试
    启动Consumer1两次,启动Consumer2一次,启动Producer,我们会看到属于consumer1的消息会被两个consumer1实例合起来消费,属于consumer2的消息会被consumer2全部消费。
    以上就是我们完成的rabbitmq的Routing模式的测试。

猜你喜欢

转载自www.cnblogs.com/alichengxuyuan/p/12513558.html
05