RabbitMQ 教程

QosDemo1.java

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

/**
 * 验证 RabbitMQ 的 channel.basicQos() 方法的作用
 * @author hxstrive.com 2022/3/8
 */
public class QosDemo1 {
    /** 交换器名称 */
    private static final String EXCHANGE_NAME = "exchange-" + QosDemo1.class.getSimpleName();
    /** 队列名称 */
    private static final String QUEUE_NAME = "queue-" + QosDemo1.class.getSimpleName();


    public static void main(String[] args) throws Exception {
        QosDemo1 demo = new QosDemo1();
        demo.consumer();
        demo.consumer2();
        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.exchangeDeclare(EXCHANGE_NAME, "topic");

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

        // 发送消息
        System.out.println("[Sender] Send Message...");
        for(int i = 0; i < 10; i++) {
            String message = "qos message i=" + i;
            channel.basicPublish(EXCHANGE_NAME, "www.hxstrive.com",
                    null, message.getBytes());
            System.out.println("[Sender] 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.exchangeDeclare(EXCHANGE_NAME, "topic");

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

        // 消费消息
        System.out.println("[Consumer1] Waiting for a message....");
        channel.basicQos(1);
        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("[Consumer1] body = " + new String(body));
                    Thread.sleep(2000);
                    channel.basicAck(envelope.getDeliveryTag(), false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 消费者2
     * @throws Exception
     */
    private void consumer2() 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.exchangeDeclare(EXCHANGE_NAME, "topic");

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

        // 消费消息
        System.out.println("[Consumer2] Waiting for a message....");
        channel.basicQos(1);
        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("[Consumer2] body = " + new String(body));
                    Thread.sleep(1000);
                    channel.basicAck(envelope.getDeliveryTag(), false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号