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.mule.transport.amqp.AmqpConnector.java

License:Open Source License

public void closeChannel(final Channel channel) throws ConnectException {
    // FIXME remove when http://www.mulesoft.org/jira/browse/MULE-5290 is fixed
    if (!channel.isOpen()) {
        logger.warn(// w  w  w.j  ava  2s .  c  om
                "Attempting to close an already closed channel (probably due to http://www.mulesoft.org/jira/browse/MULE-5290)");
        return;
    }

    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Closing channel: " + channel);
        }

        channel.close();

        if (logger.isDebugEnabled()) {
            logger.debug("Closed channel: " + channel);
        }
    } catch (final IOException ioe) {
        throw new ConnectException(MessageFactory.createStaticMessage("Error when closing channel: " + channel),
                ioe, this);
    }
}

From source file:org.mule.transport.amqp.harness.TestConnectionManager.java

License:Open Source License

public void disposeChannel(Channel channel) throws IOException {
    if (channel == null) {
        return;/*from   ww  w .  ja v  a  2s . c  o  m*/
    }

    channel.close();
    if (channelCount.decrementAndGet() == 0) {
        connection.close();
    }
}

From source file:org.mule.transport.amqp.internal.client.ChannelHandler.java

License:Open Source License

public void closeChannel(final Channel channel) throws ConnectException {
    if (channel == null) {
        return;//  w  w  w.j  ava  2s  .c om
    }

    if (!channel.isOpen()) {
        return;
    }

    try {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Closing channel: " + channel);
        }

        channel.close();

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Closed channel: " + channel);
        }
    } catch (AlreadyClosedException alreadyClosedException) {
        LOGGER.warn("Attempted to close an already closed channel: " + channel, alreadyClosedException);
    } catch (final Exception e) {
        LOGGER.warn("Failed to close channel: " + channel, e);
    }
}

From source file:org.ninjav.rabbitmq.SendTest.java

@Test
public void canSendMessageToQueue() throws IOException, TimeoutException {

    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 < 1000000; i++) {
        String message = "Hello world " + i;
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
    }//from  ww w. j  a  v a 2 s .c o  m

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

From source file:org.objectweb.joram.mom.dest.amqp.AmqpAcquisition.java

License:Open Source License

public void stop() {
    if (logger.isLoggable(BasicLevel.DEBUG)) {
        logger.log(BasicLevel.DEBUG, "Stop AmqpAcquisition.");
    }//from  w w w . j a  va 2s.co  m

    connectionUpdater.removeUpdateListener(this);

    closing = true;
    synchronized (channels) {
        for (Channel channel : channels.values()) {
            try {
                channel.close();
            } catch (Exception exc) {
                if (logger.isLoggable(BasicLevel.DEBUG)) {
                    logger.log(BasicLevel.DEBUG, "Error while stopping AmqpAcquisition.", exc);
                }
            }
        }
        channels.clear();
    }
}

From source file:org.objectweb.joram.mom.dest.amqp.AmqpDistribution.java

License:Open Source License

public void close() {
    for (Channel channel : channels.values()) {
        try {// ww  w. j a v a2 s. c o m
            channel.close();
        } catch (IOException exc) {
            if (logger.isLoggable(BasicLevel.DEBUG)) {
                logger.log(BasicLevel.DEBUG, "Error while stopping AmqpDistribution.", exc);
            }
        }
    }
    channels.clear();
}

From source file:org.objectweb.proactive.extensions.amqp.federation.AMQPFederationUtils.java

License:Open Source License

static boolean pingRemoteObject(String queueName, URI uri) throws IOException {
    RpcReusableChannel reusableChannel = AMQPFederationUtils.getRpcChannel(uri);
    Channel channel = reusableChannel.getChannel();
    try {// w w w  .ja v  a2s. co m
        BasicProperties props = new BasicProperties.Builder().replyTo(reusableChannel.getReplyQueue())
                .type(AMQPFederationRemoteObjectServer.PING_MESSAGE_TYPE).build();

        channel.basicPublish(AMQPFederationConfig.PA_AMQP_FEDERATION_RPC_EXCHANGE_NAME.getValue(), queueName,
                props, null);

        QueueingConsumer.Delivery delivery = null;

        try {
            delivery = reusableChannel.getReplyQueueConsumer()
                    .nextDelivery(AMQPFederationConfig.PA_AMQP_FEDERATION_PING_TIMEOUT.getValue());
        } catch (InterruptedException e) {
            logger.warn("AMQPFederationUtils.isQueueExists is interrupted", e);
        }

        if (delivery == null) {
            reusableChannel.close();
            return false;
        } else {
            reusableChannel.returnChannel();
            return true;
        }
    } catch (IOException e) {
        channel.close();
        throw e;
    }
}

From source file:org.openbaton.common.vnfm_sdk.amqp.AbstractVnfmSpringAmqp.java

License:Apache License

@Override
protected void unregister() {
    try {/*from w  w  w  .j a v  a 2  s .com*/
        ((VnfmSpringHelperRabbit) vnfmHelper).sendMessageToQueue(RabbitConfiguration.queueName_vnfmUnregister,
                vnfmManagerEndpoint);
    } catch (IllegalStateException e) {
        log.warn("Got exception while unregistering trying to do it manually");
        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost(rabbitHost);
        Connection connection = null;
        try {
            connection = factory.newConnection();

            Channel channel = connection.createChannel();

            String message = gson.toJson(vnfmManagerEndpoint);
            channel.basicPublish("openbaton-exchange", RabbitConfiguration.queueName_vnfmUnregister,
                    MessageProperties.TEXT_PLAIN, message.getBytes("UTF-8"));
            log.debug("Sent '" + message + "'");

            channel.close();
            connection.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

From source file:org.openbaton.plugin.utils.PluginCaller.java

License:Apache License

public Serializable executeRPC(String methodName, Collection<Serializable> args, Type returnType)
        throws IOException, InterruptedException, PluginException {

    Channel channel = connection.createChannel();
    String replyQueueName = channel.queueDeclare().getQueue();
    String exchange = "plugin-exchange";
    channel.queueBind(replyQueueName, exchange, replyQueueName);
    QueueingConsumer consumer = new QueueingConsumer(channel);
    String consumerTag = channel.basicConsume(replyQueueName, true, consumer);

    //Check if plugin is still up
    if (!RabbitManager.getQueues(brokerIp, username, password, managementPort).contains(pluginId))
        throw new PluginException("Plugin with id: " + pluginId + " not existing anymore...");

    String response;/*www . j  a  v  a2 s. c  om*/
    String corrId = UUID.randomUUID().toString();
    PluginMessage pluginMessage = new PluginMessage();
    pluginMessage.setMethodName(methodName);
    pluginMessage.setParameters(args);
    String message = gson.toJson(pluginMessage);

    BasicProperties props = new Builder().correlationId(corrId).replyTo(replyQueueName).build();

    channel.basicPublish(exchange, pluginId, props, message.getBytes());

    if (returnType != null) {

        while (true) {
            Delivery delivery = consumer.nextDelivery();
            if (delivery.getProperties().getCorrelationId().equals(corrId)) {
                response = new String(delivery.getBody());
                log.trace("received: " + response);
                break;
            } else {
                log.error("Received Message with wrong correlation id");
                throw new PluginException(
                        "Received Message with wrong correlation id. This should not happen, if it does please call us.");
            }
        }

        channel.queueDelete(replyQueueName);
        try {
            channel.close();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        JsonObject jsonObject = gson.fromJson(response, JsonObject.class);

        JsonElement exceptionJson = jsonObject.get("exception");
        if (exceptionJson == null) {
            JsonElement answerJson = jsonObject.get("answer");

            Serializable ret = null;

            if (answerJson.isJsonPrimitive()) {
                ret = gson.fromJson(answerJson.getAsJsonPrimitive(), returnType);
            } else if (answerJson.isJsonArray()) {
                ret = gson.fromJson(answerJson.getAsJsonArray(), returnType);
            } else
                ret = gson.fromJson(answerJson.getAsJsonObject(), returnType);

            log.trace("answer is: " + ret);
            return ret;
        } else {
            PluginException pluginException;
            try {
                pluginException = new PluginException(
                        gson.fromJson(exceptionJson.getAsJsonObject(), VimDriverException.class));
                log.debug("Got Vim Driver Exception with server: "
                        + ((VimDriverException) pluginException.getCause()).getServer());
            } catch (Exception ignored) {
                pluginException = new PluginException(
                        gson.fromJson(exceptionJson.getAsJsonObject(), Throwable.class));
            }
            throw pluginException;
        }
    } else
        return null;
}

From source file:org.opendaylight.federationmessagequeue.impl.RabbitMessageBus.java

License:Open Source License

@Override
public void destroyQueue(String queueName) {
    LOG.info("Started delete of queue {}", queueName);

    // lookup connection by queueName
    MessageBusConnectionData messageBusConnectionData = queueNameToConnectionData.get(queueName);
    if (messageBusConnectionData != null) {
        // get channel from active connections map
        Channel channel = messageBusConnectionData.channel;
        String brokerIp = messageBusConnectionData.brokerIp;
        Connection conn = messageBusConnectionData.conn;
        try {/*from   w  w w .j  a  va 2  s .co m*/
            // kill the queue dont wait for confirmation
            if (channel != null) {
                try {
                    channel.queueDelete(queueName);
                    LOG.info("Deleted queue {} successfully", queueName);
                } catch (IOException e) {
                    LOG.warn("Failed to delete queue {} msg: {}", queueName, e.getMessage());
                }
                channel.close();
            } else {
                LOG.warn("Null channel while deleting queue {} on broker {}", queueName, brokerIp);
            }
            if (conn != null) {
                conn.close();
            } else {
                LOG.warn("Null connection while deleting queue {} on broker {}", queueName, brokerIp);
            }
        } catch (IOException | TimeoutException e) {
            LOG.warn("Failed to close channel while deleting queue {} on broker {}", queueName, brokerIp, e);
        }
        // remove the queue from the internal queue list
        queueNameToConnectionData.remove(queueName);
    } else {
        LOG.warn("Cancelled deletion of queue {} because queueName not found in queueNameToConnectionData",
                queueName);
    }

}