前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >订阅模式 - fanout

订阅模式 - fanout

作者头像
用户5927264
发布2019-08-01 10:22:52
4710
发布2019-08-01 10:22:52
举报
文章被收录于专栏:OSChinaOSChina
代码语言:javascript
复制
package com.shi.ps;

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

import org.junit.Test;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;
import com.rabbitmq.client.ShutdownSignalException;
import com.shi.util.RabbitMqUtils;

/**
 * 订阅模式
 * @author SHF
 * @version 创建时间:2018年7月4日  下午2:43:50
 */
public class PublishSubscribe {
	
	//交换机名称
	private final static String EXCHANGE_NAME = "exchange_fanout";
	
	//队列名称
	private final static String QUEUE_1 ="exchange_queue_1";
	private final static String QUEUE_2 ="exchange_queue_2";
	
	/**
	 * 生产者  - 订阅模式
	 * @author SHF
	 * @version 创建时间:2018年7月4日  下午2:59:42
	 * @throws TimeoutException 
	 * @throws IOException 
	 */
	@Test
	public void send() throws IOException, TimeoutException {
		//1  获取链接以及mq通道
		  Connection connection = RabbitMqUtils.getConnection();
		  Channel channel = connection.createChannel();
		  
		//2  申明exchange (交换机)  "fanout":交换机的类型
		 channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
		  
		//3 消息内容
		 String message = "施爷 exchange_fanout  来消息了....";
		 channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
		 
		//4 关闭通道及连接
		 channel.close();
		 connection.close();
	}
	
	/**
	 * 消费者1 - 订阅模式
	 * @author SHF
	 * @version 创建时间:2018年7月4日  下午3:12:31
	 * @throws TimeoutException 
	 * @throws IOException 
	 * @throws InterruptedException 
	 * @throws ConsumerCancelledException 
	 * @throws ShutdownSignalException 
	 */
	@Test
	public void reic1() throws IOException, TimeoutException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {
		//1  获取链接 及mq通道
		Connection connection = RabbitMqUtils.getConnection();
		Channel channel = connection.createChannel();
		
		//2  申明队列
		channel.queueDeclare(QUEUE_1, false, false, false, null);
		
		//3  绑定队列到交换机
		channel.queueBind(QUEUE_1, EXCHANGE_NAME, "");
		
		//4  同一时刻服务器只会发送一条消息给消费者
		channel.basicQos(1);
		
		//5  定义队列的消费者
		QueueingConsumer consumer = new QueueingConsumer(channel);
		
		//6  监听队列,手动返回完成(false)
		channel.basicConsume(QUEUE_1, false,consumer);
		
		//7  获取消息
		while(true) {
			Delivery delivery = consumer.nextDelivery();
			String message = new String(delivery.getBody());
			System.out.println(" x :exchange_queue_1 接收到消息:" + message);
			Thread.sleep(10);
			
			//手动返回  告诉队列消息已经成功接收
			channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
		}
	}
	
	/**
	 * 消费者2 - 订阅模式
	 * @author SHF
	 * @version 创建时间:2018年7月4日  下午3:37:49
	 * @throws TimeoutException 
	 * @throws IOException 
	 * @throws InterruptedException 
	 * @throws ConsumerCancelledException 
	 * @throws ShutdownSignalException 
	 */
	@Test
	public void reiv2 () throws IOException, TimeoutException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {
		//1   获取链接 借mq通道
		Connection connection = RabbitMqUtils.getConnection();
		Channel channel = connection.createChannel();
		
		//2 申明队列
		channel.queueDeclare(QUEUE_2, false, false, false, null);
		
		//3 绑定队列到交换机
		channel.queueBind(QUEUE_2, EXCHANGE_NAME, "");
		
		//4 同一时刻服务器只发送一条消息给消费者
		channel.basicQos(1);
		
		//5 定义队列的消费者
		QueueingConsumer consumer = new QueueingConsumer(channel);
		
		//6 监听队列,手动返回完成(false:表示需要手动完成)
		channel.basicConsume(QUEUE_2, false, consumer);
		
		//7 获取消息
		while(true) {
			Delivery delivery = consumer.nextDelivery();
			String message = new String(delivery.getBody());
			System.out.println(" x :exchange_queue_2  接收到消息:" + message);
			Thread.sleep(10);
			
			//手动返回  告诉队列消息已经成功接收
			channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
		}
	}
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档