Search in sources :

Example 26 with DeliverCallback

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

the class Receive1 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(2, 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 27 with DeliverCallback

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

the class InfoReceive method main.

@SneakyThrows
public static void main(String[] args) {
    ConnectionFactory factory = ConnectionFactoryUtils.getFactory();
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.exchangeDeclare(exchangeName, "topic");
    channel.queueDeclare(queueName, false, false, false, null);
    channel.queueBind(queueName, exchangeName, bindingKey);
    DeliverCallback callback = (consumerTag, delivery) -> {
        String msg = new String(delivery.getBody(), "utf-8");
        System.out.println("接收到一条info消息:" + msg);
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    };
    channel.basicQos(1);
    channel.basicConsume(queueName, false, callback, consumerTag -> {
    });
}
Also used : ConnectionFactoryUtils(com.hyf.testDemo.mq.ConnectionFactoryUtils) DeliverCallback(com.rabbitmq.client.DeliverCallback) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) SneakyThrows(lombok.SneakyThrows) Connection(com.rabbitmq.client.Connection) Channel(com.rabbitmq.client.Channel) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback) SneakyThrows(lombok.SneakyThrows)

Example 28 with DeliverCallback

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

the class UserReceive method main.

@SneakyThrows
public static void main(String[] args) {
    ConnectionFactory factory = ConnectionFactoryUtils.getFactory();
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.exchangeDeclare(exchangeName, "topic");
    channel.queueDeclare(queueName, false, false, false, null);
    channel.queueBind(queueName, exchangeName, bindingKey);
    DeliverCallback callBack = (consumerTag, delivery) -> {
        String msg = new String(delivery.getBody(), "utf-8");
        System.out.println("接收到一条user消息:" + msg);
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    };
    channel.basicQos(1);
    channel.basicConsume(queueName, false, callBack, consumerTag -> {
    });
}
Also used : ConnectionFactoryUtils(com.hyf.testDemo.mq.ConnectionFactoryUtils) DeliverCallback(com.rabbitmq.client.DeliverCallback) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) SneakyThrows(lombok.SneakyThrows) Connection(com.rabbitmq.client.Connection) Channel(com.rabbitmq.client.Channel) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback) SneakyThrows(lombok.SneakyThrows)

Example 29 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project force-oneself by Force-oneself.

the class ReceiveLogs method psReceive.

public void psReceive(String[] argv) throws Exception {
    Channel channel = RabbitMQUtils.getChannel();
    channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, EXCHANGE_NAME, "");
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
        System.out.println(" [x] Received '" + message + "'");
    };
    channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
    });
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) RabbitMQUtils(com.quan.framework.amqp.util.RabbitMQUtils) Channel(com.rabbitmq.client.Channel) StandardCharsets(java.nio.charset.StandardCharsets) Channel(com.rabbitmq.client.Channel) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Example 30 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project force-oneself by Force-oneself.

the class ReceiveLogsDirect method main.

public static void main(String[] argv) throws Exception {
    Channel channel = RabbitMQUtils.getChannel();
    channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
    String queueName = channel.queueDeclare().getQueue();
    if (argv.length < 1) {
        System.err.println("Usage: ReceiveLogsDirect [info] [warning] [error]");
        System.exit(1);
    }
    for (String severity : argv) {
        channel.queueBind(queueName, EXCHANGE_NAME, severity);
    }
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
        System.out.println(" [x] Received '" + delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
    };
    channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
    });
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) RabbitMQUtils(com.quan.framework.amqp.util.RabbitMQUtils) Channel(com.rabbitmq.client.Channel) BuiltinExchangeType(com.rabbitmq.client.BuiltinExchangeType) StandardCharsets(java.nio.charset.StandardCharsets) Channel(com.rabbitmq.client.Channel) 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