RabbitMQ 教程

SenderAckDemo4.java

import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.*;

/**
 * 验证 RabbitMQ 生产者确认,异步确认
 * @author hxstrive.com 2022/3/7
 */
public class SenderAckDemo4 {
    /** 交换器名称 */
    private final String EXCHANGE_NAME = "exchange-" + SenderAckDemo4.class.getSimpleName();
    /** 队列名称 */
    private final String QUEUE_NAME = "queue-" + SenderAckDemo4.class.getSimpleName();
    /** 缓存已发送的消息 */
    private static final SortedSet<Long> MESSAGE_LIST = new TreeSet<Long>();

    public static void main(String[] args) throws Exception {
        SenderAckDemo4 demo = new SenderAckDemo4();
        demo.consumer();
        demo.sender();
    }

    /**
     * 生产者
     * @throws Exception
     */
    private void sender() throws Exception {
        // 创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        Connection connection = factory.newConnection();

        // 创建信道
        Channel channel = connection.createChannel();
        channel.basicQos(1);

        // 声明交换器
        channel.exchangeDeclare(EXCHANGE_NAME, "topic");

        // 声明队列
        channel.queueDeclare(QUEUE_NAME, true, false, false, null) ;
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "*.hxstrive.com");

        // 添加监听器
        channel.addConfirmListener(new ConfirmListener() {
            // 处理 Basic.Ack 命令
            public void handleAck(long deliveryTag, boolean multiple) throws IOException {
                System.out.println("ConfirmListener.handleAck deliveryTag=" +
                        deliveryTag + ", multiple=" + multiple);
                if (multiple) {
                    // headSet() 方法将返回小于 deliveryTag - 1 的元素
                    MESSAGE_LIST.headSet(deliveryTag - 1).clear();
                } else {
                    // 移除等于 deliveryTag 的元素
                    MESSAGE_LIST.remove(deliveryTag);
                }
            }

            // 处理 Basic.Nack 命令
            public void handleNack(long deliveryTag, boolean multiple) throws IOException {
                System.out.println("ConfirmListener.handleNack deliveryTag=" +
                        deliveryTag + ", multiple=" + multiple);
                if (multiple) {
                    MESSAGE_LIST.headSet(deliveryTag - 1).clear();
                } else {
                    MESSAGE_LIST.remove(deliveryTag);
                }
            }
        });

        // 将信道置为 publisher confirm 模式
        channel.confirmSelect();

        // 发送消息
        for(int i = 0; i < 20; i++) {
            long publishSeqNo = channel.getNextPublishSeqNo();
            MESSAGE_LIST.add(publishSeqNo);

            String message = "transaction message i=" + i;
            channel.basicPublish(EXCHANGE_NAME, "www.hxstrive.com",
                    false, null, message.getBytes());
            System.out.println("[Send] Send message ‘" + message + "’");
        }

        // 关闭连接
        channel.close();
        connection.close();
    }

    /**
     * 消费者
     * @throws Exception
     */
    private void consumer() throws Exception {
        // 创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        Connection connection = factory.newConnection();

        // 创建信道
        final Channel channel = connection.createChannel();
        channel.basicQos(1);

        // 声明交换器
        channel.exchangeDeclare(EXCHANGE_NAME, "topic");

        // 声明队列
        channel.queueDeclare(QUEUE_NAME, true,
                false, false, null) ;
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "*.hxstrive.com");

        // 消费消息
        System.out.println("[Consumer] Waiting for a message....");
        channel.basicConsume(QUEUE_NAME, false, new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                    AMQP.BasicProperties properties, byte[] body) throws IOException {
                try {
                    System.out.println("[Consumer] body = " + new String(body));
                    Thread.sleep(100);
                    channel.basicAck(envelope.getDeliveryTag(), false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
说说我的看法
全部评论(
没有评论
关于
本网站专注于 Java、数据库(MySQL、Oracle)、Linux、软件架构及大数据等多领域技术知识分享。涵盖丰富的原创与精选技术文章,助力技术传播与交流。无论是技术新手渴望入门,还是资深开发者寻求进阶,这里都能为您提供深度见解与实用经验,让复杂编码变得轻松易懂,携手共赴技术提升新高度。如有侵权,请来信告知:hxstrive@outlook.com
公众号