Search in sources :

Example 56 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project study-by-myself by Howinfun.

the class Receive2 method main.

public static void main(String[] args) throws Exception {
    ConnectionFactory factory = ConnectionFactoryUtils.getFactory();
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(queueName, false, false, false, null);
    channel.basicQos(1);
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), "UTF-8");
        ThreadUtil.sleep(4, TimeUnit.SECONDS);
        System.out.println(message);
        // 是否批量提交
        boolean multiple = false;
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), multiple);
    };
    boolean autoAck = false;
    channel.basicConsume(queueName, autoAck, deliverCallback, consumerTag -> {
    });
}
Also used : ConnectionFactoryUtils(com.hyf.testDemo.mq.ConnectionFactoryUtils) TimeUnit(java.util.concurrent.TimeUnit) DeliverCallback(com.rabbitmq.client.DeliverCallback) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) Channel(com.rabbitmq.client.Channel) ThreadUtil(cn.hutool.core.thread.ThreadUtil) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Example 57 with DeliverCallback

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

the class WorkConsumerAckImmediately 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.queueDeclare(queueName, true, false, false, null);
    channel.basicQos(1);
    // 由于消息推送是异步的,需要定义回调
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
        System.out.println(message + "      Done");
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        try {
            Thread.sleep(70000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    };
    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 58 with DeliverCallback

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

the class SubscribeConsumerTemporaryQueue2 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 59 with DeliverCallback

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

the class WorkConsumer1 method main.

/**
 * By default, RabbitMQ will send each message to the next consumer, in sequence.
 * On average every consumer will get the same number of messages.
 * This way of distributing messages is called round-robin
 * 默认情况下,多个consumer会按顺序消费队列消息,模式称为:round-robin
 * consumer之间是对等的公平的
 * @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