Example usage for com.rabbitmq.client ShutdownSignalException ShutdownSignalException

List of usage examples for com.rabbitmq.client ShutdownSignalException ShutdownSignalException

Introduction

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

Prototype

public ShutdownSignalException(boolean hardError, boolean initiatedByApplication, Method reason, Object ref) 

Source Link

Document

Construct a ShutdownSignalException from the arguments.

Usage

From source file:reactor.rabbitmq.ConnectionRecoveryTests.java

License:Open Source License

@Test
void consumeAutoAckRetryOnAck() 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);

    CountDownLatch consumerRegisteredLatch = new CountDownLatch(1);
    AtomicReference<DeliverCallback> deliverCallbackAtomicReference = new AtomicReference<>();

    when(mockChannel.basicConsume(anyString(), anyBoolean(), any(DeliverCallback.class),
            any(CancelCallback.class))).thenAnswer(answer -> {
                deliverCallbackAtomicReference.set(answer.getArgument(2));
                consumerRegisteredLatch.countDown();
                return "ctag";
            });//from w ww.  j  a v  a  2 s. co  m

    AtomicLong ackCount = new AtomicLong(0);
    AtomicLong errorAck = new AtomicLong(0);
    doAnswer(answer -> {
        ackCount.incrementAndGet();
        if (ackCount.get() == 3 || ackCount.get() == 4) {
            errorAck.incrementAndGet();
            throw new AlreadyClosedException(new ShutdownSignalException(true, false, null, null));
        }
        return null;
    }).when(mockChannel).basicAck(anyLong(), anyBoolean());

    receiver = RabbitFlux.createReceiver(new ReceiverOptions().connectionSupplier(cf -> mockConnection));

    AtomicInteger ackedMessages = new AtomicInteger(0);
    receiver.consumeAutoAck("whatever",
            new ConsumeOptions().exceptionHandler(new ExceptionHandlers.RetryAcknowledgmentExceptionHandler(
                    ofSeconds(5), ofMillis(100), ExceptionHandlers.CONNECTION_RECOVERY_PREDICATE)))
            .subscribe(msg -> {
                ackedMessages.incrementAndGet();
            });

    assertTrue(consumerRegisteredLatch.await(1, TimeUnit.SECONDS),
            "Consumer should have been registered by now");

    int nbMessages = 10;
    IntStream.range(0, nbMessages).forEach(i -> {
        try {
            deliverCallbackAtomicReference.get().handle("",
                    new Delivery(new Envelope(i, true, null, null), null, null));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });

    assertEquals(nbMessages, ackedMessages.get(), "All messages should have been ack-ed, as ack is retried");
    assertEquals(nbMessages + errorAck.get(), ackCount.get(),
            "There should have been nbMessages+ackInError calls to basicAck as acknowledgments are retried");
}

From source file:reactor.rabbitmq.ConnectionRecoveryTests.java

License:Open Source License

@Test
void consumeManualAckRetryOnAck() 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);

    CountDownLatch consumerRegisteredLatch = new CountDownLatch(1);
    AtomicReference<DeliverCallback> deliverCallbackAtomicReference = new AtomicReference<>();

    when(mockChannel.basicConsume(anyString(), anyBoolean(), any(DeliverCallback.class),
            any(CancelCallback.class))).thenAnswer(answer -> {
                deliverCallbackAtomicReference.set(answer.getArgument(2));
                consumerRegisteredLatch.countDown();
                return "ctag";
            });//from w w  w  .ja  v a 2  s. com

    AtomicLong ackCount = new AtomicLong(0);
    doAnswer(answer -> {
        ackCount.incrementAndGet();
        if (ackCount.get() == 3 || ackCount.get() == 4) {
            throw new AlreadyClosedException(new ShutdownSignalException(true, false, null, null));
        }
        return null;
    }).when(mockChannel).basicAck(anyLong(), anyBoolean());

    receiver = RabbitFlux.createReceiver(new ReceiverOptions().connectionSupplier(cf -> mockConnection));

    AtomicInteger ackedMessages = new AtomicInteger(0);
    BiConsumer<Receiver.AcknowledgmentContext, Exception> exceptionHandler = new ExceptionHandlers.RetryAcknowledgmentExceptionHandler(
            ofSeconds(5), ofMillis(100), ExceptionHandlers.CONNECTION_RECOVERY_PREDICATE);
    receiver.consumeManualAck("whatever", new ConsumeOptions().exceptionHandler(exceptionHandler))
            .subscribe(msg -> {
                // do business stuff
                // ...
                // trying to ack
                msg.ack();
                ackedMessages.incrementAndGet();
            });

    assertTrue(consumerRegisteredLatch.await(1, TimeUnit.SECONDS),
            "Consumer should have been registered by now");

    int nbMessages = 10;
    IntStream.range(0, nbMessages).forEach(i -> {
        try {
            deliverCallbackAtomicReference.get().handle("",
                    new Delivery(new Envelope(i, true, null, null), null, null));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });

    assertEquals(nbMessages, ackedMessages.get(), "All messages should have been ack-ed, as ack is retried");
}

From source file:reactor.rabbitmq.ExceptionHandlersTests.java

License:Open Source License

@Test
public void retryableExceptions() {
    predicate = new ExceptionHandlers.ExceptionPredicate(singletonMap(Exception.class, true));
    assertTrue(predicate.test(new AlreadyClosedException(new ShutdownSignalException(true, true, null, null))));
    assertFalse(predicate.test(new Throwable()));

    predicate = new ExceptionHandlers.ExceptionPredicate(new HashMap<Class<? extends Throwable>, Boolean>() {
        {/*w ww .  ja  v  a 2  s  .c o m*/
            put(ShutdownSignalException.class, true);
            put(IOException.class, false);
            put(AuthenticationFailureException.class, true);
        }
    });

    assertTrue(predicate.test(new ShutdownSignalException(true, true, null, null)), "directly retryable");
    assertTrue(predicate.test(new AlreadyClosedException(new ShutdownSignalException(true, true, null, null))),
            "retryable from its super-class");
    assertFalse(predicate.test(new IOException()), "not retryable");
    assertTrue(predicate.test(new AuthenticationFailureException("")), "directly retryable");
}

From source file:reactor.rabbitmq.ExceptionHandlersTests.java

License:Open Source License

@Test
public void connectionRecoveryTriggering() {
    predicate = new ExceptionHandlers.ConnectionRecoveryTriggeringPredicate();
    assertTrue(predicate.test(new ShutdownSignalException(true, false, null, null)),
            "hard error, not triggered by application");
    assertTrue(predicate.test(new ShutdownSignalException(false, false, null, null)),
            "soft error, not triggered");
    assertFalse(predicate.test(new ShutdownSignalException(false, true, null, null)),
            "soft error, triggered by application");
}

From source file:uk.trainwatch.rabbitmq.RabbitRPCClient.java

License:Apache License

private byte[] primitiveCall(AMQP.BasicProperties props, byte[] message)
        throws IOException, ShutdownSignalException, TimeoutException {
    checkConsumer();//from  ww  w.  j a  va 2  s .  c  om
    BlockingCell<Object> k = new BlockingCell<>();

    String replyId = RabbitMQ.newCorrelationId();
    props = ((props == null) ? new AMQP.BasicProperties.Builder() : props.builder()).correlationId(replyId)
            .replyTo(replyQueue).build();
    continuationMap.put(replyId, k);

    publish(props, message);

    Object reply = k.uninterruptibleGet(timeout);
    if (reply instanceof ShutdownSignalException) {
        ShutdownSignalException sig = (ShutdownSignalException) reply;
        ShutdownSignalException wrapper = new ShutdownSignalException(sig.isHardError(),
                sig.isInitiatedByApplication(), sig.getReason(), sig.getReference());
        wrapper.initCause(sig);
        throw wrapper;
    } else {
        return (byte[]) reply;
    }
}