Search in sources :

Example 6 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project code-study by chenyaoBOY.

the class WorkConsumerAck method main.

/**
 * A timeout (30 minutes by default) is enforced on consumer delivery acknowledgement.
 * This helps detect buggy (stuck) consumers that never acknowledge deliveries.
 * You can increase this timeout as described in Delivery Acknowledgement Timeout.
 * 消费者超时30分钟后将强制发送ACK
 * If a consumer does not ack its delivery for more than the timeout value (30 minutes by default),
 * its channel will be closed with a PRECONDITION_FAILED channel exception
 * 超过30分钟后,channel会被关闭 并抛出异常
 *
 * The timeout value is configurable in [rabbitmq.conf] (in milliseconds):
 *
 * # 30 minutes in milliseconds
 * consumer_timeout = 1800000
 * # one hour in milliseconds
 * consumer_timeout = 3600000
 * <p>
 * <p>
 * 是否开启ACK 通过 boolean autoAck 参数控制
 *
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    // 创建连接工厂
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("127.0.0.1");
    // 创建连接
    Connection connection = factory.newConnection();
    // 创建通道
    Channel channel = connection.createChannel();
    // 这里声明队列的原因是因为 消费者可能会早于生产者启动,为了确保队列一定存在而创建的
    // 所以如果队列存在,那么直接订阅即可
    channel.queueDeclare(queueName, false, false, false, null);
    // 由于消息推送是异步的,需要定义回调
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        try {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            doWork(message);
            System.out.println(message + "      Done");
        } finally {
            /**
             * Acknowledgement must be sent on the same channel that received the delivery.
             * Attempts to acknowledge using a different channel will result in a channel-level protocol exception.
             * 如果不发送ACK确认消息,则MQ不会删除队列中的消息
             * 消费者有消息没有确认ack的时候,当数量达到prefetch时,mq将不会再发送消息给该消费者,直到超时
             */
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
    };
    /**
     * autoAck = false
     */
    channel.basicConsume(queueName, false, deliverCallback, (consumerTag, sig) -> {
    });
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) Channel(com.rabbitmq.client.Channel) StandardCharsets(java.nio.charset.StandardCharsets) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Example 7 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project code-study by chenyaoBOY.

the class WorkConsumerDispatch method main.

public static void main(String[] args) throws Exception {
    // 创建连接工厂
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("127.0.0.1");
    // 创建连接
    Connection connection = factory.newConnection();
    // 创建通道
    Channel channel = connection.createChannel();
    /**
     * 设置消费数量
     */
    channel.basicQos(1);
    // 这里声明队列的原因是因为 消费者可能会早于生产者启动,为了确保队列一定存在而创建的
    // 所以如果队列存在,那么直接订阅即可
    channel.queueDeclare(queueName, false, false, false, null);
    // 由于消息推送是异步的,需要定义回调
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        try {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            doWork(message);
            System.out.println(message + "      Done");
        } finally {
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
    };
    channel.basicConsume(queueName, false, deliverCallback, (consumerTag, sig) -> {
    });
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) Channel(com.rabbitmq.client.Channel) StandardCharsets(java.nio.charset.StandardCharsets) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Example 8 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project code-study by chenyaoBOY.

the class WorkConsumerDispatch2 method main.

public static void main(String[] args) throws Exception {
    // 创建连接工厂
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("127.0.0.1");
    // 创建连接
    Connection connection = factory.newConnection();
    // 创建通道
    Channel channel = connection.createChannel();
    /**
     * 设置消费数量
     */
    channel.basicQos(1);
    // 这里声明队列的原因是因为 消费者可能会早于生产者启动,为了确保队列一定存在而创建的
    // 所以如果队列存在,那么直接订阅即可
    channel.queueDeclare(queueName, false, false, false, null);
    // 由于消息推送是异步的,需要定义回调
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        try {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            doWork(message);
            System.out.println(message + "      Done");
        } finally {
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        }
    };
    channel.basicConsume(queueName, false, deliverCallback, (consumerTag, sig) -> {
    });
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) Channel(com.rabbitmq.client.Channel) StandardCharsets(java.nio.charset.StandardCharsets) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Example 9 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project code-study by chenyaoBOY.

the class SubscribeConsumerTemporaryQueue method main.

public static void main(String[] args) throws Exception {
    // 创建连接工厂
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("127.0.0.1");
    // 创建连接
    Connection connection = factory.newConnection();
    // 创建通道
    Channel channel = connection.createChannel();
    // 临时队列,连接断开,队列消失
    // amq.gen-jnnX7acGeSY50L4Wi_DK4g
    String queueRandom = channel.queueDeclare().getQueue();
    channel.queueBind(queueRandom, exchangeName, "");
    // 由于消息推送是异步的,需要定义回调
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
        doWork(message);
        System.out.println(message + "      Done");
    };
    channel.basicConsume(queueRandom, true, deliverCallback, (consumerTag, sig) -> {
    });
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) Channel(com.rabbitmq.client.Channel) StandardCharsets(java.nio.charset.StandardCharsets) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Example 10 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project code-study by chenyaoBOY.

the class WorkConsumer2 method main.

/**
 * 启动后代码不会退出
 *
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    // 创建连接工厂
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("127.0.0.1");
    // 创建连接
    Connection connection = factory.newConnection();
    // 创建通道
    Channel channel = connection.createChannel();
    // 这里声明队列的原因是因为 消费者可能会早于生产者启动,为了确保队列一定存在而创建的
    // 所以如果队列存在,那么直接订阅即可
    channel.queueDeclare(queueName, false, false, false, null);
    // 由于消息推送是异步的,需要定义回调
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
        doWork(message);
        System.out.println(message + "      Done");
    };
    channel.basicConsume(queueName, true, deliverCallback, (consumerTag, sig) -> {
    });
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) Channel(com.rabbitmq.client.Channel) StandardCharsets(java.nio.charset.StandardCharsets) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Aggregations

DeliverCallback (com.rabbitmq.client.DeliverCallback)59 Channel (com.rabbitmq.client.Channel)58 Connection (com.rabbitmq.client.Connection)53 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)50 StandardCharsets (java.nio.charset.StandardCharsets)21 IOException (java.io.IOException)18 TimeoutException (java.util.concurrent.TimeoutException)12 ConnectionFactoryUtils (com.hyf.testDemo.mq.ConnectionFactoryUtils)11 AMQP (com.rabbitmq.client.AMQP)9 Map (java.util.Map)6 TimeUnit (java.util.concurrent.TimeUnit)6 Date (java.util.Date)5 RabbitMQUtils (com.quan.framework.amqp.util.RabbitMQUtils)4 CancelCallback (com.rabbitmq.client.CancelCallback)4 AbstractClient (ProducerDummy.Client.AbstractClient)3 AggregateMessage (ProducerDummy.Messages.AggregateMessage)2 Message (ProducerDummy.Messages.Message)2 ThreadUtil (cn.hutool.core.thread.ThreadUtil)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DeclareOk (com.rabbitmq.client.AMQP.Exchange.DeclareOk)2