Example usage for com.rabbitmq.client Connection close

List of usage examples for com.rabbitmq.client Connection close

Introduction

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

Prototype

@Override
void close() throws IOException;

Source Link

Document

Close this connection and all its channels with the com.rabbitmq.client.AMQP#REPLY_SUCCESS close code and message 'OK'.

Usage

From source file:org.apache.niolex.rabbit.log.EmitLogTopic.java

License:Apache License

public static void main(String[] argv) throws Exception {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(EXCHANGE_NAME, "topic");

    for (int i = 0; i < 100; ++i) {
        String message = String.format("%02d %s", i, MockUtil.randString(5));
        ThreadUtil.sleepAtLeast(1000);/*www  .  ja  v  a 2 s.co m*/

        String routingKey = getSeverity() + "." + getFacility();

        channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
        System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
    }

    channel.close();
    connection.close();
}

From source file:org.apache.niolex.rabbit.rpc.RPCServer.java

License:Apache License

public static void main(String[] argv) {
    Connection connection = null;
    Channel channel = null;/*from ww w.ja va 2 s .  c  om*/
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

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

        channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);

        channel.basicQos(1);

        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(RPC_QUEUE_NAME, false, consumer);

        System.out.println(" [x] Awaiting RPC requests");

        while (true) {
            String response = null;

            QueueingConsumer.Delivery delivery = consumer.nextDelivery();

            BasicProperties props = delivery.getProperties();
            BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId())
                    .build();

            try {
                String message = new String(delivery.getBody(), "UTF-8");
                int n = Integer.parseInt(message);

                System.out.println(" [.] fib(" + message + ")");
                response = "" + fib(n);
            } catch (Exception e) {
                System.out.println(" [.] " + e.toString());
                response = "";
            } finally {
                channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes("UTF-8"));
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception ignore) {
            }
        }
    }
}

From source file:org.apache.nutch.indexer.history.HistoryIndexer.java

License:Apache License

private void closeChannelAndConnection(@Nullable final Channel channel, final boolean swallowErrors) {

    if (channel == null) {
        return;/*from  w  w  w.j a v a2  s  .c  om*/
    }

    try {
        final Connection connection = channel.getConnection();
        if (channel.isOpen()) {
            channel.close();
        }
        if (connection != null && connection.isOpen()) {
            connection.close();
        }
    } catch (IOException | TimeoutException e) {
        final String errMsg = "failed closing connection or channel";
        LOG.error(errMsg, e);
        if (!swallowErrors) {
            throw new RuntimeException(errMsg, e);
        }
    }
}

From source file:org.apache.synapse.message.store.impl.rabbitmq.RabbitMQStore.java

License:Open Source License

/**
 * Close any given connection//from w  w w  .  jav  a2 s. co  m
 *
 * @param connection
 * @return true if the connection closed successfully false otherwise
 */
public boolean closeConnection(Connection connection) {
    try {
        if (connection.isOpen()) {
            connection.close();
        }
        if (logger.isDebugEnabled()) {
            logger.debug(nameString() + " closed connection to RabbitMQ broker.");
        }
    } catch (IOException e) {
        return false;
    }
    return true;
}

From source file:org.apache.synapse.message.store.impl.rabbitmq.RabbitMQStore.java

License:Open Source License

/**
 * Cleans up the RabbitMQ Connection//from   www  . j ava 2s  .  c om
 *
 * @param connection RabbitMQ Connection
 * @param error      is this method called upon an error
 * @return {@code true} if the cleanup is successful. {@code false} otherwise.
 */
public boolean cleanup(Connection connection, boolean error) {
    if (connection == null && error) {
        return true;
    }
    try {
        if (connection != null && error && connection.isOpen()) {
            connection.close();
        }
    } catch (IOException e) {
        return false;
    }
    return true;
}

From source file:org.apache.synapse.tranport.amqp.AMQPTwoWayProducerClient.java

License:Apache License

public static void main(String[] args) throws IOException, InterruptedException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();

    Channel channel = connection.createChannel();

    AMQPTwoWayProducerClient.produceAndConsume(MESSAGE2, channel, "consumer", "consumerReply");

    channel.close();/*w  w w  .ja va  2 s.com*/
    connection.close();

}

From source file:org.axonframework.amqp.eventhandling.RabbitMQBenchmark.java

License:Apache License

public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
    final Connection connection = new ConnectionFactory().newConnection();
    final Channel channel = connection.createChannel();
    String queueName = channel.queueDeclare().getQueue();
    execute("Transactional and Channel pooling", createChannelPoolSharingThreads(connection, queueName));
    queueName = refreshQueue(channel, queueName);
    execute("Transactional, new Channel per tx", createChannelCreatingThreads(connection, queueName, true));
    queueName = refreshQueue(channel, queueName);
    execute("Non-transactional, new Channel per tx",
            createChannelCreatingThreads(connection, queueName, false));
    queueName = refreshQueue(channel, queueName);
    execute("Non-transactional, single Channel", createChannelSharingThreads(connection, queueName));
    channel.confirmSelect();//from   ww  w  . j a v a 2s .co  m
    connection.close();
}

From source file:org.axonframework.eventhandling.amqp.RabbitMQBenchmark.java

License:Apache License

public static void main(String[] args) throws IOException, InterruptedException {
    final Connection connection = new ConnectionFactory().newConnection();
    final Channel channel = connection.createChannel();
    String queueName = channel.queueDeclare().getQueue();
    execute("Transactional and Channel pooling", createChannelPoolSharingThreads(connection, queueName));
    queueName = refreshQueue(channel, queueName);
    execute("Transactional, new Channel per tx", createChannelCreatingThreads(connection, queueName, true));
    queueName = refreshQueue(channel, queueName);
    execute("Non-transactional, new Channel per tx",
            createChannelCreatingThreads(connection, queueName, false));
    queueName = refreshQueue(channel, queueName);
    execute("Non-transactional, single Channel", createChannelSharingThreads(connection, queueName));
    channel.confirmSelect();/* w  ww.j  ava  2 s.c  om*/
    connection.close();
}

From source file:org.ballerinalang.messaging.rabbitmq.nativeimpl.channel.listener.Stop.java

License:Open Source License

@Override
public void execute(Context context) {
    @SuppressWarnings(RabbitMQConstants.UNCHECKED)
    BMap<String, BValue> channelListObject = (BMap<String, BValue>) context.getRefArgument(0);
    @SuppressWarnings(RabbitMQConstants.UNCHECKED)
    BMap<String, BValue> channelOb = (BMap<String, BValue>) channelListObject
            .get(RabbitMQConstants.CHANNEL_REFERENCE);
    Channel channel = (Channel) channelOb.getNativeData(RabbitMQConstants.CHANNEL_NATIVE_OBJECT);
    if (channel == null) {
        throw new BallerinaException("ChannelListener not properly initialised");
    } else {/*from  w  ww  .j a  v a2  s. com*/
        try {
            Connection connection = channel.getConnection();
            channel.close();
            connection.close();
        } catch (IOException | TimeoutException exception) {
            RabbitMQUtils.returnError(RabbitMQConstants.CLOSE_CHANNEL_ERROR + " " + exception.getMessage(),
                    context, exception);
        }
    }
}

From source file:org.ballerinalang.messaging.rabbitmq.util.ConnectionUtils.java

License:Open Source License

/**
 * Closes the connection./*from  ww  w .  ja  va2 s . c o  m*/
 *
 * @param connection RabbitMQ Connection object.
 * @throws IOException If an I/O problem is encountered.
 */
private static void close(Connection connection) throws IOException {
    connection.close();
}