栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

RabbitMQ work queues

RabbitMQ work queues

多消费者消费模式
消费通道每次获取一个消息:channel.basicQos(1);
channel.basicConsume(“msg”,false,new DefaultConsumer(channel){
autoAck 关闭自动确认消息
获取消息后手动确认

生产者
package wordqueues;

import com.jia.utils.RabbitMQUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;
import org.junit.Test;

import java.io.IOException;

public class Provider {

    @Test
    public void sendMsg() throws IOException {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("msg",false,false,false,null);
        for (int i = 0; i < 200; i++) {
            channel.basicPublish("","msg", MessageProperties.PERSISTENT_TEXT_PLAIN,("消息"+i).getBytes());
        }
        RabbitMQUtils.closeConnectAndChanel(channel,connection);
    }
}

消费者
package com.jia.wordqueues;

import com.jia.utils.RabbitMQUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer1 {
    public static void main(String[] args) throws IOException {
        Connection connection = RabbitMQUtils.getConnection();
        final Channel channel = connection.createChannel();
        //通道只取一个消息
        channel.basicQos(1);
        channel.queueDeclare("msg",false,false,false,null);
        channel.basicConsume("msg",false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body));
                //手动确认消息标识  multiple 是否确认多个 false 每次确认一个
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });

    }
}

package com.jia.wordqueues;

import com.jia.utils.RabbitMQUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer2 {
    public static void main(String[] args) throws IOException {
        //获取连接
        Connection connection = RabbitMQUtils.getConnection();
        final Channel channel = connection.createChannel();
        //通道只取一个消息
        channel.basicQos(1);
        //队列声明
        channel.queueDeclare("msg",false,false,false,null);
        channel.basicConsume("msg",false,new DefaultConsumer(channel){

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(new String(body));
                //手动确认消息标识  multiple 是否确认多个 false 每次确认一个
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/700046.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号