RabbitMq之Exchange的Direct模式

direct模式: 通过routingKey控制同一交换机下指定队列获得消息的一种模式
举例子代码如下
生产者

package com.six;

import com.rabbitmq.client.Channel;
import com.util.Util;

import java.util.Scanner;

public class Send {
    //交换机的名称
    public static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] args) throws  Exception{
        //连接mq
        Channel channel = Util.getChannel();
        //从控制台中接收消息
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String message = scanner.next();
            //指定info队列接收消息
            channel.basicPublish(EXCHANGE_NAME,"info",null,message.getBytes("UTF-8"));
            System.out.println("发送消息完成"+message);
        }
    }
}

info接收者

package com.six;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.util.Util;

public class Receive01 {

    public static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] args) throws Exception{
        //连接mq
        Channel channel = Util.getChannel();
        //声明一个交换机 类型为direct模式
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        //声明一个队列名字
        channel.queueDeclare("console",false,false,false,null);
        //队列绑定交换机 routingKey为info
        channel.queueBind("console",EXCHANGE_NAME,"info");


        DeliverCallback deliverCallback = (consumerTag, message) ->{
            System.out.println("info控制台打印接受的消息:"+ new String(message.getBody()));
        };
        //取消消息时的回调
        CancelCallback cancelCallback = consumerTag ->{
            System.out.println("消息消费被中断");
        };
        //接收消息
        channel.basicConsume("console",true,deliverCallback,cancelCallback);
    }
}

error接收者

package com.six;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.util.Util;

public class Receive02 {
    public static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] args) throws Exception{
        //连接mq
        Channel channel = Util.getChannel();
        //声明一个交换机 类型为direct模式
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        //声明一个队列名字
        channel.queueDeclare("disk",false,false,false,null);
        //队列绑定交换机 routingKey为error
        channel.queueBind("disk",EXCHANGE_NAME,"error");

        DeliverCallback deliverCallback = (consumerTag, message) ->{
            System.out.println("error控制台打印接受的消息:"+ new String(message.getBody()));
        };
        //取消消息时的回调
        CancelCallback cancelCallback = consumerTag ->{
            System.out.println("消息消费被中断");
        };
        //接收消息
        channel.basicConsume("disk",true,deliverCallback,cancelCallback);
    }
}

启动三个线程,指定info收消息
发消息
RabbitMq之Exchange的Direct模式_第1张图片
info接收者
RabbitMq之Exchange的Direct模式_第2张图片
error接收者
RabbitMq之Exchange的Direct模式_第3张图片
这就是通过routingKey指定队列收消息的direct模式

你可能感兴趣的:(组件,java-rabbitmq,rabbitmq,java)