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

Work模式

作者头像
用户5927264
发布2019-08-01 10:22:08
3400
发布2019-08-01 10:22:08
举报
文章被收录于专栏:OSChinaOSChina

模式是一个生产者多个消费者模式,一个消息只能别一个消费者消费

代码语言:javascript
复制
package com.shi.work;

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.QueueingConsumer;
import com.shi.util.RabbitMqUtils;

/**
 * work 模式开发
 * @author SHF
 * @version 创建时间:2018年7月3日  下午5:55:20
 */
public class WorkMQTest {
	
	private final static String QUEUE_NAME = "work_queue";
	
	/**
	 * 生产者
	 * @author SHF
	 * @version 创建时间:2018年7月3日  下午5:56:41
	 * @throws TimeoutException 
	 * @throws IOException 
	 * @throws InterruptedException 
	 */
	@Test
	public void send() throws IOException, TimeoutException, InterruptedException {
		//1 获取链接及mq通道
		Connection connection = RabbitMqUtils.getConnection();
		Channel channel = connection.createChannel();
		
		//2 申明队列
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		
		//3  循环发送消息
		for(int i = 0;i < 100 ; i++) {
			//消息内容
			String message = "" + i;
			channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
			System.out.println( "[x] sent "+ message );
			
			Thread.sleep(i * 10);
		}
		channel.close();
		connection.close();
	}
	
	/**
	 * 消费者 1
	 * @author SHF
	 * @version 创建时间:2018年7月3日  下午6:09:19
	 *  @throws IOException
	 *  @throws TimeoutException
	 *  @throws InterruptedException
	 */
	@Test
	public void recv1() throws IOException, TimeoutException, InterruptedException {
		//1 获取链接及mq通道
		Connection connection = RabbitMqUtils.getConnection();
		Channel channel = connection.createChannel();
		
		//2 声明队列
		 channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		 
		//3 同一时刻服务器只会发送一条消息给消费者
		channel.basicQos(1);
		 
		//4 定义队列的消费者
		 QueueingConsumer consumer = new QueueingConsumer(channel);
		 
		//5 监听队列,手动返回完成
		 channel.basicConsume(QUEUE_NAME, false, consumer);
		 
		//6 获取消息
		while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" [x1] Received '" + message + "'");
            //休眠
            Thread.sleep(10);
            // 返回确认状态
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
	}
	
	/**
	 * 消费者2
	 * @author SHF
	 * @version 创建时间:2018年7月3日  下午6:09:46
	 *  @throws IOException
	 *  @throws TimeoutException
	 *  @throws InterruptedException
	 */
	@Test
	public void recv2() throws IOException, TimeoutException, InterruptedException {
		//1 获取链接及mq通道
		Connection connection = RabbitMqUtils.getConnection();
		Channel channel = connection.createChannel();
		
		//2 声明队列
		 channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		 
		//3 同一时刻服务器只会发送一条消息给消费者
		 channel.basicQos(1);
		 
		//4 定义队列的消费者
		 QueueingConsumer consumer = new QueueingConsumer(channel);
		 
		//5 监听队列,手动返回完成
		 channel.basicConsume(QUEUE_NAME, false, consumer);
		 
		//6 获取消息
		while(true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" [x2] Received '" + message + "'");
            //休眠
            Thread.sleep(1000);
            // 返回确认状态
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
	}
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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