use of com.rabbitmq.client.impl.recovery.AutorecoveringConnection in project rabbitmq-java-client by rabbitmq.
the class ChannelLimitNegotiation method openingTooManyChannels.
@Test
public void openingTooManyChannels() throws Exception {
int n = 48;
try {
Host.rabbitmqctl("eval 'application:set_env(rabbit, channel_max, " + n + ").'");
ConnectionFactory cf = TestUtils.connectionFactory();
Connection conn = cf.newConnection();
assertEquals(n, conn.getChannelMax());
for (int i = 1; i <= n; i++) {
assertNotNull(conn.createChannel(i));
}
// ChannelManager guards against channel.open being sent
assertNull(conn.createChannel(n + 1));
// Construct a channel directly
final ChannelN ch = new ChannelN(((AutorecoveringConnection) conn).getDelegate(), n + 1, new ConsumerWorkService(Executors.newSingleThreadExecutor(), Executors.defaultThreadFactory(), ConnectionFactory.DEFAULT_SHUTDOWN_TIMEOUT));
conn.addShutdownListener(new ShutdownListener() {
public void shutdownCompleted(ShutdownSignalException cause) {
// make sure channel.open continuation is released
ch.processShutdownSignal(cause, true, true);
}
});
ch.open();
fail("expected channel.open to cause a connection exception");
} catch (IOException e) {
checkShutdownSignal(530, e);
} finally {
Host.rabbitmqctl("eval 'application:set_env(rabbit, channel_max, 0).'");
}
}
use of com.rabbitmq.client.impl.recovery.AutorecoveringConnection in project rabbitmq-java-client by rabbitmq.
the class Metrics method checkAcksWithAutomaticRecovery.
@Test
public void checkAcksWithAutomaticRecovery() throws Exception {
ConnectionFactory connectionFactory = createConnectionFactory();
connectionFactory.setNetworkRecoveryInterval(2000);
connectionFactory.setAutomaticRecoveryEnabled(true);
StandardMetricsCollector metrics = new StandardMetricsCollector();
connectionFactory.setMetricsCollector(metrics);
Connection connection = null;
try {
connection = connectionFactory.newConnection();
Channel channel1 = connection.createChannel();
AtomicInteger ackedMessages = new AtomicInteger(0);
channel1.basicConsume(QUEUE, false, (consumerTag, message) -> {
try {
channel1.basicAck(message.getEnvelope().getDeliveryTag(), false);
ackedMessages.incrementAndGet();
} catch (Exception e) {
}
}, tag -> {
});
Channel channel2 = connection.createChannel();
channel2.confirmSelect();
int nbMessages = 10;
for (int i = 0; i < nbMessages; i++) {
sendMessage(channel2);
}
channel2.waitForConfirms(1000);
closeAndWaitForRecovery((AutorecoveringConnection) connection);
for (int i = 0; i < nbMessages; i++) {
sendMessage(channel2);
}
waitAtMost(timeout()).until(() -> ackedMessages.get(), equalTo(nbMessages * 2));
assertThat(metrics.getConsumedMessages().getCount(), is((long) (nbMessages * 2)));
assertThat(metrics.getAcknowledgedMessages().getCount(), is((long) (nbMessages * 2)));
} finally {
safeClose(connection);
}
}
use of com.rabbitmq.client.impl.recovery.AutorecoveringConnection in project rabbitmq-java-client by rabbitmq.
the class Metrics method checkListenersWithAutoRecoveryConnection.
@Test
public void checkListenersWithAutoRecoveryConnection() throws Exception {
ConnectionFactory connectionFactory = createConnectionFactory();
connectionFactory.setNetworkRecoveryInterval(2000);
connectionFactory.setAutomaticRecoveryEnabled(true);
StandardMetricsCollector metrics = new StandardMetricsCollector();
connectionFactory.setMetricsCollector(metrics);
Connection connection = null;
try {
connection = connectionFactory.newConnection();
Collection<?> shutdownHooks = getShutdownHooks(connection);
assertThat(shutdownHooks.size(), is(0));
connection.createChannel();
assertThat(metrics.getConnections().getCount(), is(1L));
assertThat(metrics.getChannels().getCount(), is(1L));
closeAndWaitForRecovery((AutorecoveringConnection) connection);
assertThat(metrics.getConnections().getCount(), is(1L));
assertThat(metrics.getChannels().getCount(), is(1L));
assertThat(shutdownHooks.size(), is(0));
} finally {
safeClose(connection);
}
}
use of com.rabbitmq.client.impl.recovery.AutorecoveringConnection in project rabbitmq-java-client by rabbitmq.
the class ConnectionFactory method newConnection.
/**
* Create a new broker connection with a client-provided name, picking the first available address from
* the list provided by the {@link AddressResolver}.
*
* If <a href="http://www.rabbitmq.com/api-guide.html#recovery">automatic connection recovery</a>
* is enabled, the connection returned by this method will be {@link Recoverable}. Future
* reconnection attempts will pick a random accessible address provided by the {@link AddressResolver}.
*
* @param executor thread execution service for consumers on the connection
* @param addressResolver discovery service to list potential addresses (hostname/port pairs) to connect to
* @param clientProvidedName application-specific connection name, will be displayed
* in the management UI if RabbitMQ server supports it.
* This value doesn't have to be unique and cannot be used
* as a connection identifier e.g. in HTTP API requests.
* This value is supposed to be human-readable.
* @return an interface to the connection
* @throws java.io.IOException if it encounters a problem
* @see <a href="http://www.rabbitmq.com/api-guide.html#recovery">Automatic Recovery</a>
*/
public Connection newConnection(ExecutorService executor, AddressResolver addressResolver, String clientProvidedName) throws IOException, TimeoutException {
if (this.metricsCollector == null) {
this.metricsCollector = new NoOpMetricsCollector();
}
// make sure we respect the provided thread factory
FrameHandlerFactory fhFactory = createFrameHandlerFactory();
ConnectionParams params = params(executor);
// set client-provided via a client property
if (clientProvidedName != null) {
Map<String, Object> properties = new HashMap<String, Object>(params.getClientProperties());
properties.put("connection_name", clientProvidedName);
params.setClientProperties(properties);
}
if (isAutomaticRecoveryEnabled()) {
// see com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory#newConnection
AutorecoveringConnection conn = new AutorecoveringConnection(params, fhFactory, addressResolver, metricsCollector);
conn.init();
return conn;
} else {
List<Address> addrs = addressResolver.getAddresses();
Exception lastException = null;
for (Address addr : addrs) {
try {
FrameHandler handler = fhFactory.create(addr, clientProvidedName);
AMQConnection conn = createConnection(params, handler, metricsCollector);
conn.start();
this.metricsCollector.newConnection(conn);
return conn;
} catch (IOException e) {
lastException = e;
} catch (TimeoutException te) {
lastException = te;
}
}
if (lastException != null) {
if (lastException instanceof IOException) {
throw (IOException) lastException;
} else if (lastException instanceof TimeoutException) {
throw (TimeoutException) lastException;
}
}
throw new IOException("failed to connect");
}
}
Aggregations