-
RabbitMQ 工作队列:
- 默认的传统队列是为均摊消费,存在不公平性;如果每个消费者速度不一样的情况下,均摊消费是不公平的,应该是能者多劳
- 采用工作队列模式:
- 在通道中只需要设置 baseicQos 的值即可
- 表示 MQ 服务器每次只会给消费者推送 n 条消息
- 必须手动应答之后才会继续发送
- 在通道中只需要设置 baseicQos 的值即可
- 生产者:
public class ProducerFanout { private static final String QUEUE_NAME = "BoyatopMamber"; public static void main(String[] args) throws IOException, TimeoutException, InterruptedException { // 1.创建新的连接 Connection connection = RabbitMQConnection.getConnection(); // 2.设置 channel Channel channel = connection.createChannel(); for (int i = 0; i < 10; i++) { // 3.发送消息 String msg = "Hello my Bro" + i; channel.confirmSelect(); channel.basicPublish("",QUEUE_NAME,null,msg.getBytes()); boolean result = channel.waitForConfirms(); } // 4.关闭资源 channel.close(); connection.close(); } }
- 消费者1:
public class Consumer1 { //定义队列 private static final String QUEUE_NAME = "BoyatopMamber"; public static void main(String[] args) throws IOException, TimeoutException { //创建连接 Connection connection = RabbitMQConnection.getConnection(); //创建通道 final Channel channel = connection.createChannel(); channel.basicQos(1); DefaultConsumer defaultConsumer = new DefaultConsumer(channel){ @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String msg = new String(body,"UTF-8"); System.out.println("用户1:" + msg); channel.basicAck(envelope.getDeliveryTag(),false); } }; //监听消息 channel.
- 默认的传统队列是为均摊消费,存在不公平性;如果每个消费者速度不一样的情况下,均摊消费是不公平的,应该是能者多劳
RabbitMQ深度探索:五种消息模式
最新推荐文章于 2025-02-21 14:47:37 发布