com.rabbitmq.client.AlreadyClosedException Java Examples

The following examples show how to use com.rabbitmq.client.AlreadyClosedException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractFunctionalTest.java    From lyra with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an answer that fails n times for each thread, throwing t for the first n invocations
 * and returning {@code returnValue} thereafter. Prior to throwing t, the connection handler's
 * shutdown listener is completed if t is a connection shutdown signal.
 */
protected <T> Answer<T> failNTimes(final int n, final Throwable t, final T returnValue,
    final RetryableResource resource) {
  return new Answer<T>() {
    AtomicInteger failures = new AtomicInteger();

    @Override
    public T answer(InvocationOnMock invocation) throws Throwable {
      if (failures.getAndIncrement() >= n)
        return returnValue;

      if (t instanceof ShutdownSignalException)
        callShutdownListener(resource, (ShutdownSignalException) t);
      if (t instanceof ShutdownSignalException && !(t instanceof AlreadyClosedException))
        throw new IOException(t);
      else
        throw t;
    }
  };
}
 
Example #2
Source File: MockConnection.java    From rabbitmq-mock with Apache License 2.0 5 votes vote down vote up
@Override
public MockChannel createChannel(int channelNumber) throws AlreadyClosedException {
    if (!isOpen()) {
        throw new AlreadyClosedException(new ShutdownSignalException(false, true, null, this));
    }
    return new MockChannel(channelNumber, mockNode, this, metricsCollectorWrapper);
}
 
Example #3
Source File: MockConnectionTest.java    From rabbitmq-mock with Apache License 2.0 5 votes vote down vote up
@Test
void createChannel_throws_when_connection_is_closed() throws IOException {
    try (Connection connection = new MockConnectionFactory().newConnection()) {
        connection.close();

        assertThatExceptionOfType(AlreadyClosedException.class)
            .isThrownBy(() -> connection.createChannel());
    }
}
 
Example #4
Source File: AmqpForwardAttributeTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
public void serviceShouldNotFailWhenAlreadyClosedException() throws Exception {
    mailet.init(mailetConfig);
    Mail mail = mock(Mail.class);
    when(mail.getAttribute(MAIL_ATTRIBUTE)).thenReturn(ATTRIBUTE_CONTENT);
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    ShutdownSignalException shutdownSignalException = new ShutdownSignalException(false, false, new Close.Builder().build(), "reference");
    when(connectionFactory.newConnection()).thenThrow(new AlreadyClosedException(shutdownSignalException));
    mailet.setConnectionFactory(connectionFactory);

    mailet.service(mail);
}
 
Example #5
Source File: ChannelHandlerTest.java    From lyra with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = AlreadyClosedException.class)
public void shouldThrowOnAlreadyClosedChannelInvocation() throws Throwable {
  mockConnection();
  Channel channel = mockChannel().proxy;
  when(channel.getCloseReason()).thenReturn(channelShutdownSignal());
  channel.close();
  channel.abort();
}
 
Example #6
Source File: MockConnection.java    From rabbitmq-mock with Apache License 2.0 4 votes vote down vote up
@Override
public MockChannel createChannel() throws AlreadyClosedException {
    return createChannel(channelSequence.incrementAndGet());
}
 
Example #7
Source File: ChannelHandler.java    From lyra with Apache License 2.0 4 votes vote down vote up
@Override
public Object invoke(Object ignored, final Method method, final Object[] args) throws Throwable {
  if (closed && method.getDeclaringClass().isAssignableFrom(Channel.class))
    throw new AlreadyClosedException(delegate.getCloseReason());

  Callable<Object> callable = new Callable<Object>() {
    @Override
    public Object call() throws Exception {
      if (method.getDeclaringClass().isAssignableFrom(ChannelConfig.class))
        return Reflection.invoke(config, method, args);

      String methodName = method.getName();

      if ("basicAck".equals(methodName) || "basicNack".equals(methodName)
        || "basicReject".equals(methodName)) {
        long deliveryTag = (Long) args[0] - previousMaxDeliveryTag;
        if (deliveryTag > 0)
          args[0] = deliveryTag;
        else
          return null;
      } else if ("basicConsume".equals(methodName))
        return handleConsumerDeclare(method, args);
      else if ("basicCancel".equals(methodName) && args[0] != null)
        consumerDeclarations.remove((String) args[0]);
      else if ("exchangeDelete".equals(methodName) && args[0] != null)
        connectionHandler.exchangeDeclarations.remove((String) args[0]);
      else if ("exchangeUnbind".equals(methodName) && args[0] != null)
        connectionHandler.exchangeBindings.remove((String) args[0], new Binding(args));
      else if ("queueDelete".equals(methodName) && args[0] != null)
        connectionHandler.queueDeclarations.remove((String) args[0]);
      else if ("queueUnbind".equals(methodName) && args[0] != null)
        connectionHandler.queueBindings.remove((String) args[0], new Binding(args));

      Object result = Reflection.invoke(delegate, method, args);

      if ("exchangeDeclare".equals(methodName))
        handleExchangeDeclare(method, args);
      else if ("exchangeBind".equals(methodName))
        handleExchangeBind(args);
      else if ("queueDeclare".equals(methodName))
        handleQueueDeclare(((Queue.DeclareOk) result).getQueue(), method, args);
      else if ("queueBind".equals(methodName))
        handleQueueBind(method, args);
      else if ("flowBlocked".equals(methodName))
        flowBlocked = true;
      else if ("basicQos".equals(methodName)) {
        // Store non-global Qos
        if (args.length < 3 || !(Boolean) args[2])
          basicQos = new ResourceDeclaration(method, args);
      } else if ("confirmSelect".equals(methodName))
        confirmSelect = true;
      else if ("txSelect".equals(methodName))
        txSelect = true;
      else if (methodName.startsWith("add"))
        handleAdd(methodName, args[0]);
      else if (methodName.startsWith("remove"))
        handleRemove(methodName, args[0]);
      else if (methodName.startsWith("clear"))
        handleClear(methodName);

      return result;
    }

    @Override
    public String toString() {
      return Reflection.toString(method);
    }
  };

  return handleCommonMethods(delegate, method, args) ? null : callWithRetries(callable,
    config.getChannelRetryPolicy(), null, config.getRetryableExceptions(), canRecover(), true);
}
 
Example #8
Source File: Exceptions.java    From lyra with Apache License 2.0 4 votes vote down vote up
/**
 * Reliably returns whether the shutdown signal represents a connection closure.
 */
public static boolean isConnectionClosure(ShutdownSignalException e) {
  return e instanceof AlreadyClosedException ? e.getReference() instanceof Connection
      : e.isHardError();
}