Search in sources :

Example 21 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 IOException, TimeoutException {
    ConnectionFactory factory = ConnectionFactoryUtils.getFactory();
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.exchangeDeclare(exchangeName, "fanout");
    channel.queueDeclare(queueName, false, false, false, null);
    channel.queueBind(queueName, exchangeName, "");
    DeliverCallback callback = (s, delivery) -> {
        String message = new String(delivery.getBody(), "UTF-8");
        System.out.println(message);
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    };
    channel.basicQos(1);
    boolean autoAck = false;
    channel.basicConsume(queueName, autoAck, callback, consumerTag -> {
    });
}
Also used : ConnectionFactoryUtils(com.hyf.testDemo.mq.ConnectionFactoryUtils) DeliverCallback(com.rabbitmq.client.DeliverCallback) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) 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)

Example 22 with DeliverCallback

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

the class ErrorReceive method main.

public static void main(String[] args) throws Exception {
    ConnectionFactory factory = ConnectionFactoryUtils.getFactory();
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    // 声明 exchange 和 queue
    channel.exchangeDeclare(exchangeName, "direct");
    channel.queueDeclare(queueName, false, false, false, null);
    // 进行绑定
    channel.queueBind(queueName, exchangeName, bindingKey);
    channel.queueBind(queueName, exchangeName, bindingKey2);
    DeliverCallback callback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), "utf-8");
        System.out.println("ErrorReceive 接收到" + delivery.getEnvelope().getRoutingKey() + "消息:" + message);
        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) 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)

Example 23 with DeliverCallback

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

the class Server method main.

public static void main(String[] args) throws Exception {
    ConnectionFactory factory = ConnectionFactoryUtils.getFactory();
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(requestQueueName, false, false, false, null);
    DeliverCallback callback = (consumerTag, delivery) -> {
        String msg = new String(delivery.getBody(), "utf-8");
        // 处理消息
        String reponse = handleMsg(msg);
        // 将消息的 correlationId 传回去
        AMQP.BasicProperties replyProps = new AMQP.BasicProperties.Builder().correlationId(delivery.getProperties().getCorrelationId()).build();
        channel.basicPublish("", delivery.getProperties().getReplyTo(), replyProps, reponse.getBytes());
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    };
    channel.basicQos(1);
    channel.basicConsume(requestQueueName, false, callback, consumeTag -> {
    });
}
Also used : ConnectionFactoryUtils(com.hyf.testDemo.mq.ConnectionFactoryUtils) DeliverCallback(com.rabbitmq.client.DeliverCallback) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) Channel(com.rabbitmq.client.Channel) AMQP(com.rabbitmq.client.AMQP) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Example 24 with DeliverCallback

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

the class Receive12 method main.

public static void main(String[] args) throws IOException, TimeoutException {
    ConnectionFactory factory = ConnectionFactoryUtils.getFactory();
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.exchangeDeclare(exchangeName, "fanout");
    channel.queueDeclare(queueName, false, false, false, null);
    channel.queueBind(queueName, exchangeName, "");
    DeliverCallback callback = (s, delivery) -> {
        String message = new String(delivery.getBody(), "UTF-8");
        System.out.println(message);
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    };
    channel.basicQos(1);
    boolean autoAck = false;
    channel.basicConsume(queueName, autoAck, callback, consumerTag -> {
    });
}
Also used : ConnectionFactoryUtils(com.hyf.testDemo.mq.ConnectionFactoryUtils) DeliverCallback(com.rabbitmq.client.DeliverCallback) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) 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)

Example 25 with DeliverCallback

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

the class InfoReceive method main.

public static void main(String[] args) throws Exception {
    ConnectionFactory factory = ConnectionFactoryUtils.getFactory();
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    // 声明 exchange 和 queue
    channel.exchangeDeclare(exchangeName, "direct");
    channel.queueDeclare(queueName, false, false, false, null);
    // 进行绑定
    channel.queueBind(queueName, exchangeName, bindingKey);
    DeliverCallback callback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), "utf-8");
        System.out.println("ErrorReceive 接收到" + delivery.getEnvelope().getRoutingKey() + "消息:" + message);
        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) 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)

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