Search in sources :

Example 51 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project SpringBoot-Learning by kong0827.

the class consumer02 method main.

public static void main(String[] args) throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("47.102.218.26");
    factory.setPort(5672);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    /**
     * 注意
     * Rabbitmq服务通道是持久通道,该queue 已经存在, 而且通道属性需要保持一致
     */
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    // DefaultConsumer consumer = new DefaultConsumer(channel) {
    // /**
    // *
    // * @param consumerTag 标识
    // * @param envelope 获取的一些信息 包括交换机 路由
    // * @param properties 配置信息
    // * @param body 数据
    // * @throws IOException
    // */
    // @Override
    // public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
    // System.out.println("consumerTag:" + consumerTag);
    // System.out.println("envelope:" + envelope);
    // System.out.println("properties:" + properties);
    // System.out.println("message:" + new String(body));
    // }
    // };
    // channel.basicConsume(QUEUE_NAME, true, consumer);
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), "UTF-8");
        System.out.println(" message: " + message + "'");
    };
    /**
     * 启动一个消费者,并返回服务端生成的消费者标识
     * queue:队列名
     * autoAck:true 接收到传递过来的消息后acknowledged(应答服务器),false 接收到消息后不应答服务器
     * deliverCallback: 当一个消息发送过来后的回调接口
     * cancelCallback:当一个消费者取消订阅时的回调接口;取消消费者订阅队列时除了使用{@link Channel#basicCancel}之外的所有方式都会调用该回调方法
     * @return 服务端生成的消费者标识
     */
    channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
    });
}
Also used : 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 52 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project SpringBoot-Learning by kong0827.

the class consumer02 method main.

public static void main(String[] args) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("47.102.218.26");
    factory.setPort(5672);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(TOPIC_QUEUE_2, true, false, false, null);
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), "UTF-8");
        System.out.println(" message: " + message + "'");
    };
    channel.basicConsume(TOPIC_QUEUE_2, true, deliverCallback, consumerTag -> {
    });
}
Also used : 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 53 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project mercury by yellow013.

the class ReceiveLogsTopic method main.

public static void main(String[] args) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.exchangeDeclare(EXCHANGE_NAME, "topic");
    String queueName = channel.queueDeclare().getQueue();
    if (args.length < 1) {
        System.err.println("Usage: ReceiveLogsTopic [binding_key]...");
        System.exit(1);
    }
    for (String bindingKey : args) {
        channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);
    }
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), "UTF-8");
        System.out.println(" [x] Received '" + delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
    };
    channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
    });
}
Also used : 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 54 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project mercury by yellow013.

the class ReceiveLogs method main.

public static void main(String[] args) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    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(), "UTF-8");
        System.out.println(" [x] Received '" + message + "'");
    };
    channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
    });
}
Also used : 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 55 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project skywalking-java by apache.

the class CaseController method rabbitmqCase.

@RequestMapping("/rabbitmq")
@ResponseBody
public String rabbitmqCase() {
    Channel channel = null;
    Connection connection = null;
    try {
        ConnectionFactory factory = new ConnectionFactory();
        LOGGER.info("Using brokerUrl = " + brokerUrl);
        factory.setHost(brokerUrl);
        factory.setPort(PORT);
        factory.setUsername(USERNAME);
        factory.setPassword(PASSWORD);
        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        AMQP.BasicProperties.Builder propsBuilder = new AMQP.BasicProperties.Builder();
        LOGGER.info("Message being published -------------->" + MESSAGE);
        channel.basicPublish("", QUEUE_NAME, propsBuilder.build(), MESSAGE.getBytes(StandardCharsets.UTF_8));
        LOGGER.info("Message has been published-------------->" + MESSAGE);
        final CountDownLatch waitForConsume = new CountDownLatch(1);
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            LOGGER.info("Message received-------------->" + message);
            waitForConsume.countDown();
        };
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
        });
        waitForConsume.await(5000L, TimeUnit.MILLISECONDS);
        LOGGER.info("Message Consumed-------------->");
    } catch (Exception ex) {
        LOGGER.error(ex.toString());
    } finally {
        if (channel != null) {
            try {
                channel.close();
            } catch (Exception e) {
            // ignore
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            // ignore
            }
        }
    }
    return "Success";
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) PropertySource(org.springframework.context.annotation.PropertySource) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Connection(com.rabbitmq.client.Connection) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RestController(org.springframework.web.bind.annotation.RestController) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) Value(org.springframework.beans.factory.annotation.Value) CountDownLatch(java.util.concurrent.CountDownLatch) Logger(org.apache.logging.log4j.Logger) Channel(com.rabbitmq.client.Channel) LogManager(org.apache.logging.log4j.LogManager) AMQP(com.rabbitmq.client.AMQP) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) AMQP(com.rabbitmq.client.AMQP) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback) CountDownLatch(java.util.concurrent.CountDownLatch) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

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