Example usage for com.rabbitmq.client ShutdownListener ShutdownListener

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

Introduction

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

Prototype

ShutdownListener

Source Link

Usage

From source file:org.apache.airavata.messaging.core.impl.RabbitMQConsumer.java

License:Apache License

private void createConnection() throws AiravataException {
    try {//w  ww  . jav  a  2  s. co m
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(url);
        connection = connectionFactory.newConnection();
        connection.addShutdownListener(new ShutdownListener() {
            public void shutdownCompleted(ShutdownSignalException cause) {
            }
        });
        log.info("connected to rabbitmq: " + connection + " for " + exchangeName);

        channel = connection.createChannel();
        channel.exchangeDeclare(exchangeName, "topic", false);

    } catch (Exception e) {
        String msg = "could not open channel for exchange " + exchangeName;
        log.error(msg);
        throw new AiravataException(msg, e);
    }
}

From source file:org.apache.airavata.messaging.core.impl.RabbitMQProcessConsumer.java

License:Apache License

private void createConnection() throws AiravataException {
    try {//  w ww  .  ja v a 2  s  .  co m
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(url);
        connection = connectionFactory.newConnection();
        connection.addShutdownListener(new ShutdownListener() {
            public void shutdownCompleted(ShutdownSignalException cause) {
            }
        });
        log.info("connected to rabbitmq: " + connection + " for default");

        channel = connection.createChannel();
        //            channel.exchangeDeclare(taskLaunchExchangeName, "fanout");

    } catch (Exception e) {
        String msg = "could not open channel for exchange default";
        log.error(msg);
        throw new AiravataException(msg, e);
    }
}

From source file:org.apache.airavata.messaging.core.impl.RabbitMQProcessLaunchConsumer.java

License:Apache License

private void createConnection() throws AiravataException {
    try {//from   w w w .  j a va  2s  .  co m
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(url);
        connectionFactory.setAutomaticRecoveryEnabled(true);
        connection = connectionFactory.newConnection();
        connection.addShutdownListener(new ShutdownListener() {
            public void shutdownCompleted(ShutdownSignalException cause) {
            }
        });
        log.info("connected to rabbitmq: " + connection + " for " + taskLaunchExchangeName);

        channel = connection.createChannel();
        channel.basicQos(prefetchCount);

        //            channel.exchangeDeclare(taskLaunchExchangeName, "fanout");

    } catch (Exception e) {
        String msg = "could not open channel for exchange " + taskLaunchExchangeName;
        log.error(msg);
        throw new AiravataException(msg, e);
    }
}

From source file:org.apache.airavata.messaging.core.impl.RabbitMQProducer.java

License:Apache License

private Connection createConnection() throws IOException {
    try {/*from  www . j  a v a 2 s  .c  o m*/
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(url);
        connectionFactory.setAutomaticRecoveryEnabled(true);
        Connection connection = connectionFactory.newConnection();
        connection.addShutdownListener(new ShutdownListener() {
            public void shutdownCompleted(ShutdownSignalException cause) {
            }
        });
        log.info("connected to rabbitmq: " + connection + " for " + exchangeName);
        return connection;
    } catch (Exception e) {
        log.info("connection failed to rabbitmq: " + connection + " for " + exchangeName);
        return null;
    }
}

From source file:org.apache.airavata.messaging.core.impl.RabbitMQPublisher.java

License:Apache License

private void connect() throws AiravataException {
    try {//from w w  w .j  a  va  2 s  . c o  m
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(properties.getBrokerUrl());
        connectionFactory.setAutomaticRecoveryEnabled(properties.isAutoRecoveryEnable());
        connection = connectionFactory.newConnection();
        connection.addShutdownListener(new ShutdownListener() {
            public void shutdownCompleted(ShutdownSignalException cause) {
            }
        });
        log.info("connected to rabbitmq: " + connection + " for " + properties.getExchangeName());
        channel = connection.createChannel();
        if (properties.getPrefetchCount() > 0) {
            channel.basicQos(properties.getPrefetchCount());
        }

        if (properties.getExchangeName() != null) {
            channel.exchangeDeclare(properties.getExchangeName(), properties.getExchangeType(), true); //durable
        }
    } catch (Exception e) {
        String msg = "RabbitMQ connection issue for exchange : " + properties.getExchangeName();
        log.error(msg);
        throw new AiravataException(msg, e);
    }

}

From source file:org.apache.airavata.messaging.core.impl.RabbitMQStatusConsumer.java

License:Apache License

private void createConnection() throws AiravataException {
    try {//  w  w  w.ja  va 2  s.c  om
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(url);
        connectionFactory.setAutomaticRecoveryEnabled(true);
        connection = connectionFactory.newConnection();
        connection.addShutdownListener(new ShutdownListener() {
            public void shutdownCompleted(ShutdownSignalException cause) {
            }
        });
        log.info("connected to rabbitmq: " + connection + " for " + exchangeName);

        channel = connection.createChannel();
        channel.basicQos(prefetchCount);
        channel.exchangeDeclare(exchangeName, EXCHANGE_TYPE, false);

    } catch (Exception e) {
        String msg = "could not open channel for exchange " + exchangeName;
        log.error(msg);
        throw new AiravataException(msg, e);
    }
}

From source file:org.apache.airavata.messaging.core.impl.RabbitMQSubscriber.java

License:Apache License

private void addShutdownListener() {
    connection.addShutdownListener(new ShutdownListener() {
        public void shutdownCompleted(ShutdownSignalException cause) {
            log.error("RabbitMQ connection " + connection + " for " + properties.getExchangeName()
                    + " has been shut down", cause);
        }/* ww  w. jav a  2s. c  o m*/
    });
}

From source file:org.apache.airavata.messaging.core.impl.RabbitMQTaskLaunchConsumer.java

License:Apache License

private void createConnection() throws AiravataException {
    try {/*from w  w  w  . jav  a 2  s .com*/
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri(url);
        connection = connectionFactory.newConnection();
        connection.addShutdownListener(new ShutdownListener() {
            public void shutdownCompleted(ShutdownSignalException cause) {
            }
        });
        log.info("connected to rabbitmq: " + connection + " for " + taskLaunchExchangeName);

        channel = connection.createChannel();
        //            channel.exchangeDeclare(taskLaunchExchangeName, "fanout");

    } catch (Exception e) {
        String msg = "could not open channel for exchange " + taskLaunchExchangeName;
        log.error(msg);
        throw new AiravataException(msg, e);
    }
}

From source file:org.apache.druid.firehose.rabbitmq.RabbitMQFirehoseFactory.java

License:Apache License

@Override
public Firehose connect(final InputRowParser<ByteBuffer> firehoseParser, File temporaryDirectory)
        throws IOException {
    ConnectionOptions lyraOptions = new ConnectionOptions(this.connectionFactory);
    Config lyraConfig = new Config().withRecoveryPolicy(new RetryPolicy().withMaxRetries(config.getMaxRetries())
            .withRetryInterval(Duration.seconds(config.getRetryIntervalSeconds()))
            .withMaxDuration(Duration.seconds(config.getMaxDurationSeconds())));

    String queue = config.getQueue();
    String exchange = config.getExchange();
    String routingKey = config.getRoutingKey();

    boolean durable = config.isDurable();
    boolean exclusive = config.isExclusive();
    boolean autoDelete = config.isAutoDelete();

    final Connection connection = Connections.create(lyraOptions, lyraConfig);

    connection.addShutdownListener(new ShutdownListener() {
        @Override//w  w  w . j  a  v  a 2s  . c o m
        public void shutdownCompleted(ShutdownSignalException cause) {
            log.warn(cause, "Connection closed!");
        }
    });

    final Channel channel = connection.createChannel();
    channel.queueDeclare(queue, durable, exclusive, autoDelete, null);
    channel.queueBind(queue, exchange, routingKey);
    channel.addShutdownListener(new ShutdownListener() {
        @Override
        public void shutdownCompleted(ShutdownSignalException cause) {
            log.warn(cause, "Channel closed!");
        }
    });

    // We create a QueueingConsumer that will not auto-acknowledge messages since that
    // happens on commit().
    final QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, false, consumer);

    return new Firehose() {
        /**
         * Storing the latest row as a member variable should be safe since this will only be run
         * by a single thread.
         */
        private InputRow nextRow;

        /**
         * Store the latest delivery tag to be able to commit (acknowledge) the message delivery up to
         * and including this tag. See commit() for more detail.
         */
        private long lastDeliveryTag;

        private Iterator<InputRow> nextIterator = Collections.emptyIterator();

        @Override
        public boolean hasMore() {
            nextRow = null;
            try {
                if (nextIterator.hasNext()) {
                    nextRow = nextIterator.next();
                    return true;
                }
                // Wait for the next delivery. This will block until something is available.
                final Delivery delivery = consumer.nextDelivery();
                if (delivery != null) {
                    lastDeliveryTag = delivery.getEnvelope().getDeliveryTag();
                    nextIterator = firehoseParser.parseBatch(ByteBuffer.wrap(delivery.getBody())).iterator();
                    if (nextIterator.hasNext()) {
                        nextRow = nextIterator.next();
                        // If delivery is non-null, we report that there is something more to process.
                        return true;
                    }
                }
            } catch (InterruptedException e) {
                // A little unclear on how we should handle this.

                // At any rate, we're in an unknown state now so let's log something and return false.
                log.wtf(e, "Got interrupted while waiting for next delivery. Doubt this should ever happen.");
            }

            // This means that delivery is null or we caught the exception above so we report that we have
            // nothing more to process.
            return false;
        }

        @Nullable
        @Override
        public InputRow nextRow() {
            if (nextRow == null) {
                //Just making sure.
                log.wtf("I have nothing in delivery. Method hasMore() should have returned false.");
                return null;
            }

            return nextRow;
        }

        @Override
        public Runnable commit() {
            // This method will be called from the same thread that calls the other methods of
            // this Firehose. However, the returned Runnable will be called by a different thread.
            //
            // It should be (thread) safe to copy the lastDeliveryTag like we do below and then
            // acknowledge values up to and including that value.
            return new Runnable() {
                // Store (copy) the last delivery tag to "become" thread safe.
                final long deliveryTag = lastDeliveryTag;

                @Override
                public void run() {
                    try {
                        log.info("Acknowledging delivery of messages up to tag: " + deliveryTag);

                        // Acknowledge all messages up to and including the stored delivery tag.
                        channel.basicAck(deliveryTag, true);
                    } catch (IOException e) {
                        log.error(e, "Unable to acknowledge message reception to message queue.");
                    }
                }
            };
        }

        @Override
        public void close() throws IOException {
            log.info("Closing connection to RabbitMQ");
            channel.close();
            connection.close();
        }
    };
}

From source file:org.graylog2.inputs.amqp.Consumer.java

License:Open Source License

public void connect() throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostname);/*from w  ww  . ja v  a 2s.  c o m*/
    factory.setPort(port);

    // Authenticate?
    if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
        factory.setUsername(username);
        factory.setPassword(password);
    }

    connection = factory.newConnection();
    channel = connection.createChannel();

    if (prefetchCount > 0) {
        channel.basicQos(prefetchCount);

        LOG.info("AMQP prefetch count overriden to <{}>.", prefetchCount);
    }

    connection.addShutdownListener(new ShutdownListener() {
        @Override
        public void shutdownCompleted(ShutdownSignalException cause) {
            while (true) {
                try {
                    LOG.error("AMQP connection lost! Trying reconnect in 1 second.");

                    Thread.sleep(1000);

                    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);
                } catch (InterruptedException ignored) {
                }
            }
        }
    });
}