RabbitMQ work模式

一个生产者,一个默认的交换机,一个队列,两个消费者

 

public class WorkTest {

    @Test
    public void pubTest() throws IOException {//发送消息
        Connection connection = MQFactory.getConnection();//建立连接

        Channel channel = connection.createChannel();//创建信道对象



        for (int i = 0; i < 10; i++) {
            channel.basicPublish("","work_queue",null,("hello work:"+i).getBytes());//发送消息
        }


    }

    @Test
    public void conTest1() throws IOException {//接收消息
        Connection connection = MQFactory.getConnection();

        Channel channel = connection.createChannel();

        channel.queueDeclare("work_queue",true,false,false,null);//创建队列

        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {//处理消息的一个回调
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                String mag = new String(body, "utf-8");
                System.out.println("消费者1:"+mag);

            }
        };

        channel.basicConsume("work_queue",true,defaultConsumer);//消费者的消费方法

        System.in.read();

    }

    @Test
    public void conTest2() throws IOException {//接收消息
        Connection connection = MQFactory.getConnection();

        Channel channel = connection.createChannel();


        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {//处理消息的一个回调
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                String mag = new String(body, "utf-8");
                System.out.println("消费者2:"+mag);

            }
        };

        channel.basicConsume("work_queue",true,defaultConsumer);//消费者的消费方法

        System.in.read();


    }
}

 

 

这时 两台消费者拿到的是均分的

只需要在消费者端,添加Qos能力以及更改为手动ack即可让消费者,根据自己的能力去消费指定的消息,而不是默认情况下由RabbitMQ平均分配了,生产者不变,正常发布消息到默认的exchange,并指定routing消费者指定Qoa和手动ack

public class WorkTest {

    @Test
    public void pubTest() throws IOException {//发送消息
        Connection connection = MQFactory.getConnection();//建立连接

        Channel channel = connection.createChannel();//创建信道对象



        for (int i = 0; i < 10; i++) {
            channel.basicPublish("","work_queue",null,("hello work:"+i).getBytes());//发送消息
        }


    }

    @Test
    public void conTest1() throws IOException {//接收消息
        Connection connection = MQFactory.getConnection();

        Channel channel = connection.createChannel();

        channel.queueDeclare("work_queue",true,false,false,null);//创建队列

        channel.basicQos(1);

        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {//处理消息的一个回调
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                String mag = new String(body, "utf-8");
                System.out.println("消费者1:"+mag);

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                channel.basicAck(envelope.getDeliveryTag(),false);

            }
        };

        channel.basicConsume("work_queue",false,defaultConsumer);//消费者的消费方法

        System.in.read();

    }

    @Test
    public void conTest2() throws IOException {//接收消息
        Connection connection = MQFactory.getConnection();

        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 mag = new String(body, "utf-8");
                System.out.println("消费者2:"+mag);
                  //手动ack
                //envelope.getDeliveryTag 消息的标识  false 是否批量操作
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };

        channel.basicConsume("work_queue",false,defaultConsumer);//消费者的消费方法

        System.in.read();


    }
}

 

手动ack  跟设置当前消费者一次消费多少消息  就可以看自己性能接收消息

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值