Search in sources :

Example 11 with Channel

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

the class RabbitMQConsumerIntTest method sentMessageIsReceived.

@Test
public void sentMessageIsReceived() throws InterruptedException, IOException, TimeoutException {
    to.expectedMessageCount(1);
    to.expectedHeaderReceived(RabbitMQConstants.REPLY_TO, "myReply");
    AMQP.BasicProperties.Builder properties = new AMQP.BasicProperties.Builder();
    properties.replyTo("myReply");
    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) Test(org.junit.Test)

Example 12 with Channel

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

the class RabbitMQConsumerIntTest method sentMessageIsReceivedWithHeadersRouting.

/**
     * Tests the proper rabbit binding arguments are in place when the headersExchangeWithQueue is created.
     * Should only receive messages with the header [foo=bar]
     */
@Test
public void sentMessageIsReceivedWithHeadersRouting() throws InterruptedException, IOException, TimeoutException {
    //should only be one message that makes it through because only
    //one has the correct header set
    to.expectedMessageCount(1);
    Channel channel = connection().createChannel();
    channel.basicPublish(HEADERS_EXCHANGE, "", propertiesWithHeader("foo", "bar"), MSG.getBytes());
    channel.basicPublish(HEADERS_EXCHANGE, "", null, MSG.getBytes());
    channel.basicPublish(HEADERS_EXCHANGE, "", propertiesWithHeader("foo", "bra"), MSG.getBytes());
    to.assertIsSatisfied();
}
Also used : Channel(com.rabbitmq.client.Channel) Test(org.junit.Test)

Example 13 with Channel

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

the class RabbitConsumer method openChannel.

/**
     * Open channel
     */
private Channel openChannel(Connection conn) throws IOException {
    log.trace("Creating channel...");
    Channel channel = conn.createChannel();
    log.debug("Created channel: {}", channel);
    // setup the basicQos
    if (consumer.getEndpoint().isPrefetchEnabled()) {
        channel.basicQos(consumer.getEndpoint().getPrefetchSize(), consumer.getEndpoint().getPrefetchCount(), consumer.getEndpoint().isPrefetchGlobal());
    }
    // reconnections.
    if (consumer.getEndpoint().isDeclare()) {
        consumer.getEndpoint().declareExchangeAndQueue(channel);
    }
    return channel;
}
Also used : Channel(com.rabbitmq.client.Channel)

Example 14 with Channel

use of com.rabbitmq.client.Channel in project voltdb by VoltDB.

the class ExportRabbitMQVerifier method run.

public void run() throws IOException, InterruptedException {
    final Connection connection = m_connFactory.newConnection();
    final Channel channel = connection.createChannel();
    try {
        channel.exchangeDeclare(m_exchangeName, "topic", true);
        String dataQueue = channel.queueDeclare().getQueue();
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_PARTITIONED_TABLE.#");
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_PARTITIONED_TABLE2.#");
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_REPLICATED_TABLE.#");
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_PARTITIONED_TABLE_FOO.#");
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_PARTITIONED_TABLE2_FOO.#");
        channel.queueBind(dataQueue, m_exchangeName, "EXPORT_REPLICATED_TABLE_FOO.#");
        String doneQueue = channel.queueDeclare().getQueue();
        channel.queueBind(doneQueue, m_exchangeName, "EXPORT_DONE_TABLE.#");
        channel.queueBind(doneQueue, m_exchangeName, "EXPORT_DONE_TABLE_FOO.#");
        // Setup callback for data stream
        channel.basicConsume(dataQueue, false, createConsumer(channel));
        // Setup callback for the done message
        QueueingConsumer doneConsumer = new QueueingConsumer(channel);
        channel.basicConsume(doneQueue, true, doneConsumer);
        // Wait until the done message arrives, then verify count
        final QueueingConsumer.Delivery doneMsg = doneConsumer.nextDelivery();
        final long expectedRows = Long.parseLong(ExportOnServerVerifier.RoughCSVTokenizer.tokenize(new String(doneMsg.getBody(), Charsets.UTF_8))[6]);
        while (expectedRows > m_verifiedRows) {
            Thread.sleep(1000);
            System.err.println("Expected " + expectedRows + " " + m_verifiedRows);
        }
    } finally {
        tearDown(channel);
        channel.close();
        connection.close();
    }
}
Also used : Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) QueueingConsumer(com.rabbitmq.client.QueueingConsumer)

Example 15 with Channel

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

the class RabbitMQEventBus method publish.

// publish event on to the exchange created on AMQP server
@Override
public void publish(Event event) throws EventBusException {
    String routingKey = createRoutingKey(event);
    String eventDescription = event.getDescription();
    try {
        Connection connection = getConnection();
        Channel channel = createChannel(connection);
        createExchange(channel, amqpExchangeName);
        publishEventToExchange(channel, amqpExchangeName, routingKey, eventDescription);
        channel.close();
    } catch (AlreadyClosedException e) {
        closeConnection();
        throw new EventBusException("Failed to publish event to message broker as connection to AMQP broker in lost");
    } catch (Exception e) {
        throw new EventBusException("Failed to publish event to message broker due to " + e.getMessage());
    }
}
Also used : Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) EventBusException(org.apache.cloudstack.framework.events.EventBusException) AlreadyClosedException(com.rabbitmq.client.AlreadyClosedException) ConfigurationException(javax.naming.ConfigurationException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ConnectException(java.net.ConnectException) EventBusException(org.apache.cloudstack.framework.events.EventBusException) IOException(java.io.IOException) AlreadyClosedException(com.rabbitmq.client.AlreadyClosedException)

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