{"error":400,"message":"over quota"} RabbitMQ四种Exchange类型之Direct (Java)——云诺说
云诺说 - 小程序开发 - 软件定制
当前位置: RabbitMQ > RabbitMQ四种Exchange类型之Direct (Java)

RabbitMQ四种Exchange类型之Direct (Java)

2019-07-04 08:35 分类:RabbitMQ 作者:云诺 阅读(1313)

版权声明:本文为博主原创文章,如果转载请给出原文链接:http://doofuu.com/article/4156152.html

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

如下图:

QQ截图20190704200534.png

下面是代码实现!!!

消费者:

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);
		}
 
	}
 
}

生产者:

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();
			
		}
		
	}
 
}

运行结果:

QQ截图20190704203459.png

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

 

祝生活愉快!

「创作不易,你的支持是本站持续更新最大的动力!」

赞(0) 打赏

谢谢你请我喝奶茶*^_^*

支付宝
微信
1

谢谢你请我喝奶茶*^_^*

支付宝
微信

上一篇:

下一篇:

共有 0 条评论 - RabbitMQ四种Exchange类型之Direct (Java)

博客简介

云诺说是一个致力于分享互联网编程技术交流、小程序开发、小程序源码分享、软件服务定制和生活记录的技术服务型学习博客网站。

微信 :LGY178888

职业 :小程序开发、软件定制

现居 :广东省-广州市-天河区

友情链接

欢迎与本博客交换友情链接,本博客对交换链接的网站没有要求。如果您是本博客的友情链接网站,在遇到网站运行问题时,可以随时联系,我们将免费提供技术类支持! 申请交换友链

站点统计

  • 文章总数:155 篇
  • 草稿数目:0 篇
  • 分类数目:14 个
  • 独立页面:165 个
  • 评论总数:0 条
  • 访问总量: 586133次
  • 最近更新:2024年04月28日