Search in sources :

Example 41 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project nutch by apache.

the class RabbitIndexWriter method open.

@Override
public void open(Configuration conf, String name) throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(serverHost);
    factory.setPort(serverPort);
    if (serverVirtualHost != null) {
        factory.setVirtualHost(serverVirtualHost);
    }
    factory.setUsername(serverUsername);
    factory.setPassword(serverPassword);
    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.exchangeDeclare(exchangeServer, exchangeType, true);
        channel.queueDeclare(queueName, queueDurable, false, false, null);
        channel.queueBind(queueName, exchangeServer, queueRoutingKey);
    } catch (TimeoutException | IOException ex) {
        throw makeIOException(ex);
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException)

Example 42 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project nutch by apache.

the class RabbitMQPublisherImpl method setConfig.

@Override
public boolean setConfig(Configuration conf) {
    try {
        EXCHANGE_SERVER = conf.get("rabbitmq.exchange.server", "fetcher_log");
        EXCHANGE_TYPE = conf.get("rabbitmq.exchange.type", "fanout");
        HOST = conf.get("rabbitmq.host", "localhost");
        PORT = conf.getInt("rabbitmq.port", 5672);
        VIRTUAL_HOST = conf.get("rabbitmq.virtualhost", null);
        USERNAME = conf.get("rabbitmq.username", null);
        PASSWORD = conf.get("rabbitmq.password", null);
        QUEUE_NAME = conf.get("rabbitmq.queue.name", "fanout.queue");
        QUEUE_DURABLE = conf.getBoolean("rabbitmq.queue.durable", true);
        QUEUE_ROUTING_KEY = conf.get("rabbitmq.queue.routingkey", "fanout.key");
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(HOST);
        factory.setPort(PORT);
        if (VIRTUAL_HOST != null) {
            factory.setVirtualHost(VIRTUAL_HOST);
        }
        if (USERNAME != null) {
            factory.setUsername(USERNAME);
            factory.setPassword(PASSWORD);
        }
        Connection connection = factory.newConnection();
        channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_SERVER, EXCHANGE_TYPE);
        channel.queueDeclare(QUEUE_NAME, QUEUE_DURABLE, false, false, null);
        channel.queueBind(QUEUE_NAME, EXCHANGE_SERVER, QUEUE_ROUTING_KEY);
        LOG.info("Configured RabbitMQ publisher");
        return true;
    } catch (Exception e) {
        LOG.error("Could not initialize RabbitMQ publisher - {}", StringUtils.stringifyException(e));
        return false;
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 43 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project wso2-axis2-transports by wso2.

the class RabbitMQConnectionFactory method initConnectionFactory.

/**
 * Initialize connection factory
 */
private void initConnectionFactory() {
    connectionFactory = new ConnectionFactory();
    String hostName = parameters.get(RabbitMQConstants.SERVER_HOST_NAME);
    String portValue = parameters.get(RabbitMQConstants.SERVER_PORT);
    String serverRetryIntervalS = parameters.get(RabbitMQConstants.SERVER_RETRY_INTERVAL);
    String retryIntervalS = parameters.get(RabbitMQConstants.RETRY_INTERVAL);
    String retryCountS = parameters.get(RabbitMQConstants.RETRY_COUNT);
    String heartbeat = parameters.get(RabbitMQConstants.HEARTBEAT);
    String connectionTimeout = parameters.get(RabbitMQConstants.CONNECTION_TIMEOUT);
    String sslEnabledS = parameters.get(RabbitMQConstants.SSL_ENABLED);
    String userName = parameters.get(RabbitMQConstants.SERVER_USER_NAME);
    String password = parameters.get(RabbitMQConstants.SERVER_PASSWORD);
    String virtualHost = parameters.get(RabbitMQConstants.SERVER_VIRTUAL_HOST);
    String connectionPoolSizeS = parameters.get(RabbitMQConstants.CONNECTION_POOL_SIZE);
    if (!StringUtils.isEmpty(heartbeat)) {
        try {
            int heartbeatValue = Integer.parseInt(heartbeat);
            connectionFactory.setRequestedHeartbeat(heartbeatValue);
        } catch (NumberFormatException e) {
            // proceeding with rabbitmq default value
            log.warn("Number format error in reading heartbeat value. Proceeding with default");
        }
    }
    if (!StringUtils.isEmpty(connectionTimeout)) {
        try {
            int connectionTimeoutValue = Integer.parseInt(connectionTimeout);
            connectionFactory.setConnectionTimeout(connectionTimeoutValue);
        } catch (NumberFormatException e) {
            // proceeding with rabbitmq default value
            log.warn("Number format error in reading connection timeout value. Proceeding with default");
        }
    }
    if (!StringUtils.isEmpty(connectionPoolSizeS)) {
        try {
            connectionPoolSize = Integer.parseInt(connectionPoolSizeS);
        } catch (NumberFormatException e) {
            // proceeding with rabbitmq default value
            log.warn("Number format error in reading connection timeout value. Proceeding with default");
        }
    }
    if (!StringUtils.isEmpty(sslEnabledS)) {
        try {
            boolean sslEnabled = Boolean.parseBoolean(sslEnabledS);
            if (sslEnabled) {
                String keyStoreLocation = parameters.get(RabbitMQConstants.SSL_KEYSTORE_LOCATION);
                String keyStoreType = parameters.get(RabbitMQConstants.SSL_KEYSTORE_TYPE);
                String keyStorePassword = parameters.get(RabbitMQConstants.SSL_KEYSTORE_PASSWORD);
                String trustStoreLocation = parameters.get(RabbitMQConstants.SSL_TRUSTSTORE_LOCATION);
                String trustStoreType = parameters.get(RabbitMQConstants.SSL_TRUSTSTORE_TYPE);
                String trustStorePassword = parameters.get(RabbitMQConstants.SSL_TRUSTSTORE_PASSWORD);
                String sslVersion = parameters.get(RabbitMQConstants.SSL_VERSION);
                if (StringUtils.isEmpty(keyStoreLocation) || StringUtils.isEmpty(keyStoreType) || StringUtils.isEmpty(keyStorePassword) || StringUtils.isEmpty(trustStoreLocation) || StringUtils.isEmpty(trustStoreType) || StringUtils.isEmpty(trustStorePassword)) {
                    log.info("Trustore and keystore information is not provided");
                    if (StringUtils.isNotEmpty(sslVersion)) {
                        connectionFactory.useSslProtocol(sslVersion);
                    } else {
                        log.info("Proceeding with default SSL configuration");
                        connectionFactory.useSslProtocol();
                    }
                } else {
                    char[] keyPassphrase = keyStorePassword.toCharArray();
                    KeyStore ks = KeyStore.getInstance(keyStoreType);
                    ks.load(new FileInputStream(keyStoreLocation), keyPassphrase);
                    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
                    kmf.init(ks, keyPassphrase);
                    char[] trustPassphrase = trustStorePassword.toCharArray();
                    KeyStore tks = KeyStore.getInstance(trustStoreType);
                    tks.load(new FileInputStream(trustStoreLocation), trustPassphrase);
                    TrustManagerFactory tmf = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
                    tmf.init(tks);
                    SSLContext c = SSLContext.getInstance(sslVersion);
                    c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
                    connectionFactory.useSslProtocol(c);
                }
            }
        } catch (Exception e) {
            log.warn("Format error in SSL enabled value. Proceeding without enabling SSL", e);
        }
    }
    if (!StringUtils.isEmpty(retryCountS)) {
        try {
            retryCount = Integer.parseInt(retryCountS);
        } catch (NumberFormatException e) {
            log.warn("Number format error in reading retry count value. Proceeding with default value (3)", e);
        }
    }
    // Resolving hostname(s) and port(s)
    if (!StringUtils.isEmpty(hostName) && !StringUtils.isEmpty(portValue)) {
        String[] hostNames = hostName.split(",");
        String[] portValues = portValue.split(",");
        if (hostNames.length == portValues.length) {
            addresses = new Address[hostNames.length];
            for (int i = 0; i < hostNames.length; i++) {
                if (!hostNames[i].isEmpty() && !portValues[i].isEmpty()) {
                    try {
                        addresses[i] = new Address(hostNames[i].trim(), Integer.parseInt(portValues[i].trim()));
                    } catch (NumberFormatException e) {
                        handleException("Number format error in port number", e);
                    }
                }
            }
        }
    } else {
        handleException("Host name(s) and port(s) are not correctly defined");
    }
    if (!StringUtils.isEmpty(userName)) {
        connectionFactory.setUsername(userName);
    }
    if (!StringUtils.isEmpty(password)) {
        connectionFactory.setPassword(password);
    }
    if (!StringUtils.isEmpty(virtualHost)) {
        connectionFactory.setVirtualHost(virtualHost);
    }
    if (!StringUtils.isEmpty(retryIntervalS)) {
        try {
            retryInterval = Integer.parseInt(retryIntervalS);
        } catch (NumberFormatException e) {
            log.warn("Number format error in reading retry interval value. Proceeding with default value (30000ms)", e);
        }
    }
    if (!StringUtils.isEmpty(serverRetryIntervalS)) {
        try {
            int serverRetryInterval = Integer.parseInt(serverRetryIntervalS);
            connectionFactory.setNetworkRecoveryInterval(serverRetryInterval);
        } catch (NumberFormatException e) {
            log.warn("Number format error in reading server retry interval value. Proceeding with default value", e);
        }
    }
    connectionFactory.setAutomaticRecoveryEnabled(true);
    connectionFactory.setTopologyRecoveryEnabled(false);
}
Also used : Address(com.rabbitmq.client.Address) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) AxisRabbitMQException(org.apache.axis2.transport.rabbitmq.utils.AxisRabbitMQException) SecureVaultException(org.wso2.securevault.SecureVaultException) IOException(java.io.IOException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) TrustManagerFactory(javax.net.ssl.TrustManagerFactory)

Example 44 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project apex-malhar by apache.

the class AbstractRabbitMQInputOperator method activate.

@Override
public void activate(OperatorContext ctx) {
    try {
        connFactory = new ConnectionFactory();
        connFactory.setHost(host);
        if (port != 0) {
            connFactory.setPort(port);
        }
        connection = connFactory.newConnection();
        channel = connection.createChannel();
        channel.exchangeDeclare(exchange, exchangeType);
        boolean resetQueueName = false;
        if (queueName == null) {
            // unique queuename is generated
            // used in case of fanout exchange
            queueName = channel.queueDeclare().getQueue();
            resetQueueName = true;
        } else {
            // user supplied name
            // used in case of direct exchange
            channel.queueDeclare(queueName, true, false, false, null);
        }
        channel.queueBind(queueName, exchange, routingKey);
        // consumer = new QueueingConsumer(channel);
        // channel.basicConsume(queueName, true, consumer);
        tracingConsumer = new TracingConsumer(channel);
        cTag = channel.basicConsume(queueName, false, tracingConsumer);
        if (resetQueueName) {
            queueName = null;
        }
    } catch (IOException ex) {
        throw new RuntimeException("Connection Failure", ex);
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) IOException(java.io.IOException)

Example 45 with ConnectionFactory

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

Aggregations

ConnectionFactory (com.rabbitmq.client.ConnectionFactory)64 Connection (com.rabbitmq.client.Connection)28 Test (org.junit.Test)26 IOException (java.io.IOException)17 Channel (com.rabbitmq.client.Channel)13 AMQConnection (com.rabbitmq.client.impl.AMQConnection)6 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)4 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)3 QueueingConsumer (com.rabbitmq.client.QueueingConsumer)3 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)3 KeyManagementException (java.security.KeyManagementException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 TimeoutException (java.util.concurrent.TimeoutException)3 AMQP (com.rabbitmq.client.AMQP)2 Address (com.rabbitmq.client.Address)2 Consumer (com.rabbitmq.client.Consumer)2 DnsRecordIpAddressResolver (com.rabbitmq.client.DnsRecordIpAddressResolver)2 MetricsCollector (com.rabbitmq.client.MetricsCollector)2 ShutdownListener (com.rabbitmq.client.ShutdownListener)2 SslContextFactory (com.rabbitmq.client.SslContextFactory)2