work queues 模式

本文介绍了RabbitMQ中两种消费者处理消息的工作队列模式:默认的轮询模式和公平模式。在轮询模式下,消息会平均分配给多个消费者。而在公平模式中,消费者按处理速度获取消息,处理慢的消费者接收消息较少,处理快的消费者接收消息较多。通过示例代码展示了如何配置和实现这两种模式。
摘要由CSDN通过智能技术生成

工作队列模式:默认是轮询(你1 我1 执行)
在这里插入图片描述
轮询:
生产者:

package Task.lunxun;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import utils.RabbitMqUtils;
import java.io.IOException;
public class Producer {
    public static void main(String[] args) {
        Connection connection = RabbitMqUtils.getConnection();
        Channel channel = null;
        try {
            channel = connection.createChannel();
            channel.queueDeclare("queue1", false, false, false, null);//有就绑定,没有就创建
            for (int i = 1; i <= 10; i++) {
                channel.basicPublish("", "queue1", null, ("hello rabbit" + i).getBytes());
            }
            System.out.println("消息发送成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            RabbitMqUtils.closeConnectionAndChannel(channel, connection);
        }
    }
}

多个消费者:
消费者1:

package Task.lunxun;
import com.rabbitmq.client.*;
import utils.RabbitMqUtils;
import java.io.IOException;
public class Consumer1 {
    public static void main(String[] args) {
        Connection connection = RabbitMqUtils.getConnection();
        Channel channel = null;
        try {
            channel = connection.createChannel();
            channel.queueDeclare("queue1", false, false, false, null);//有就绑定,没有就创建
            channel.basicConsume("queue1", true, new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("消费者2" + new String(body));
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }
}

消费者2:

package Task.lunxun;
import com.rabbitmq.client.*;
import utils.RabbitMqUtils;
import java.io.IOException;
public class Consumer2 {
    public static void main(String[] args) {
        Connection connection = RabbitMqUtils.getConnection();
        Channel channel = null;
        try {
            channel = connection.createChannel();
            channel.queueDeclare("queue1", false, false, false, null);//有就绑定,没有就创建
            channel.basicConsume("queue1", true, new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("消费者1" + new String(body));
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }
}

在这里插入图片描述
在这里插入图片描述
公平模式(慢就少执行,快就多执行):
生产者:

package Task.fair;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import utils.RabbitMqUtils;
import java.io.IOException;
public class Producer {
    public static void main(String[] args) {
        Connection connection = RabbitMqUtils.getConnection();
        Channel channel =null;
        try {
            channel = connection.createChannel();
            channel.queueDeclare("queue1",false,false,false,null);
            for (int i = 1; i <=10 ; i++) {
                channel.basicPublish("","queue1",null,("hello rabbit"+i).getBytes());
            }
            System.out.println("消息发送成功");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            RabbitMqUtils.closeConnectionAndChannel(channel,connection);
        }
    }
}

多个消费者:
消费者1:

package Task.fair;
import com.rabbitmq.client.*;
import utils.RabbitMqUtils;
import java.io.IOException;
public class Consumer1 {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitMqUtils.getConnection();
        final Channel channel = connection.createChannel();
        try {
            channel.queueDeclare("queue1", false, false, false, null);
            channel.basicQos(1);//只一条一条的读
            channel.basicConsume("queue1", false, new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("消费者2" + new String(body));
                    channel.basicAck(envelope.getDeliveryTag(), false);//1.确认队列中那个具体的消息 2.是否开启多个消息同时确认 false:手动确认
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }
}

消费者2:

package Task.fair;
import com.rabbitmq.client.*;
import utils.RabbitMqUtils;
import java.io.IOException;
public class Consumer2 {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitMqUtils.getConnection();
        final Channel channel = connection.createChannel();
        try {
            channel.queueDeclare("queue1", false, false, false, null);
            channel.basicQos(1);//一次只消费一条
            channel.basicConsume("queue1", false, new DefaultConsumer(channel) {//必须关闭消息自动确认机制
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("消费者1" + new String(body));
                    //1.确认队列中那个具体的消息 2.是否开启多个消息同时确认 false:手动确认
                    channel.basicAck(envelope.getDeliveryTag(), false);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }
}

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值