Search in sources :

Example 6 with Connection

use of com.rabbitmq.client.Connection in project elasticsearch-river-rabbitmq by elastic.

the class RabbitMQIntegrationTest method launchTest.

private void launchTest(XContentBuilder river, final int numMessages, final int numDocsPerMessage, InjectorHook injectorHook, boolean delete, boolean update) throws Exception {
    final String dbName = getDbName();
    logger.info(" --> create index [{}]", dbName);
    try {
        client().admin().indices().prepareDelete(dbName).get();
    } catch (IndexMissingException e) {
    // No worries.
    }
    try {
        createIndex(dbName);
    } catch (IndexMissingException e) {
    // No worries.
    }
    ensureGreen(dbName);
    logger.info("  -> Checking rabbitmq running");
    // We try to connect to RabbitMQ.
    // If it's not launched, we don't fail the test but only log it
    Channel channel = null;
    Connection connection = null;
    try {
        logger.info(" --> connecting to rabbitmq");
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(AMQP.PROTOCOL.PORT);
        connection = factory.newConnection();
    } catch (ConnectException ce) {
        throw new Exception("RabbitMQ service is not launched on localhost:" + AMQP.PROTOCOL.PORT + ". Can not start Integration test. " + "Launch `rabbitmq-server`.", ce);
    }
    try {
        logger.info("  -> Creating [{}] channel", dbName);
        channel = connection.createChannel();
        logger.info("  -> Creating queue [{}]", dbName);
        channel.queueDeclare(getDbName(), true, false, false, null);
        // We purge the queue in case of something is remaining there
        logger.info("  -> Purging [{}] channel", dbName);
        channel.queuePurge(getDbName());
        logger.info("  -> Put [{}] messages with [{}] documents each = [{}] docs", numMessages, numDocsPerMessage, numMessages * numDocsPerMessage);
        final Set<String> removed = new HashSet<String>();
        int nbUpdated = 0;
        for (int i = 0; i < numMessages; i++) {
            StringBuffer message = new StringBuffer();
            for (int j = 0; j < numDocsPerMessage; j++) {
                if (logger.isTraceEnabled()) {
                    logger.trace("  -> Indexing document [{}] - [{}][{}]", i + "_" + j, i, j);
                }
                message.append("{ \"index\" : { \"_index\" : \"" + dbName + "\", \"_type\" : \"typex\", \"_id\" : \"" + i + "_" + j + "\" } }\n");
                message.append("{ \"field\" : \"" + i + "_" + j + "\",\"numeric\" : " + i * j + " }\n");
                // Sometime we update a document
                if (update && rarely()) {
                    String id = between(0, i) + "_" + between(0, j);
                    // We can only update if it has not been removed :)
                    if (!removed.contains(id)) {
                        logger.debug("  -> Updating document [{}] - [{}][{}]", id, i, j);
                        message.append("{ \"update\" : { \"_index\" : \"" + dbName + "\", \"_type\" : \"typex\", \"_id\" : \"" + id + "\" } }\n");
                        message.append("{ \"doc\": { \"foo\" : \"bar\", \"field2\" : \"" + i + "_" + j + "\" }}\n");
                        nbUpdated++;
                    }
                }
                // Sometime we delete a document
                if (delete && rarely()) {
                    String id = between(0, i) + "_" + between(0, j);
                    if (!removed.contains(id)) {
                        logger.debug("  -> Removing document [{}] - [{}][{}]", id, i, j);
                        message.append("{ \"delete\" : { \"_index\" : \"" + dbName + "\", \"_type\" : \"typex\", \"_id\" : \"" + id + "\" } }\n");
                        removed.add(id);
                    }
                }
            }
            channel.basicPublish("", dbName, null, message.toString().getBytes(StandardCharsets.UTF_8));
        }
        logger.info("  -> We removed [{}] docs and updated [{}] docs", removed.size(), nbUpdated);
        if (injectorHook != null) {
            logger.info("  -> Injecting extra data");
            injectorHook.inject();
        }
        logger.info(" --> create river");
        IndexResponse indexResponse = index("_river", dbName, "_meta", river);
        assertTrue(indexResponse.isCreated());
        logger.info("-->  checking that river [{}] was created", dbName);
        assertThat(awaitBusy(new Predicate<Object>() {

            public boolean apply(Object obj) {
                GetResponse response = client().prepareGet(RiverIndexName.Conf.DEFAULT_INDEX_NAME, dbName, "_status").get();
                return response.isExists();
            }
        }, 5, TimeUnit.SECONDS), equalTo(true));
        // Check that docs are still processed by the river
        logger.info(" --> waiting for expected number of docs: [{}]", numDocsPerMessage * numMessages - removed.size());
        assertThat(awaitBusy(new Predicate<Object>() {

            public boolean apply(Object obj) {
                try {
                    refresh();
                    int expected = numDocsPerMessage * numMessages - removed.size();
                    CountResponse response = client().prepareCount(dbName).get();
                    logger.debug("  -> got {} docs, expected {}", response.getCount(), expected);
                    return response.getCount() == expected;
                } catch (IndexMissingException e) {
                    return false;
                }
            }
        }, 20, TimeUnit.SECONDS), equalTo(true));
    } finally {
        if (channel != null && channel.isOpen()) {
            channel.close();
        }
        if (connection != null && connection.isOpen()) {
            connection.close();
        }
        // Deletes the river
        GetResponse response = client().prepareGet(RiverIndexName.Conf.DEFAULT_INDEX_NAME, dbName, "_status").get();
        if (response.isExists()) {
            client().prepareDelete(RiverIndexName.Conf.DEFAULT_INDEX_NAME, dbName, "_meta").get();
            client().prepareDelete(RiverIndexName.Conf.DEFAULT_INDEX_NAME, dbName, "_status").get();
        }
        assertThat(awaitBusy(new Predicate<Object>() {

            public boolean apply(Object obj) {
                GetResponse response = client().prepareGet(RiverIndexName.Conf.DEFAULT_INDEX_NAME, dbName, "_status").get();
                return response.isExists();
            }
        }, 5, TimeUnit.SECONDS), equalTo(false));
    }
}
Also used : Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) CountResponse(org.elasticsearch.action.count.CountResponse) IndexMissingException(org.elasticsearch.indices.IndexMissingException) GetResponse(org.elasticsearch.action.get.GetResponse) ConnectException(java.net.ConnectException) IndexMissingException(org.elasticsearch.indices.IndexMissingException) Predicate(org.elasticsearch.common.base.Predicate) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) IndexResponse(org.elasticsearch.action.index.IndexResponse) ConnectException(java.net.ConnectException) HashSet(java.util.HashSet)

Example 7 with Connection

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

the class RabbitConsumer method reconnect.

/**
     * If the RabbitMQ connection is good this returns without changing
     * anything. If the connection is down it will attempt to reconnect
     */
public void reconnect() throws IOException, TimeoutException {
    if (isChannelOpen()) {
        // The connection is good, so nothing to do
        return;
    }
    log.info("Attempting to open a new rabbitMQ channel");
    Connection conn = consumer.getConnection();
    channel = openChannel(conn);
    // Register the channel to the tag
    start();
}
Also used : Connection(com.rabbitmq.client.Connection)

Example 8 with Connection

use of com.rabbitmq.client.Connection 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 9 with Connection

use of com.rabbitmq.client.Connection 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)

Example 10 with Connection

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

the class RabbitMQEventBus method createConnection.

private synchronized Connection createConnection() throws Exception {
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername(username);
        factory.setPassword(password);
        factory.setHost(amqpHost);
        factory.setPort(port);
        if (virtualHost != null && !virtualHost.isEmpty()) {
            factory.setVirtualHost(virtualHost);
        } else {
            factory.setVirtualHost("/");
        }
        if (useSsl != null && !useSsl.isEmpty() && useSsl.equalsIgnoreCase("true")) {
            factory.useSslProtocol(secureProtocol);
        }
        Connection connection = factory.newConnection();
        connection.addShutdownListener(disconnectHandler);
        connection.addBlockedListener(blockedConnectionHandler);
        s_connection = connection;
        return s_connection;
    } catch (Exception e) {
        throw e;
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) 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

Connection (com.rabbitmq.client.Connection)10 Channel (com.rabbitmq.client.Channel)7 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)4 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)4 IOException (java.io.IOException)4 ConnectException (java.net.ConnectException)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 AlreadyClosedException (com.rabbitmq.client.AlreadyClosedException)3 ConfigurationException (javax.naming.ConfigurationException)3 EventBusException (org.apache.cloudstack.framework.events.EventBusException)3 Ternary (com.cloud.utils.Ternary)1 AMQP (com.rabbitmq.client.AMQP)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 Delivery (com.rabbitmq.client.QueueingConsumer.Delivery)1 ShutdownListener (com.rabbitmq.client.ShutdownListener)1 Firehose (io.druid.data.input.Firehose)1 SimpleDateFormat (java.text.SimpleDateFormat)1