Search in sources :

Example 1 with Channel

use of com.rabbitmq.client.Channel in project camel by apache.

the class RabbitMQProducer method execute.

/**
     * Do something with a pooled channel (similar to Spring JDBC TransactionTemplate#execute)
     */
private <T> T execute(ChannelCallback<T> callback) throws Exception {
    Channel channel;
    try {
        channel = channelPool.borrowObject();
    } catch (IllegalStateException e) {
        // Since this method is not synchronized its possible the
        // channelPool has been cleared by another thread
        checkConnectionAndChannelPool();
        channel = channelPool.borrowObject();
    }
    if (!channel.isOpen()) {
        log.warn("Got a closed channel from the pool");
        // Reconnect if another thread hasn't yet
        checkConnectionAndChannelPool();
        channel = channelPool.borrowObject();
    }
    try {
        return callback.doWithChannel(channel);
    } finally {
        channelPool.returnObject(channel);
    }
}
Also used : Channel(com.rabbitmq.client.Channel)

Example 2 with Channel

use of com.rabbitmq.client.Channel in project camel by apache.

the class TemporaryQueueReplyManager method createListenerContainer.

@Override
protected Connection createListenerContainer() throws Exception {
    log.debug("Creating connection");
    Connection conn = endpoint.connect(executorService);
    log.debug("Creating channel");
    Channel channel = conn.createChannel();
    // setup the basicQos
    if (endpoint.isPrefetchEnabled()) {
        channel.basicQos(endpoint.getPrefetchSize(), endpoint.getPrefetchCount(), endpoint.isPrefetchGlobal());
    }
    //Let the server pick a random name for us
    DeclareOk result = channel.queueDeclare();
    log.info("Using temporary queue name: {}", result.getQueue());
    setReplyTo(result.getQueue());
    //TODO check for the RabbitMQConstants.EXCHANGE_NAME header
    channel.queueBind(getReplyTo(), endpoint.getExchangeName(), getReplyTo());
    consumer = new RabbitConsumer(this, channel);
    consumer.start();
    return conn;
}
Also used : DeclareOk(com.rabbitmq.client.AMQP.Queue.DeclareOk) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection)

Example 3 with Channel

use of com.rabbitmq.client.Channel in project camel by apache.

the class RabbitMQConsumerIntTest method sentMessageWithTimestampIsReceived.

@Test
public void sentMessageWithTimestampIsReceived() throws InterruptedException, IOException, TimeoutException {
    Date timestamp = currentTimestampWithoutMillis();
    to.expectedMessageCount(1);
    to.expectedHeaderReceived(RabbitMQConstants.TIMESTAMP, timestamp);
    AMQP.BasicProperties.Builder properties = new AMQP.BasicProperties.Builder();
    properties.timestamp(timestamp);
    Channel channel = connection().createChannel();
    channel.basicPublish(EXCHANGE, "", properties.build(), MSG.getBytes());
    to.assertIsSatisfied();
}
Also used : AMQP(com.rabbitmq.client.AMQP) RouteBuilder(org.apache.camel.builder.RouteBuilder) Channel(com.rabbitmq.client.Channel) Date(java.util.Date) Test(org.junit.Test)

Example 4 with Channel

use of com.rabbitmq.client.Channel in project camel by apache.

the class RabbitMQConsumerIntTest method sentMessageIsReceivedWithHeadersRoutingMultiValueMapBindings.

@Test
public void sentMessageIsReceivedWithHeadersRoutingMultiValueMapBindings() throws Exception {
    to.expectedMessageCount(3);
    Channel channel = connection().createChannel();
    channel.basicPublish("ex7", "", propertiesWithHeader("fizz", "buzz"), MSG.getBytes());
    channel.basicPublish("ex7", "", propertiesWithHeader("fizz", "buzz"), MSG.getBytes());
    channel.basicPublish("ex7", "", propertiesWithHeader("fizz", "buzz"), MSG.getBytes());
    channel.basicPublish("ex7", "", propertiesWithHeader("fizz", "nope"), MSG.getBytes());
    to.assertIsSatisfied();
}
Also used : Channel(com.rabbitmq.client.Channel) Test(org.junit.Test)

Example 5 with Channel

use of com.rabbitmq.client.Channel in project cloudstack by apache.

the class RabbitMQEventBus method stop.

@Override
public synchronized boolean stop() {
    if (s_connection.isOpen()) {
        for (String subscriberId : s_subscribers.keySet()) {
            Ternary<String, Channel, EventSubscriber> subscriberDetails = s_subscribers.get(subscriberId);
            Channel channel = subscriberDetails.second();
            String queueName = subscriberId;
            try {
                channel.queueDelete(queueName);
                channel.abort();
            } catch (IOException ioe) {
                s_logger.warn("Failed to delete queue: " + queueName + " on AMQP server due to " + ioe.getMessage());
            }
        }
    }
    closeConnection();
    return true;
}
Also used : EventSubscriber(org.apache.cloudstack.framework.events.EventSubscriber) Channel(com.rabbitmq.client.Channel) IOException(java.io.IOException)

Aggregations

Channel (com.rabbitmq.client.Channel)15 Connection (com.rabbitmq.client.Connection)7 IOException (java.io.IOException)5 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)4 ConnectException (java.net.ConnectException)4 Test (org.junit.Test)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 AMQP (com.rabbitmq.client.AMQP)3 AlreadyClosedException (com.rabbitmq.client.AlreadyClosedException)3 ConfigurationException (javax.naming.ConfigurationException)3 EventBusException (org.apache.cloudstack.framework.events.EventBusException)3 EventSubscriber (org.apache.cloudstack.framework.events.EventSubscriber)3 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)2 Date (java.util.Date)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 Ternary (com.cloud.utils.Ternary)1 DeclareOk (com.rabbitmq.client.AMQP.Queue.DeclareOk)1 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)1 Envelope (com.rabbitmq.client.Envelope)1 QueueingConsumer (com.rabbitmq.client.QueueingConsumer)1