其他分享
首页 > 其他分享> > Direct类型交换机

Direct类型交换机

作者:互联网

        生产者发送消息,指定消息路由key,到达direct类型交换机,direct类型交换机与队列绑定,并指定了路由key,direct交换机根据消息的路由key与绑定的路由key匹配,找到对应的队列,交换机将消息转发到队列中。

package com.tech.rabbitmq.nospring.direct;

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

import java.nio.charset.StandardCharsets;

/**
 * @author lw
 * @since 2021/10/25
 */
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.50.134");
        factory.setPort(5672);
        factory.setVirtualHost("/dev");
        factory.setUsername("tech");
        factory.setPassword("tech");
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            //绑定交换机
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
            String debugMsg = "这是订单服务的debug日志";
            String infoMsg = "这是订单服务的info日志";
            String errorMsg = "这是订单服务的error日志";
            channel.basicPublish(EXCHANGE_NAME, "debugRoutingKey", null, debugMsg.getBytes(StandardCharsets.UTF_8));
            channel.basicPublish(EXCHANGE_NAME, "infoRoutingKey", null, infoMsg.getBytes(StandardCharsets.UTF_8));
            channel.basicPublish(EXCHANGE_NAME, "errorRoutingKey", null, errorMsg.getBytes(StandardCharsets.UTF_8));
            System.out.println("消息发送成功");
        }
    }
}
package com.tech.rabbitmq.nospring.direct;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author lw
 * @since 2021/10/25
 */
public class Recv1 {
    private final static String EXCHANGE_NAME = "exchange_direct";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.50.134");
        factory.setPort(5672);
        factory.setVirtualHost("/dev");
        factory.setUsername("tech");
        factory.setPassword("tech");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //绑定交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

        String queueName = channel.queueDeclare().getQueue();
        //绑定交换机和队列,指定routingKey
        channel.queueBind(queueName, EXCHANGE_NAME, "debugRoutingKey");
        channel.queueBind(queueName, EXCHANGE_NAME, "infoRoutingKey");
        channel.queueBind(queueName, EXCHANGE_NAME, "errorRoutingKey");

        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("body=" + new String(body, "utf-8"));
                //手动确认消息,不是多条确认
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        };
        //false 关闭自动确认
        channel.basicConsume(queueName, false, consumer);
    }
}
package com.tech.rabbitmq.nospring.direct;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author lw
 * @since 2021/10/25
 */
public class Recv2 {
    private final static String EXCHANGE_NAME = "exchange_direct";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.50.134");
        factory.setPort(5672);
        factory.setVirtualHost("/dev");
        factory.setUsername("tech");
        factory.setPassword("tech");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //绑定交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

        String queueName = channel.queueDeclare().getQueue();
        //绑定交换机和队列,指定routingKey
        channel.queueBind(queueName, EXCHANGE_NAME, "errorRoutingKey");

        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("body=" + new String(body, "utf-8"));
                //手动确认消息,不是多条确认
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        };
        //false 关闭自动确认
        channel.basicConsume(queueName, false, consumer);
    }
}

 

send发送3条消息到交换机,交换机根据路由key将消息转发到匹配的队列。

Recv1指定了三个路由key,因此能收到3条消息;

Recv2指定了一个路由key,因此能收到1条消息; 

标签:String,EXCHANGE,Direct,factory,交换机,类型,channel,NAME
来源: https://blog.csdn.net/liangwenmail/article/details/121038275