RabbitMQ四种Exchange类型之Direct (Java)

本文介绍如何使用 Java 实现 RabbitMQ 中 Direct 类型的 Exchange,并通过具体代码示例展示了如何配置 Producer 和 Consumer 来精确匹配路由键,实现消息传递。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的个人博客网站云诺说上线啦!所有文章都搬到新地址了,点击围观吧!

Direct类型的Exchange是不处理路由键,需要将一个队列绑定到交换机上,要求该消息与一个特定的路由键完全匹配。这是一个完整的匹配。如果一个队列绑定到该交换机上要求路由键为 “logs”,则只有路由键为“logs”的消息才被转发,不会转发路由键为"logs.error",只会转发路由键为"logs"。 

如下图:

 

 

代码实现如下!!

Consumer:

package direct;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

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 DirectConsumer {
	private static final String EXCHANGE_NAME 	= "exchange_direct";
	public static void main(String[] argv) throws IOException, TimeoutException  {
		
		new ExchangeDirect("logs.info");
		new ExchangeDirect("logs.error");
		
	}

	static class ExchangeDirect{
		public  ExchangeDirect(String routingKey) throws IOException, TimeoutException {
			ConnectionFactory factory = new ConnectionFactory();
			//rabbitmq监听IP
			factory.setHost("192.168.249.128");
			//rabbitmq监听默认端口
			factory.setPort(5672);
			//设置访问的用户
			factory.setUsername("test");
			factory.setPassword("test");
			Connection connection = factory.newConnection();
			Channel channel = connection.createChannel();
			//声明路由名字和类型
			channel.exchangeDeclare(EXCHANGE_NAME, "direct", false, true, null);
			//队列名称
			String queueName = routingKey + ".queue";
			//创建队列
			channel.queueDeclare(queueName, false, false, true, null);
			//把队列绑定到路由上
			channel.queueBind(queueName, EXCHANGE_NAME, routingKey);

			System.out.println(" [routingKey = "+ routingKey +"] Waiting for msg....");

			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("[routingKey = "+ envelope.getRoutingKey() +"] Received msg is '" + message + "'");
				}
			};
			channel.basicConsume(queueName, true, consumer);
		}

	}

}

Producer:

package direct;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

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

public class DirectProducer {

	private static final String EXCHANGE_NAME = "exchange_direct";

	public static void main(String[] argv) throws Exception{
		new ExchangeDirect("logs.info", "logs Info test !!");
		new ExchangeDirect("logs.error", "logs error test !!");
		new ExchangeDirect("logs.warning", "logs warning test !!");
	}
	
	static class ExchangeDirect{
		public ExchangeDirect(String routingKey,String message) throws IOException, TimeoutException{
			ConnectionFactory factory = new ConnectionFactory();
			//rabbitmq监听IP
			factory.setHost("192.168.249.128");
			//rabbitmq监听默认端口
			factory.setPort(5672);
			//设置访问的用户
			factory.setUsername("test");
			factory.setPassword("test");
			Connection connection = factory.newConnection();
			Channel channel = connection.createChannel();

			//声明路由名字和类型
			channel.exchangeDeclare(EXCHANGE_NAME, "direct", false, true, null);
			
			channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
			System.out.println("[routingKey = "+routingKey+"] Sent msg is '" + message + "'");

			channel.close();
			connection.close();
			
		}
		
	}


}

运行结果:

 

注意这里的queueName不能通过 channel.queueDeclare().getQueue() 来随机生成!!

 

祝生活愉快!!!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值