List of usage examples for com.rabbitmq.client ShutdownListener ShutdownListener
ShutdownListener
From source file:org.graylog2.inputs.transports.AmqpConsumer.java
License:Open Source License
public void connect() throws IOException { final ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostname);/*from w w w.j av a2s . c o m*/ factory.setPort(port); factory.setVirtualHost(virtualHost); if (tls) { try { LOG.info("Enabling TLS for AMQP input [{}/{}].", sourceInput.getName(), sourceInput.getId()); factory.useSslProtocol(); } catch (NoSuchAlgorithmException | KeyManagementException e) { throw new IOException("Couldn't enable TLS for AMQP input.", e); } } // Authenticate? if (!isNullOrEmpty(username) && !isNullOrEmpty(password)) { factory.setUsername(username); factory.setPassword(password); } connection = factory.newConnection(); channel = connection.createChannel(); if (null == channel) { LOG.error("No channel descriptor available!"); } if (null != channel && prefetchCount > 0) { channel.basicQos(prefetchCount); LOG.info("AMQP prefetch count overriden to <{}>.", prefetchCount); } connection.addShutdownListener(new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException cause) { if (cause.isInitiatedByApplication()) { LOG.info("Not reconnecting connection, we disconnected explicitly."); return; } while (true) { try { LOG.error("AMQP connection lost! Trying reconnect in 1 second."); Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); connect(); LOG.info("Connected! Re-starting consumer."); run(); LOG.info("Consumer running."); break; } catch (IOException e) { LOG.error("Could not re-connect to AMQP broker.", e); } } } }); }
From source file:org.mule.transport.amqp.AbstractAmqpITCase.java
License:Open Source License
private Channel newChannel() throws IOException { final Channel channel = connection.createChannel(); channel.addShutdownListener(new ShutdownListener() { public void shutdownCompleted(final ShutdownSignalException sse) { if (!sse.isInitiatedByApplication()) { channelRef.set(null);/*from ww w. ja v a2 s. c o m*/ } } }); return channel; }
From source file:org.mule.transport.amqp.internal.client.ChannelHandler.java
License:Open Source License
public Channel createChannel(ImmutableEndpoint endpoint) throws IOException { final AmqpConnector connector = (AmqpConnector) endpoint.getConnector(); try {//from w w w . j av a 2 s . co m final Channel channel = connector.getConnection().createChannel(); channel.addReturnListener(connector.getDefaultReturnListener()); channel.basicQos(connector.getPrefetchSize(), connector.getPrefetchCount(), false); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Created and configured new channel: " + channel); } channel.addShutdownListener(new ShutdownListener() { public void shutdownCompleted(final ShutdownSignalException sse) { if (sse.isInitiatedByApplication()) { return; } // do not inform the connector of the issue as it can't // decide what to do reset the channel so it would later // be lazily reconnected if (LOGGER.isDebugEnabled()) { LOGGER.debug("Terminated dead channel: " + channel, sse); } } }); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Shutdown listener configured on channel: " + channel); } return channel; } catch (final Exception e) { if ((!connector.isStopping()) && (connector.isStarted())) { connector.getMuleContext().getExceptionListener() .handleException(new ConnectException(MessageFactory.createStaticMessage( "Impossible to create new channels on connection: " + connector.getConnection()), e, connector)); } return null; } }
From source file:org.mule.transport.amqp.internal.connector.AmqpConnector.java
License:Open Source License
protected void connectToFirstResponsiveBroker(final List<Address> brokerAddresses) throws Exception { Exception lastException = null; for (final Address brokerAddress : brokerAddresses) { lastException = null;/*from w ww . jav a 2 s .c om*/ try { logger.debug("Connecting to AMQP host: " + brokerAddress.getHost() + " and port: " + brokerAddress.getPort()); connectionFactory.setHost(brokerAddress.getHost()); connectionFactory.setPort(brokerAddress.getPort()); connection = connectionFactory.newConnection(receiverExecutor); connection.addShutdownListener(new ShutdownListener() { public void shutdownCompleted(final ShutdownSignalException sse) { if (sse.isInitiatedByApplication()) { return; } forceReconnect("Connection shutdown detected for: " + getName(), sse); } }); logger.info("Connected to AMQP host: " + brokerAddress.getHost() + " and port: " + brokerAddress.getPort()); break; } catch (final Exception e) { logger.error("Error occurred when connecting to AMQP host: " + brokerAddress.getHost() + " and port: " + brokerAddress.getPort(), e); lastException = e; } } if (lastException != null) { throw lastException; } }
From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryIntegrationTests.java
License:Apache License
@Test public void testHardErrorAndReconnect() throws Exception { RabbitTemplate template = new RabbitTemplate(connectionFactory); RabbitAdmin admin = new RabbitAdmin(connectionFactory); Queue queue = new Queue("foo"); admin.declareQueue(queue);/*from w w w .ja v a 2 s. com*/ final String route = queue.getName(); final CountDownLatch latch = new CountDownLatch(1); try { template.execute(new ChannelCallback<Object>() { @Override public Object doInRabbit(Channel channel) throws Exception { channel.getConnection().addShutdownListener(new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException cause) { logger.info("Error", cause); latch.countDown(); // This will be thrown on the Connection thread just before it dies, so basically ignored throw new RuntimeException(cause); } }); String tag = channel.basicConsume(route, new DefaultConsumer(channel)); // Consume twice with the same tag is a hard error (connection will be reset) String result = channel.basicConsume(route, false, tag, new DefaultConsumer(channel)); fail("Expected IOException, got: " + result); return null; } }); fail("Expected AmqpIOException"); } catch (AmqpIOException e) { // expected } template.convertAndSend(route, "message"); assertTrue(latch.await(1000, TimeUnit.MILLISECONDS)); String result = (String) template.receiveAndConvert(route); assertEquals("message", result); result = (String) template.receiveAndConvert(route); assertEquals(null, result); }
From source file:rapture.exchange.rabbitmq.RabbitExchangeHandler.java
License:Open Source License
@Override public synchronized void setConfig(Map<String, String> config) { // Attempt to bind to RabbitMQ ConnectionFactory factory = new ConnectionFactory(); logger.info(Messages.getString("RabbitExchangeHandler.config")); //$NON-NLS-1$ try {/*from www . j a v a 2 s. c om*/ String uri = MultiValueConfigLoader.getConfig("RABBITMQ-" //$NON-NLS-1$ + instanceName); if (uri == null || uri.isEmpty()) { uri = "amqp://guest:guest@localhost:5672/%2f"; //$NON-NLS-1$ } factory.setUri(uri); factory.setAutomaticRecoveryEnabled(true); logger.debug(Messages.getString("RabbitExchangeHandler.creatingChannel")); //$NON-NLS-1$ connection = factory.newConnection(); connection.addShutdownListener(new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException cause) { // This should theoretically be called when we disconnect // from RabbitMQ, but due to a bug in the client library it // instead gets invoked // when reconnecting. This may change in future versions of // amqp-client so may need to revisit logger.info("Reconnected to RabbitMQ"); } }); logger.debug(Messages.getString("RabbitExchangeHandler.connectionMade")); //$NON-NLS-1$ channel = connection.createChannel(); channel.addShutdownListener(new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException cause) { logger.info("Disconnected from RabbitMQ. Cause :" + cause.getMessage()); logger.debug(ExceptionToString.format(cause)); } }); logger.debug(Messages.getString("RabbitExchangeHandler.channelCreated")); //$NON-NLS-1$ channel.basicQos(100); channel.addReturnListener(new ReturnListener() { @Override public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException { logger.debug( String.format(Messages.getString("RabbitExchangeHandler.returnListener"), replyCode, //$NON-NLS-1$ replyText)); } }); channel.addFlowListener(new FlowListener() { @Override public void handleFlow(boolean active) throws IOException { logger.debug(String.format(Messages.getString("RabbitExchangeHandler.Flow"), active)); //$NON-NLS-1$ } }); replyQueueName = channel.queueDeclare().getQueue(); logger.info("RPC reply queue is " + replyQueueName); consumer = new QueueingConsumer(channel); channel.basicConsume(replyQueueName, true, consumer); } catch (Exception e) { String message = Messages.getString("RabbitExchangeHandler.noConnect"); throw RaptureExceptionFactory.create(HttpURLConnection.HTTP_INTERNAL_ERROR, message, e); } }