Java Code Examples for com.rabbitmq.client.Channel#isOpen()

The following examples show how to use com.rabbitmq.client.Channel#isOpen() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AMQPPublisher.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Publishes message with provided AMQP properties (see
 * {@link BasicProperties}) to a pre-defined AMQP Exchange.
 *
 * @param bytes bytes representing a message.
 * @param properties instance of {@link BasicProperties}
 * @param exchange the name of AMQP exchange to which messages will be published.
 *            If not provided 'default' exchange will be used.
 * @param routingKey (required) the name of the routingKey to be used by AMQP-based
 *            system to route messages to its final destination (queue).
 */
void publish(byte[] bytes, BasicProperties properties, String routingKey, String exchange) {
    this.validateStringProperty("routingKey", routingKey);
    exchange = exchange == null ? "" : exchange.trim();
    if (exchange.length() == 0) {
        processLog.info("The 'exchangeName' is not specified. Messages will be sent to default exchange");
    }
    processLog.info("Successfully connected AMQPPublisher to " + this.connectionString + " and '" + exchange
            + "' exchange with '" + routingKey + "' as a routing key.");

    final Channel channel = getChannel();
    if (channel.isOpen()) {
        try {
            channel.basicPublish(exchange, routingKey, true, properties, bytes);
        } catch (Exception e) {
            throw new IllegalStateException("Failed to publish to Exchange '" + exchange + "' with Routing Key '" + routingKey + "'.", e);
        }
    } else {
        throw new IllegalStateException("This instance of AMQPPublisher is invalid since its publishingChannel is closed");
    }
}
 
Example 2
Source File: ReactorRabbitMQChannelPool.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Override
public void destroyObject(PooledObject<Channel> pooledObject) throws Exception {
    Channel channel = pooledObject.getObject();
    if (channel.isOpen()) {
        channel.close();
    }
}
 
Example 3
Source File: ReactorRabbitMQChannelPool.java    From james-project with Apache License 2.0 5 votes vote down vote up
private Channel borrowFromPool() throws Exception {
    Channel channel = pool.borrowObject(MAXIMUM_BORROW_TIMEOUT_IN_MS);
    if (!channel.isOpen()) {
        invalidateObject(channel);
        throw new ChannelClosedException("borrowed channel is already closed");
    }
    return channel;
}
 
Example 4
Source File: ReactorRabbitMQChannelPool.java    From james-project with Apache License 2.0 5 votes vote down vote up
private void invalidateObject(Channel channel) {
    try {
        pool.invalidateObject(channel);
        if (channel.isOpen()) {
            channel.close();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: RabbitMqUtils.java    From roboconf-platform with Apache License 2.0 5 votes vote down vote up
/**
 * Closes the connection to a channel.
 * @param channel the channel to close (can be null)
 * @throws IOException if something went wrong
 */
public static void closeConnection( Channel channel ) throws IOException {

	if( channel != null ) {
		if( channel.isOpen())
			channel.close();

		if( channel.getConnection().isOpen())
			channel.getConnection().close();
	}
}
 
Example 6
Source File: RabbitQueueFactory.java    From yacy_grid_mcp with GNU Lesser General Public License v2.1 4 votes vote down vote up
private Channel getChannel() throws IOException {
    getConnection();
    Channel channel = connection.createChannel();
    if (!channel.isOpen()) throw new IOException("no channel");
    return channel;
}