2.工作队列

work queues

为什么会出现工作队列?
Simple队列是一一对应的,而且我们实际开发,生产者发送消息是毫无费力的,而消费者一般是要跟业务相结合的,消费者接收到消息之后就需要处理,可能需要花费时间,这时候队列就会挤压了很多消息.

1.轮询分发     
消费者1和消费者2处理的消息是一样多的.
消费者1:偶数,消费者2:奇数
这种方式叫做轮询分发(round-robin),任务总是均分.不管谁忙谁清闲.

2.公平分发
消费者2比消费者1多处理.
能者多劳

项目搭建(轮询分发(round-robin))

package com.mmr.rabbit.work;

import com.mmr.rabbit.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class Send {
	
	private static final String QUEUE_NAME="test_work_queue";
	
	public static void main(String[] args) throws Exception {
		//获取连接
		Connection connection = ConnectionUtils.getConnection();
		//获取channel
		Channel channel = connection.createChannel();
		//声明队列
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		for (int i = 0; i < 50; i++) {
			String msg = "hello"+i;
			channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
			System.err.println("WQ:"+msg);
			Thread.sleep(i*20);
		}
		channel.close();
		connection.close();
	}
	
}


package com.mmr.rabbit.work;

import java.io.IOException;

import com.mmr.rabbit.util.ConnectionUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class Recv1 {
	
	private static final String QUEUE_NAME="test_work_queue";
	
	public static void main(String[] args) throws Exception {
		//获取连接
		Connection connection = ConnectionUtils.getConnection();
		//获取channel
		Channel channel = connection.createChannel();
		//声明队列
		channel.queueDeclare(QUEUE_NAME,false , false, false, null);
		//定义一个消费者
		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.err.println("[1] Recv:" + message + "");
			    try{
			    	Thread.sleep(2000);
			    }catch(Exception e){
			    	e.printStackTrace();
			    }finally{
			    	System.err.println("[1] done");
			    }
			  }
		};
		boolean autoAck = true;
		channel.basicConsume(QUEUE_NAME,autoAck,consumer);
	}
	
}


package com.mmr.rabbit.work;

import java.io.IOException;

import com.mmr.rabbit.util.ConnectionUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class Recv2 {
	
	private static final String QUEUE_NAME="test_work_queue";
	
	public static void main(String[] args) throws Exception {
		//获取连接
		Connection connection = ConnectionUtils.getConnection();
		//获取channel
		Channel channel = connection.createChannel();
		//声明队列
		channel.queueDeclare(QUEUE_NAME,false , false, false, null);
		//定义一个消费者
		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.err.println("[2] Recv:" + message + "");
			    try{
			    	Thread.sleep(1000);
			    }catch(Exception e){
			    	e.printStackTrace();
			    }finally{
			    	System.err.println("[2] done");
			    }
			  }
		};
		boolean autoAck = true;
		channel.basicConsume(QUEUE_NAME,autoAck,consumer);
	}
	
}

项目搭建(公平分发(fair disaptch))

使用公平分发,必须关系自动应答ack.改为手动

package com.mmr.rabbit.workfair;

import com.mmr.rabbit.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

public class Send {
	
	private static final String QUEUE_NAME="test_work_queue";
	
	public static void main(String[] args) throws Exception {
		//获取连接
		Connection connection = ConnectionUtils.getConnection();
		//获取channel
		Channel channel = connection.createChannel();
		//声明队列
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		//每个消费者发送确认消息之前,消息队列不发送下一个消息到消费者,一次只处理一个消息.
		int prefetchCount = 1;
		channel.basicQos(prefetchCount);
		
		for (int i = 0; i < 50; i++) {
			String msg = "hello"+i;
			channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
			System.err.println("WQ:"+msg);
			Thread.sleep(i*20);
		}
		channel.close();
		connection.close();
	}
	
}


package com.mmr.rabbit.workfair;

import java.io.IOException;

import com.mmr.rabbit.util.ConnectionUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class Recv1 {
	
	private static final String QUEUE_NAME="test_work_queue";
	
	public static void main(String[] args) throws Exception {
		//获取连接
		Connection connection = ConnectionUtils.getConnection();
		//获取channel
		final Channel channel = connection.createChannel();
		//声明队列
		channel.queueDeclare(QUEUE_NAME,false , false, false, null);
		//保证每次只分发一个
		channel.basicQos(1);
		
		//定义一个消费者
		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.err.println("[1] Recv:" + message + "");
			    try{
			    	Thread.sleep(2000);
			    }catch(Exception e){
			    	e.printStackTrace();
			    }finally{
			    	System.err.println("[1] done");
			    	channel.basicAck(envelope.getDeliveryTag(), false);
			    }
			  }
		};
		boolean autoAck = false;//自动应答
		channel.basicConsume(QUEUE_NAME,autoAck,consumer);
	}
	
}


package com.mmr.rabbit.workfair;

import java.io.IOException;

import com.mmr.rabbit.util.ConnectionUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class Recv2 {
	
	private static final String QUEUE_NAME="test_work_queue";
	
	public static void main(String[] args) throws Exception {
		//获取连接
		Connection connection = ConnectionUtils.getConnection();
		//获取channel
		final Channel channel = connection.createChannel();
		//声明队列
		channel.queueDeclare(QUEUE_NAME,false , false, false, null);
		
		channel.basicQos(1);
		
		//定义一个消费者
		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.err.println("[2] Recv:" + message + "");
			    try{
			    	Thread.sleep(1000);
			    }catch(Exception e){
			    	e.printStackTrace();
			    }finally{
			    	System.err.println("[2] done");
			    	channel.basicAck(envelope.getDeliveryTag(),false);
			    }
			  }
		};
		boolean autoAck = false;
		channel.basicConsume(QUEUE_NAME,autoAck,consumer);
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值