RabbitMQ-工作队列

安装完 http://localhost:15672/ 登录,默认账户和密码都是guest

轮询分发(round-robin)

在这里插入图片描述
一条队列有多个消费者,当生产者发送消息到队列时,默认会平均发送到各个消费者,不考虑他们的处理能力。

package com.test.rabbitmq.work;

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

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.test.rabbitmq.util.ConnectUtils;

public class Send {
	
	private static final String QUEUE_NAME = "test_work_queue"; 
	
	public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
		Connection connection = ConnectUtils.getConnection();
		Channel channel = connection.createChannel();
		
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		for (int i = 0; i < 50; i++) {
			String msg = "hello-" + i;
			System.out.println("SEND=====" + msg);
			channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
			Thread.sleep(i*20);
		}
		channel.close();
		connection.close();
		
	}
}

package com.test.rabbitmq.work;

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

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.test.rabbitmq.util.ConnectUtils;

public class Recei1 {
	private static final String QUEUE_NAME = "test_work_queue"; 
	
	public static void main(String[] args) throws IOException, TimeoutException {
		
		
		Connection connection = ConnectUtils.getConnection();
		Channel channel = connection.createChannel();
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		
		DefaultConsumer consumer = new DefaultConsumer(channel) {
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				String msg = new String(body);
				System.out.println("RECEI=====" + msg);
				
				try {
					Thread.sleep(2*1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		
		boolean autoAck = true;
		channel.basicConsume(QUEUE_NAME,autoAck ,consumer);
	}

}

公平分发(fair dispatch)

一条队列有多个消费者,关闭自动应答模式,当消费者收到消息后发送ACK应答,生产者再发送下一条消息;

当生产者发送消息到队列时,处理速度快的消费者,处理消息多;少的,处理得少。

package com.test.rabbitmq.workfair;

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

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.test.rabbitmq.util.ConnectUtils;

public class Send {
	
	private static final String QUEUE_NAME = "test_work_queue"; 
	
	public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
		Connection connection = ConnectUtils.getConnection();
		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;
			System.out.println("SEND=====" + msg);
			channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
			Thread.sleep(i*20);
		}
		channel.close();
		connection.close();
		
	}
}

package com.test.rabbitmq.workfair;

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

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.test.rabbitmq.util.ConnectUtils;

public class Recei1 {
	private static final String QUEUE_NAME = "test_work_queue"; 
	
	public static void main(String[] args) throws IOException, TimeoutException {
		
		
		Connection connection = ConnectUtils.getConnection();
		Channel channel = connection.createChannel();
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		
		channel.basicQos(1);
		
		DefaultConsumer consumer = new DefaultConsumer(channel) {
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				String msg = new String(body);
				System.out.println("RECEI=====" + msg);
				
				try {
					Thread.sleep(2*1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					channel.basicAck(envelope.getDeliveryTag(), false);
				}
			}
		};
		
		boolean autoAck = false;
		channel.basicConsume(QUEUE_NAME,autoAck ,consumer);
	}

}

消息持久化

开启持久化,可以防止RabbitMQ或者消费者发生故障,造成消息丢失。

Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
                                 Map<String, Object> arguments) throws IOException;

第二个参数开启持久化。

发布了15 篇原创文章 · 获赞 1 · 访问量 372

猜你喜欢

转载自blog.csdn.net/qq_24819841/article/details/105072475