Search in sources :

Example 31 with DefaultConsumer

use of com.rabbitmq.client.DefaultConsumer in project rabbitmq-java-client by rabbitmq.

the class ChannelAsyncCompletableFutureTest method async.

@Test
public void async() throws Exception {
    channel.confirmSelect();
    CountDownLatch latch = new CountDownLatch(1);
    AMQP.Queue.Declare queueDeclare = new AMQImpl.Queue.Declare.Builder().queue(queue).durable(true).exclusive(false).autoDelete(false).arguments(null).build();
    channel.asyncCompletableRpc(queueDeclare).thenComposeAsync(action -> {
        try {
            return channel.asyncCompletableRpc(new AMQImpl.Exchange.Declare.Builder().exchange(exchange).type("fanout").durable(false).autoDelete(false).arguments(null).build());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }, executor).thenComposeAsync(action -> {
        try {
            return channel.asyncCompletableRpc(new AMQImpl.Queue.Bind.Builder().queue(queue).exchange(exchange).routingKey("").arguments(null).build());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }, executor).thenAcceptAsync(action -> {
        try {
            channel.basicPublish("", queue, null, "dummy".getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }, executor).thenAcceptAsync((whatever) -> {
        try {
            channel.basicConsume(queue, true, new DefaultConsumer(channel) {

                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    latch.countDown();
                }
            });
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }, executor);
    channel.waitForConfirmsOrDie(1000);
    assertTrue(latch.await(2, TimeUnit.SECONDS));
}
Also used : Envelope(com.rabbitmq.client.Envelope) java.util.concurrent(java.util.concurrent) After(org.junit.After) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) UUID(java.util.UUID) AMQImpl(com.rabbitmq.client.impl.AMQImpl) AMQP(com.rabbitmq.client.AMQP) Before(org.junit.Before) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) IOException(java.io.IOException) Envelope(com.rabbitmq.client.Envelope) AMQP(com.rabbitmq.client.AMQP) Test(org.junit.Test)

Example 32 with DefaultConsumer

use of com.rabbitmq.client.DefaultConsumer in project tech by ffyyhh995511.

the class MQConsumer method doGSMWork.

public void doGSMWork() throws Exception {
    final Connection connection = MQServer.getConnection();
    final Channel channel = connection.createChannel();
    // 消息持久化
    boolean durable = true;
    channel.queueDeclare(MQServer.GSM_QUEUE, durable, false, false, null);
    // 指定该线程同时只接收一条消息
    channel.basicQos(1);
    final Consumer consumer = new DefaultConsumer(channel) {

        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            Object gsmMessage = JSON.parseObject(message, Object.class);
            log.info(" [Consumer] Received : " + message);
            Map<String, String> map = null;
            // 只有服务端返回非失败才发ack
            if ("1".equals(map.get("status"))) {
                // envelope.getDeliveryTag() 获取消息ID
                // 接口返回消息除非网络异常和接口异常,其他(参数错误、已处理等)都做确认消息动作
                // 返回接收到消息的确认信息
                channel.basicAck(envelope.getDeliveryTag(), false);
            } else if ("2".equals(map.get("status"))) {
                // 如果nack的requeue = false,mq把消息直接丢弃
                // message requeue = true
                channel.basicNack(envelope.getDeliveryTag(), false, true);
            } else if ("3".equals(map.get("status"))) {
                // 参数不合法直接丢弃
                channel.basicNack(envelope.getDeliveryTag(), false, false);
            }
            log.info(" [Consumer] Done :" + message);
        }
    };
    // 打开消息应答机制
    channel.basicConsume(MQServer.GSM_QUEUE, false, consumer);
}
Also used : DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Consumer(com.rabbitmq.client.Consumer) DefaultConsumer(com.rabbitmq.client.DefaultConsumer) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) Envelope(com.rabbitmq.client.Envelope)

Aggregations

DefaultConsumer (com.rabbitmq.client.DefaultConsumer)32 IOException (java.io.IOException)21 Envelope (com.rabbitmq.client.Envelope)20 Channel (com.rabbitmq.client.Channel)15 Test (org.junit.Test)15 AMQP (com.rabbitmq.client.AMQP)11 TimeoutException (java.util.concurrent.TimeoutException)10 Connection (com.rabbitmq.client.Connection)9 Consumer (com.rabbitmq.client.Consumer)9 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)8 CountDownLatch (java.util.concurrent.CountDownLatch)4 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)3 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)3 KeyManagementException (java.security.KeyManagementException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 UUID (java.util.UUID)3 Path (javax.ws.rs.Path)3 AlreadyClosedException (com.rabbitmq.client.AlreadyClosedException)2 Recoverable (com.rabbitmq.client.Recoverable)2 RecoveryListener (com.rabbitmq.client.RecoveryListener)2