Example usage for com.rabbitmq.client ConnectionFactory newConnection

List of usage examples for com.rabbitmq.client ConnectionFactory newConnection

Introduction

In this page you can find the example usage for com.rabbitmq.client ConnectionFactory newConnection.

Prototype

public Connection newConnection() throws IOException, TimeoutException 

Source Link

Document

Create a new broker connection.

Usage

From source file:org.apache.nutch.fetcher.RabbitmqProxy.java

License:Apache License

private RabbitmqProxy(Configuration configuration) {
    try {//from   w  w  w.  j  av a  2 s .com
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(configuration.get("rabbitmq.ip"));
        factory.setUsername(configuration.get("rabbitmq.username"));
        factory.setPassword(configuration.get("rabbitmq.password"));
        factory.setVirtualHost(configuration.get("rabbitmq.virtualhost"));
        connection = factory.newConnection();
        channel = connection.createChannel();
        exchange_name = configuration.get("rabbitmq.exchange");
        channel.exchangeDeclare(exchange_name, "fanout");
    } catch (java.io.IOException e) {
        LOG.fatal(e);
    }

}

From source file:org.apache.nutch.indexer.history.HistoryIndexer.java

License:Apache License

private void sendEvent(final String message) {

    ConnectionFactory factory = new ConnectionFactory();
    final String host = conf.get("history.rabbitmq.host", "localhost");
    factory.setHost(host);// ww w  .  j  a va 2 s. c  o  m

    final String user = conf.get("history.rabbitmq.user");
    factory.setUsername(user);

    final String password = conf.get("history.rabbitmq.password");
    factory.setPassword(password);

    Connection connection = null;
    Channel channel = null;
    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
    } catch (IOException | TimeoutException e) {
        final String errMsg = "failed opening connection or channel";
        LOG.error(errMsg, e);
        closeChannelAndConnection(channel, true);
        throw new RuntimeException(errMsg, e);
    }

    final String queueNamesStr = conf.get("history.rabbitmq.queue.names", "nlp,langdetector");
    final String[] queueNames = queueNamesStr.split(",");
    for (final String singleQueueName : queueNames) {
        try {
            channel.queueDeclare(singleQueueName, true, false, false, null);
            channel.basicPublish("", singleQueueName, null, message.getBytes());
            LOG.debug(" [x] Sent '" + message + "'");
        } catch (IOException e) {
            final String errMsg = String.format("failed sending message [%s] to queue [%s]", message,
                    singleQueueName);
            LOG.error(errMsg, e);
        }
    }

    closeChannelAndConnection(channel, false);
}

From source file:org.apache.nutch.indexwriter.rabbit.RabbitIndexWriter.java

License:Apache License

@Override
public void open(JobConf JobConf, String name) throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(serverHost);/* w ww.  ja  v a2  s .co  m*/
    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);
    }
}

From source file:org.apache.nutch.publisher.rabbitmq.RabbitMQPublisherImpl.java

License:Apache License

@Override
public boolean setConfig(Configuration conf) {
    try {/*from ww w  . jav a2  s . c o  m*/
        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;
    }

}

From source file:org.apache.nutch.rabbitmq.RabbitMQClient.java

License:Apache License

/**
 * Builds a new instance of {@link RabbitMQClient}
 *
 * @param serverHost        The server host.
 * @param serverPort        The server port.
 * @param serverVirtualHost The virtual host into the RabbitMQ server.
 * @param serverUsername    The username to access the server.
 * @param serverPassword    The password to access the server.
 * @throws IOException It is thrown if there is some issue during the connection creation.
 *//*from  w w  w.ja  v a 2  s.co  m*/
public RabbitMQClient(String serverHost, int serverPort, String serverVirtualHost, String serverUsername,
        String serverPassword) throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(getValue(serverHost, "localhost"));
    factory.setPort(getValue(serverPort, 5672));

    factory.setVirtualHost(getValue(serverVirtualHost, "/"));

    factory.setUsername(getValue(serverUsername, "guest"));
    factory.setPassword(getValue(serverPassword, "guest"));

    try {
        connection = factory.newConnection();
    } catch (TimeoutException e) {
        throw makeIOException(e);
    }
}

From source file:org.apache.nutch.rabbitmq.RabbitMQClient.java

License:Apache License

/**
 * Builds a new instance of {@link RabbitMQClient}
 *
 * @param uri The connection parameters in the form amqp://userName:password@hostName:portNumber/virtualHost
 * @throws IOException It is thrown if there is some issue during the connection creation.
 */// w  ww . jav  a 2 s . co m
public RabbitMQClient(String uri) throws IOException {
    ConnectionFactory factory = new ConnectionFactory();

    try {
        factory.setUri(uri);

        connection = factory.newConnection();
    } catch (URISyntaxException | NoSuchAlgorithmException | KeyManagementException | TimeoutException e) {
        throw makeIOException(e);
    }
}

From source file:org.apache.synapse.tranport.amqp.AMQPTwoWayProducerClient.java

License:Apache License

public static void main(String[] args) throws IOException, InterruptedException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();

    Channel channel = connection.createChannel();

    AMQPTwoWayProducerClient.produceAndConsume(MESSAGE2, channel, "consumer", "consumerReply");

    channel.close();/*from w  w  w .  ja  va2  s .  c  om*/
    connection.close();

}

From source file:org.archive.crawler.frontier.AMQPUrlReceiver.java

License:Apache License

protected Connection connection() throws IOException {
    lock.lock();//  w  w w.  ja va 2s  .c om
    try {
        if (connection != null && !connection.isOpen()) {
            logger.warning("connection is closed, creating a new one");
            connection = null;
        }

        if (connection == null) {
            ConnectionFactory factory = new ConnectionFactory();
            try {
                factory.setUri(getAmqpUri());
            } catch (Exception e) {
                throw new IOException("problem with AMQP uri " + getAmqpUri(), e);
            }
            connection = factory.newConnection();
        }

        return connection;
    } finally {
        lock.unlock();
    }
}

From source file:org.archive.modules.AMQPProducer.java

License:Apache License

private synchronized void connect() throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    try {/*from  www . ja  va 2 s . c o m*/
        factory.setUri(amqpUri);
        connection = factory.newConnection();
        boolean wasDown = serverLooksDown.getAndSet(false);
        if (wasDown) {
            logger.info(amqpUri + " is back up, connected successfully!");
        }
    } catch (Exception e) {
        connection = null;
        serverLooksDown.getAndSet(true);
        throw new IOException("Attempting to connect to AMQP server failed! " + amqpUri, e);
    }
}

From source file:org.ballerinalang.messaging.rabbitmq.util.ConnectionUtils.java

License:Open Source License

/**
 * Creates a RabbitMQ Connection using the given connection parameters.
 *
 * @param connectionConfig Parameters used to initialize the connection.
 * @return RabbitMQ Connection object.//from ww  w. ja v  a 2 s.  c  o m
 */
public static Connection createConnection(BMap<String, BValue> connectionConfig) {
    try {
        ConnectionFactory connectionFactory = new ConnectionFactory();

        String host = RabbitMQUtils.getStringFromBValue(connectionConfig,
                RabbitMQConstants.RABBITMQ_CONNECTION_HOST);
        connectionFactory.setHost(host);

        int port = RabbitMQUtils.getIntFromBValue(connectionConfig, RabbitMQConstants.RABBITMQ_CONNECTION_PORT,
                LOGGER);
        connectionFactory.setPort(port);

        if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_USER) != null) {
            connectionFactory.setUsername(RabbitMQUtils.getStringFromBValue(connectionConfig,
                    RabbitMQConstants.RABBITMQ_CONNECTION_USER));
        }
        if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_PASS) != null) {
            connectionFactory.setPassword(RabbitMQUtils.getStringFromBValue(connectionConfig,
                    RabbitMQConstants.RABBITMQ_CONNECTION_PASS));
        }
        if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_TIMEOUT) != null) {
            connectionFactory.setConnectionTimeout(RabbitMQUtils.getIntFromBValue(connectionConfig,
                    RabbitMQConstants.RABBITMQ_CONNECTION_TIMEOUT, LOGGER));
        }
        if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_HANDSHAKE_TIMEOUT) != null) {
            connectionFactory.setHandshakeTimeout(RabbitMQUtils.getIntFromBValue(connectionConfig,
                    RabbitMQConstants.RABBITMQ_CONNECTION_HANDSHAKE_TIMEOUT, LOGGER));
        }
        if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_SHUTDOWN_TIMEOUT) != null) {
            connectionFactory.setShutdownTimeout(RabbitMQUtils.getIntFromBValue(connectionConfig,
                    RabbitMQConstants.RABBITMQ_CONNECTION_SHUTDOWN_TIMEOUT, LOGGER));
        }
        if (connectionConfig.get(RabbitMQConstants.RABBITMQ_CONNECTION_HEARTBEAT) != null) {
            connectionFactory.setRequestedHeartbeat(RabbitMQUtils.getIntFromBValue(connectionConfig,
                    RabbitMQConstants.RABBITMQ_CONNECTION_HEARTBEAT, LOGGER));
        }
        return connectionFactory.newConnection();
    } catch (IOException | TimeoutException exception) {
        LOGGER.error(RabbitMQConstants.CREATE_CONNECTION_ERROR, exception);
        throw new RabbitMQConnectorException(RabbitMQConstants.CREATE_CONNECTION_ERROR + exception.getMessage(),
                exception);
    }
}