Example usage for com.rabbitmq.client ConfirmListener handleAck

List of usage examples for com.rabbitmq.client ConfirmListener handleAck

Introduction

In this page you can find the example usage for com.rabbitmq.client ConfirmListener handleAck.

Prototype

void handleAck(long deliveryTag, boolean multiple) throws IOException;

Source Link

Usage

From source file:reactor.rabbitmq.RabbitFluxTests.java

License:Open Source License

@Test
public void publishConfirmsErrorWhilePublishing() throws Exception {
    ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class);
    Connection mockConnection = mock(Connection.class);
    Channel mockChannel = mock(Channel.class);
    when(mockConnectionFactory.newConnection()).thenReturn(mockConnection);
    when(mockConnection.createChannel()).thenReturn(mockChannel);
    when(mockConnection.isOpen()).thenReturn(true);
    when(mockChannel.getConnection()).thenReturn(mockConnection);

    AtomicLong publishSequence = new AtomicLong();
    when(mockChannel.getNextPublishSeqNo()).thenAnswer(invocation -> publishSequence.incrementAndGet());
    when(mockChannel.isOpen()).thenReturn(true);

    CountDownLatch channelCloseLatch = new CountDownLatch(1);
    doAnswer(answer -> {/*w w  w  .  j a  v a 2 s. c  o  m*/
        channelCloseLatch.countDown();
        return null;
    }).when(mockChannel).close();

    CountDownLatch serverPublishConfirmLatch = new CountDownLatch(1);
    doNothing().doAnswer(answer -> {
        // see https://github.com/reactor/reactor-rabbitmq/pull/67#issuecomment-472789735
        serverPublishConfirmLatch.await(5, TimeUnit.SECONDS);
        throw new IOException("simulated error while publishing");
    }).when(mockChannel).basicPublish(anyString(), anyString(), nullable(AMQP.BasicProperties.class),
            any(byte[].class));

    int nbMessages = 10;
    Flux<OutboundMessage> msgFlux = Flux.range(0, nbMessages)
            .map(i -> new OutboundMessage("", queue, "".getBytes()));
    int nbMessagesAckNack = 1 + 1; // first published message confirmed + "fake" confirmation because of sending failure
    CountDownLatch confirmLatch = new CountDownLatch(nbMessagesAckNack);
    sender = createSender(new SenderOptions().connectionFactory(mockConnectionFactory));
    sender.sendWithPublishConfirms(msgFlux, new SendOptions().exceptionHandler((ctx, e) -> {
        throw new RabbitFluxException(e);
    })).subscribe(outboundMessageResult -> {
        if (outboundMessageResult.getOutboundMessage() != null) {
            confirmLatch.countDown();
        }
    }, error -> {
    });

    // have to wait a bit the subscription propagates and add the confirm listener
    Thread.sleep(100L);

    ArgumentCaptor<ConfirmListener> confirmListenerArgumentCaptor = ArgumentCaptor
            .forClass(ConfirmListener.class);
    verify(mockChannel).addConfirmListener(confirmListenerArgumentCaptor.capture());
    ConfirmListener confirmListener = confirmListenerArgumentCaptor.getValue();

    ExecutorService ioExecutor = Executors.newSingleThreadExecutor();
    ioExecutor.submit(() -> {
        confirmListener.handleAck(1, false);
        serverPublishConfirmLatch.countDown();
        return null;
    });

    assertTrue(confirmLatch.await(1L, TimeUnit.SECONDS));
    assertTrue(channelCloseLatch.await(1L, TimeUnit.SECONDS));
    verify(mockChannel, times(1)).close();
}

From source file:reactor.rabbitmq.ReactorRabbitMqTests.java

License:Open Source License

@Test
public void publishConfirmsErrorWhilePublishing() throws Exception {
    ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class);
    Connection mockConnection = mock(Connection.class);
    Channel mockChannel = mock(Channel.class);
    when(mockConnectionFactory.newConnection()).thenReturn(mockConnection);
    when(mockConnection.createChannel()).thenReturn(mockChannel);

    AtomicLong publishSequence = new AtomicLong();
    when(mockChannel.getNextPublishSeqNo()).thenAnswer(invocation -> publishSequence.incrementAndGet());

    doNothing().doThrow(new IOException("simulated error while publishing")).when(mockChannel)
            .basicPublish(anyString(), anyString(), any(AMQP.BasicProperties.class), any(byte[].class));

    int nbMessages = 10;
    Flux<OutboundMessage> msgFlux = Flux.range(0, nbMessages)
            .map(i -> new OutboundMessage("", queue, "".getBytes()));
    int nbMessagesAckNack = 2;
    CountDownLatch confirmLatch = new CountDownLatch(nbMessagesAckNack);
    sender = ReactorRabbitMq.createSender(mockConnectionFactory);
    CountDownLatch subscriptionLatch = new CountDownLatch(1);
    sender.sendWithPublishConfirms(msgFlux).subscribe(outboundMessageResult -> confirmLatch.countDown(),
            error -> {/*w ww .j a  va2 s  . co m*/
            });

    // have to wait a bit the subscription propagates and add the confirm listener
    Thread.sleep(100L);

    ArgumentCaptor<ConfirmListener> confirmListenerArgumentCaptor = ArgumentCaptor
            .forClass(ConfirmListener.class);
    verify(mockChannel).addConfirmListener(confirmListenerArgumentCaptor.capture());
    ConfirmListener confirmListener = confirmListenerArgumentCaptor.getValue();

    ExecutorService ioExecutor = Executors.newSingleThreadExecutor();
    ioExecutor.submit(() -> {
        confirmListener.handleAck(1, false);
        return null;
    });

    assertTrue(confirmLatch.await(1L, TimeUnit.SECONDS));
    verify(mockChannel, times(1)).close();
}