消息中间件RabbitMQ简单队列及工作队列

一.下载安装rabbitMQ
详情见此博客
二.加入rabbitmq的依赖`


		    com.rabbitmq
		    amqp-client
		    5.6.0
		

三.简单队列,一个生产者对应一个消费者
消息中间件RabbitMQ简单队列及工作队列_第1张图片
1.获取rabbitMQ的连接

import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class ConnectionUtil {
	public static Connection getConnection() throws IOException, TimeoutException{
		Connection conn=null;
		ConnectionFactory factory=new ConnectionFactory();
		//主机IP
		factory.setHost("127.0.0.1");
		//虚拟host,类似于数据库
		factory.setVirtualHost("/vhost");
		//端口
		factory.setPort(5672);
		//用户名
		factory.setUsername("chm");
		//密码
		factory.setPassword("123456");
		conn=factory.newConnection();
		return conn;
	}
}

2.生产者

import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.yc.rabbit.util.ConnectionUtil;

public class Send {
	//队列名字
	private final static String QUEUE_NAME = "test_queue_simple";

	public static void main(String[] args) throws IOException, TimeoutException {
		//获取连接
		Connection conn = ConnectionUtil.getConnection();
		Channel channel = conn.createChannel();
		//channel.queueDeclare(queue, durable, exclusive, autoDelete, arguments)
		//声明队列
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		//定义消息
		String msg = "simple queue";
		//发布消息
		channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
		System.out.println("send msg"+msg);
		channel.close();
		conn.close();
	}
}

3.消费者

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

import com.rabbitmq.client.AMQP.BasicProperties;
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;
import com.yc.rabbit.util.ConnectionUtil;

public class Recv {
	//队列名,与生产者定义的队列名一致
	private final static String QUEUE_NAME = "test_queue_simple";
	public static void main(String[] args) throws IOException, TimeoutException {
		Connection conn=ConnectionUtil.getConnection();
		Channel channel = conn.createChannel();
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		Consumer consumer=new DefaultConsumer(channel){

			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				String msg=new String(body,"UTF-8");
				System.out.println("recv msg:"+msg);
			}
			
		};
		channel.basicConsume(QUEUE_NAME, true, consumer);
	}
}

四.工作队列模式
消息中间件RabbitMQ简单队列及工作队列_第2张图片
一个生产者对应多个消费者,但是一个消息只能被一个消费者消费。
工作队列模式又分为轮循发布和公平分发。

轮循发布(消费者1休眠100ms,消费者2休眠200ms)

1.生产者

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

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.yc.rabbit.util.ConnectionUtil;

public class Send {
	private final static String QUEUE_NAME = "test_queue_round";

	public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
		Connection conn = ConnectionUtil.getConnection();
		Channel channel = conn.createChannel();
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		for(int i=1;i<=50;i++){
			String msg = "round queue"+i;
			channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
			System.out.println("send msg"+msg);
			Thread.sleep(10);
		}
		channel.close();
		conn.close();
	}
}
片

2.消费者1

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

import com.rabbitmq.client.AMQP.BasicProperties;
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;
import com.yc.rabbit.util.ConnectionUtil;

public class Recv1 {
	private final static String QUEUE_NAME = "test_queue_round";
	public static void main(String[] args) throws IOException, TimeoutException {
		Connection conn=ConnectionUtil.getConnection();
		Channel channel=conn.createChannel();
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		Consumer consumer=new DefaultConsumer(channel){

			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				String msg=new String(body,"UTF-8");
				System.out.println("Recv1 msg"+msg);
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					System.out.println("recv1"+msg+"done");
				}
			}
			
		};
		channel.basicConsume(QUEUE_NAME, true,consumer);
	}
}

3.消费者2

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

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;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.yc.rabbit.util.ConnectionUtil;

public class Recv2 {
	private final static String QUEUE_NAME = "test_queue_round";
	public static void main(String[] args) throws IOException, TimeoutException {
		Connection conn=ConnectionUtil.getConnection();
		Channel channel=conn.createChannel();
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		Consumer consumer=new DefaultConsumer(channel){

			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				String msg=new String(body,"UTF-8");
				System.out.println("Recv2 msg"+msg);
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					System.out.println("recv2"+msg+"done");
				}
			}
			
		};
		channel.basicConsume(QUEUE_NAME, true,consumer);
	}
}

运行结果
消息中间件RabbitMQ简单队列及工作队列_第3张图片
消息中间件RabbitMQ简单队列及工作队列_第4张图片
分析:
消费者1和消费者2获取到的消息内容是不同的,也就是说同一个消息只能被一个消费者获取。
消费者1和消费者2分别获取奇数条消息和偶数条消息,两种获取消息的条数是一样的。

公平分发
1.生产者

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

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.yc.rabbit.util.ConnectionUtil;

public class Send {
	private final static String QUEUE_NAME = "test_queue_fair";

	public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
		Connection conn = ConnectionUtil.getConnection();
		Channel channel = conn.createChannel();
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		for(int i=1;i<=50;i++){
			String msg = "round queue"+i;
			channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
			System.out.println("send msg"+msg);
			Thread.sleep(10);
		}
		channel.close();
		conn.close();
	}
}

2.消费者1

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

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;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.yc.rabbit.util.ConnectionUtil;

public class Recv1 {
	private final static String QUEUE_NAME = "test_queue_fair";
	public static void main(String[] args) throws IOException, TimeoutException {
		Connection conn=ConnectionUtil.getConnection();
		Channel channel=conn.createChannel();
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		//在preCount条消息被确认消费前,不会再向其发送消息
		int preCount=1;
		channel.basicQos(preCount);
		Consumer consumer=new DefaultConsumer(channel){

			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				String msg=new String(body,"UTF-8");
				System.out.println("Recv1 msg"+msg);
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					System.out.println("recv1"+msg+"done");
					/**
					 * 手动回执,返回应答状态
					 * 告知MQ消息已经消费,可以发送新的消息了
					 * 第二个参数为false表示只是这一条消息,为true表示tag值比第一个参数的值小的所有消息
					 */
					channel.basicAck(envelope.getDeliveryTag(), false);
				}
			}
			
		};
		boolean autoAck=false;//自动应答设置为false
		channel.basicConsume(QUEUE_NAME, autoAck,consumer);
	}
}

3.消费者2

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

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;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.yc.rabbit.util.ConnectionUtil;

public class Recv2 {
	private final static String QUEUE_NAME = "test_queue_fair";
	public static void main(String[] args) throws IOException, TimeoutException {
		Connection conn=ConnectionUtil.getConnection();
		Channel channel=conn.createChannel();
		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		//在preCount条消息被确认消费前,不会再向其发送消息
		int preCount=1;
		channel.basicQos(preCount);
		Consumer consumer=new DefaultConsumer(channel){

			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
				String msg=new String(body,"UTF-8");
				System.out.println("Recv2 msg"+msg);
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					System.out.println("recv2"+msg+"done");
					/**
					 * 手动回执
					 * 告知MQ消息已经消费,可以发送新的消息了
					 * 第二个参数为false表示只是这一条消息,为true表示tag值比第一个参数的值小的所有消息
					 */
					channel.basicAck(envelope.getDeliveryTag(), false);
				}
			}
			
		};
		boolean autoAck=false;//自动应答设置为false
		channel.basicConsume(QUEUE_NAME, autoAck,consumer);
	}
}

消息中间件RabbitMQ简单队列及工作队列_第5张图片
消息中间件RabbitMQ简单队列及工作队列_第6张图片
分析:
消费者1和消费者2获取到的消息内容是不同的,也就是说同一个消息只能被一个消费者获取。
消费者1获得的消息比消费者2多。

你可能感兴趣的:(JAVA,EE)