RabbitMQ:路由队列

路由队列

  • 1.创建Maven项目
  • 2.导入rabbitmq依赖
  • 3.路由队列-生产者Send
  • 4.路由队列-消费者Recv
  • 5.运行Recv01和Recv02

RabbitMQ:路由队列_第1张图片
说明:error两个队列都走,info只走有info连接的队列

1.创建Maven项目

项目目录
RabbitMQ:路由队列_第2张图片

2.导入rabbitmq依赖


<dependency>
  <groupId>com.rabbitmqgroupId>
  <artifactId>amqp-clientartifactId>
  <version>5.7.3version>
dependency>

3.路由队列-生产者Send

break改成green,只走Recv02

/**
 * 路由队列-生产者
 */
public class Send {

    // 定义交换机名称
    private final static String EXCHANGE_NAME = "exchange_direct";

    public static void main(String[] argv) throws Exception {
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 连接工厂配置
        factory.setHost("192.168.68.152");
        factory.setPort(5672);
        factory.setUsername("lwx");
        factory.setPassword("lwx");
        factory.setVirtualHost("/lwx");
        // 创建连接
        try(Connection connection = factory.newConnection();    //Connection间接继承了AutoCloseable接口,可以不用手动关闭连接
            // 创建信道
            Channel channel = connection.createChannel()) {
            /**
             * 绑定交换机
             * 1.交换机名称
             * 2.交换机类型,fanout就是广播
             */
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
            String message = "Hello World!";
            // 发送消息
            channel.basicPublish(EXCHANGE_NAME,"black",null,message.getBytes(StandardCharsets.UTF_8));
            System.out.println("[x]Sent'" + message + "'");
        }
    }
}

4.路由队列-消费者Recv

编写Recv01,复制Recv01改名为Recv02
其中Recv02orange改成green

/**
 * 路由队列-消费者
 */
public class Recv01 {
    // 定义交换机名称
    private final static String EXCHANGE_NAME = "exchange_direct";

    public static void main(String[] argv) throws Exception {
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 连接工厂配置
        factory.setHost("192.168.68.152");
        factory.setPort(5672);
        factory.setUsername("lwx");
        factory.setPassword("lwx");
        factory.setVirtualHost("/lwx");
        // 创建连接
        Connection connection = factory.newConnection();    //Connection间接继承了AutoCloseable接口,可以不用手动关闭连接
        // 创建信道
        Channel channel = connection.createChannel();
        /**
         * 绑定交换机
         * 1.交换机名称
         * 2.交换机类型,fanout就是广播
         */
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        // 声明队列,排他
        String queueName = channel.queueDeclare().getQueue();
        // 队列和交换机绑定
        channel.queueBind(queueName, EXCHANGE_NAME, "black");
        channel.queueBind(queueName, EXCHANGE_NAME, "orange");
        System.out.println("[*] Waiting for messages. To exit press CTRL+C");
        // 打印消息
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println("[x] Received '" + message + "'");
        };
        /**
         * 消费消息
         * 1.队列名称
         * 2.自动确认
         */
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {

        });
    }
}

5.运行Recv01和Recv02

RabbitMQ:路由队列_第3张图片
RabbitMQ:路由队列_第4张图片

你可能感兴趣的:(RabbitMQ,rabbitmq,java,分布式)