Example usage for com.rabbitmq.client Channel close

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

Introduction

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

Prototype

@Override
void close() throws IOException, TimeoutException;

Source Link

Document

Close this channel with the com.rabbitmq.client.AMQP#REPLY_SUCCESS close code and message 'OK'.

Usage

From source file:org.apache.flume.RabbitMQUtil.java

License:Apache License

public static void close(Connection connection, com.rabbitmq.client.Channel channel) {
    if (null != channel) {
        try {//  ww  w .java 2  s. c o  m
            channel.close();
        } catch (Exception ex) {
            if (log.isErrorEnabled())
                log.error("Exception thrown while closing channel", ex);
        }
    }

    if (null != connection) {
        try {
            connection.close();
        } catch (Exception ex) {
            if (log.isErrorEnabled())
                log.error("Exception thrown while closing connection", ex);
        }
    }
}

From source file:org.apache.james.transport.mailets.AmqpForwardAttribute.java

License:Apache License

private void sendContent(Map<String, byte[]> content) throws IOException, TimeoutException {
    Connection connection = null;
    Channel channel = null;
    try {//from  ww w  .  j a va 2s  .com
        connection = connectionFactory.newConnection();
        channel = connection.createChannel();
        channel.exchangeDeclarePassive(exchange);
        sendContentOnChannel(channel, content);
    } finally {
        if (channel != null) {
            channel.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}

From source file:org.apache.niolex.rabbit.basic.Send.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.queueDeclare(QUEUE_NAME, false, false, false, null);
    for (int i = 0; i < 100; ++i) {
        String message = String.format("%02d %s", i, MockUtil.randString(5));
        ThreadUtil.sleepAtLeast(1000);/*  w w  w  .j a v  a2s .  co m*/
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
        System.out.println(" [x] Sent '" + message + "'");
    }

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

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

License:Apache License

public static void main(String[] argv) throws java.io.IOException, TimeoutException {

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

    channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

    for (int i = 0; i < 100; ++i) {
        String message = String.format("%02d %s", i, MockUtil.randString(5));
        ThreadUtil.sleepAtLeast(1000);//from ww  w.ja  va 2  s  .  c o m

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

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

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

License:Apache License

public static void main(String[] argv) throws java.io.IOException, TimeoutException {

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

    channel.exchangeDeclare(EXCHANGE_NAME, "direct");

    for (int i = 0; i < 100; ++i) {
        String message = String.format("%02d %s", i, MockUtil.randString(5));
        String severity = MockUtil.randInt(3) == 0 ? "error" : "info";
        ThreadUtil.sleepAtLeast(1000);/*w  ww .ja  v a  2  s.  com*/

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

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

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);// w w w  .j  a  v  a 2 s  .  com

        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.nutch.indexer.history.HistoryIndexer.java

License:Apache License

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

    if (channel == null) {
        return;/*from   ww  w .j  a v  a  2 s.  co  m*/
    }

    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.RabbitMQProducer.java

License:Open Source License

public boolean storeMessage(MessageContext synCtx) {
    if (synCtx == null) {
        return false;
    }//w ww  .j a  va 2s . c  o m
    if (connection == null) {
        if (logger.isDebugEnabled()) {
            logger.error(getId() + " cannot proceed. RabbitMQ Connection is null.");
        }
        logger.warn(getId() + ". Ignored MessageID : " + synCtx.getMessageID());
        return false;
    }
    StorableMessage message = MessageConverter.toStorableMessage(synCtx);
    boolean error = false;
    Throwable throwable = null;
    Channel channel = null;
    try {
        //Serializing message
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ObjectOutput objOut = new ObjectOutputStream(os);
        objOut.writeObject(message);
        byte[] byteForm = os.toByteArray();
        objOut.close();
        os.close();
        //building AMQP message
        AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties().builder();
        builder.messageId(synCtx.getMessageID());
        builder.deliveryMode(MessageProperties.MINIMAL_PERSISTENT_BASIC.getDeliveryMode());
        builder.priority(message.getPriority(DEFAULT_PRIORITY));
        channel = connection.createChannel();
        if (exchangeName == null) {
            channel.basicPublish("", queueName, builder.build(), byteForm);
        } else {
            channel.basicPublish(exchangeName, queueName, builder.build(), byteForm);
        }
    } catch (IOException e) {
        throwable = e;
        error = true;
        isConnectionError = true;
    } catch (Throwable t) {
        throwable = t;
        error = true;
    } finally {
        if (channel != null && channel.isOpen())
            try {
                channel.close();
            } catch (IOException e) {
                logger.error("Error when closing connection" + synCtx.getMessageID() + ". " + e);
            }
    }
    if (error) {
        String errorMsg = getId() + ". Ignored MessageID : " + synCtx.getMessageID()
                + ". Could not store message to store [" + store.getName() + "]. Error:"
                + throwable.getLocalizedMessage();
        logger.error(errorMsg, throwable);
        store.closeProducerConnection();
        connection = null;
        if (logger.isDebugEnabled()) {
            logger.debug(getId() + ". Ignored MessageID : " + synCtx.getMessageID());
        }
        return false;
    }
    if (logger.isDebugEnabled()) {
        logger.debug(getId() + ". Stored MessageID : " + synCtx.getMessageID());
    }
    store.enqueued();
    return true;
}

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

License:Open Source License

/**
 * Create a Queue to store messages, this will used queueName parameter
 *
 * @throws IOException : if its enable to create the queue or exchange
 *//*w  ww  .j  a  va2s.co m*/
private void setQueue() throws IOException {
    Channel channel = null;
    try {
        channel = producerConnection.createChannel();
        try {
            channel.queueDeclarePassive(queueName);
        } catch (java.io.IOException e) {
            logger.info("Queue :" + queueName + " not found.Declaring queue.");
            if (!channel.isOpen()) {
                channel = producerConnection.createChannel();
            }
            //Hashmap with names and parameters can be used in the place of null argument
            //Eg: adding dead letter exchange to the queue
            //params: (queueName, durable, exclusive, autoDelete, paramMap)
            channel.queueDeclare(queueName, true, false, false, null);
        }
        //declaring exchange
        if (exchangeName != null) {
            try {
                channel.exchangeDeclarePassive(exchangeName);
            } catch (java.io.IOException e) {
                logger.info("Exchange :" + exchangeName + " not found. Declaring exchange.");
                if (!channel.isOpen()) {
                    channel = producerConnection.createChannel();
                }
                //params : ( exchangeName, exchangeType, durable, autoDelete, paramMap )
                channel.exchangeDeclare(exchangeName, "direct", true, false, null);
            }
            channel.queueBind(queueName, exchangeName, routeKey);
        }
    } finally {
        channel.close();
    }
}

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();
    connection.close();// w  w w. ja v  a  2s.  com

}