Example usage for com.rabbitmq.client ConnectionFactory ConnectionFactory

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

Introduction

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

Prototype

ConnectionFactory

Source Link

Usage

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();/* w ww.jav  a 2s.  co m*/
    connection.close();

}

From source file:org.apache.synapse.transport.amqp.connectionfactory.AMQPTransportConnectionFactory.java

License:Apache License

private Connection createConnection(ExecutorService es, Map<String, String> parameters)
        throws IOException, URISyntaxException, NoSuchAlgorithmException, KeyManagementException {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setUri(parameters.get(AMQPTransportConstant.PARAMETER_CONNECTION_URI));

    if (parameters.get(AMQPTransportConstant.PARAMETER_BROKER_LIST) != null) {
        Address[] addresses = AMQPTransportUtils
                .getAddressArray(parameters.get(AMQPTransportConstant.PARAMETER_BROKER_LIST), ",", ':');
        return connectionFactory.newConnection(es, addresses);
    }//ww  w.  j av a2 s  .c om
    return connectionFactory.newConnection(es);
}

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

License:Apache License

protected Connection connection() throws IOException {
    lock.lock();/*ww w.java 2  s  .  co m*/
    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 {/*  ww w .j  a va 2  s  .c  om*/
        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.atmosphere.plugin.rabbitmq.RabbitMQConnectionFactory.java

License:Apache License

public RabbitMQConnectionFactory(AtmosphereConfig config) {

    String s = config.getInitParameter(PARAM_EXCHANGE_TYPE);
    if (s != null) {
        exchange = s;//from  w  w w . j  av  a  2s  .  c o m
    } else {
        exchange = "topic";
    }

    String host = config.getInitParameter(PARAM_HOST);
    if (host == null) {
        host = "127.0.0.1";
    }

    String vhost = config.getInitParameter(PARAM_VHOST);
    if (vhost == null) {
        vhost = "/";
    }

    String user = config.getInitParameter(PARAM_USER);
    if (user == null) {
        user = "guest";
    }

    String port = config.getInitParameter(PARAM_PORT);
    if (port == null) {
        port = "5672";
    }

    String password = config.getInitParameter(PARAM_PASS);
    if (password == null) {
        password = "guest";
    }

    exchangeName = "atmosphere." + exchange;
    try {
        logger.debug("Create Connection Factory");
        connectionFactory = new ConnectionFactory();
        connectionFactory.setUsername(user);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(vhost);
        connectionFactory.setHost(host);
        connectionFactory.setPort(Integer.valueOf(port));

        logger.debug("Try to acquire a connection ...");
        connection = connectionFactory.newConnection();
        channel = connection.createChannel();

        logger.debug("Topic creation '{}'...", exchangeName);
        channel.exchangeDeclare(exchangeName, exchange);
    } catch (Exception e) {
        String msg = "Unable to configure RabbitMQBroadcaster";
        logger.error(msg, e);
        throw new RuntimeException(msg, e);
    }
    config.shutdownHook(this);
}

From source file:org.atmosphere.samples.pubsub.rabbitmq.RabbitMQConnectionFactory.java

License:Apache License

public RabbitMQConnectionFactory(AtmosphereConfig config) {

    String s = config.getInitParameter(PARAM_EXCHANGE_TYPE);
    if (s != null) {
        exchange = s;/*from w  w w. j  av  a  2  s.  com*/
    } else {
        exchange = "topic";
    }

    String host = config.getInitParameter(PARAM_HOST);
    if (host == null) {
        host = "localhost";
    }

    String vhost = config.getInitParameter(PARAM_VHOST);
    if (vhost == null) {
        vhost = "/";
    }

    String user = config.getInitParameter(PARAM_USER);
    if (user == null) {
        user = "guest";
    }

    String port = config.getInitParameter(PARAM_PORT);
    if (port == null) {
        port = "5672";
    }

    String password = config.getInitParameter(PARAM_PASS);
    if (password == null) {
        password = "guest";
    }

    exchangeName = "atmosphere." + exchange;
    try {
        logger.info("Create Connection Factory");
        connectionFactory = new ConnectionFactory();
        connectionFactory.setUsername(user);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(vhost);
        connectionFactory.setHost(host);
        connectionFactory.setPort(Integer.valueOf(port));

        logger.info("Try to acquire a connection ...");
        connection = connectionFactory.newConnection();
        channel = connection.createChannel();

        logger.info("Topic creation '{}'...", exchangeName);
        channel.exchangeDeclare(exchangeName, exchange);
    } catch (Exception e) {
        String msg = "Unable to configure RabbitMQBroadcaster";
        logger.error(msg, e);
        throw new RuntimeException(msg, e);
    }
    config.shutdownHook(this);
}

From source file:org.atmosphere.samples.websockethub.RabbitMQRouter.java

License:Apache License

public RabbitMQRouter(AtmosphereConfig config) {

    String s = config.getInitParameter(PARAM_EXCHANGE_TYPE);
    if (s != null) {
        exchange = s;//w  ww .java 2  s. co m
    } else {
        exchange = "topic";
    }

    String host = config.getInitParameter(PARAM_HOST);
    if (host == null) {
        host = "127.0.0.1";
    }

    String vhost = config.getInitParameter(PARAM_VHOST);
    if (vhost == null) {
        vhost = "/";
    }

    String user = config.getInitParameter(PARAM_USER);
    if (user == null) {
        user = "guest";
    }

    String port = config.getInitParameter(PARAM_PORT);
    if (port == null) {
        port = "5672";
    }

    String password = config.getInitParameter(PARAM_PASS);
    if (password == null) {
        password = "guest";
    }

    exchangeName = config.getInitParameter(EXCHANGE_NAME);
    if (exchangeName == null) {
        exchangeName = "atmosphere." + exchange;
    }

    s = config.getInitParameter(ROUTING_KEY);
    if (s != null) {
        String[] rk = s.split(",");
        for (String r : rk) {
            amqRoutingKey(r);
        }
    } else {
        amqRoutingKey("atmosphere.all");
    }

    try {
        logger.debug("Create Connection Factory");
        connectionFactory = new ConnectionFactory();
        connectionFactory.setUsername(user);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(vhost);
        connectionFactory.setHost(host);
        connectionFactory.setPort(Integer.valueOf(port));

        logger.debug("Try to acquire a connection ...");
        connection = connectionFactory
                .newConnection(ExecutorsFactory.getMessageDispatcher(config, "connectionFactory"));
        channel = connection.createChannel();

        logger.debug("Topic creation '{}'...", exchangeName);
        channel.exchangeDeclare(exchangeName, exchange);
    } catch (Exception e) {
        String msg = "Unable to configure RabbitMQBroadcaster";
        logger.error(msg, e);
        throw new RuntimeException(msg, e);
    }
    config.shutdownHook(this);

    routeIn();
}

From source file:org.axonframework.amqp.eventhandling.RabbitMQBenchmark.java

License:Apache License

public static void main(String[] args) throws IOException, InterruptedException, TimeoutException {
    final Connection connection = new ConnectionFactory().newConnection();
    final Channel channel = connection.createChannel();
    String queueName = channel.queueDeclare().getQueue();
    execute("Transactional and Channel pooling", createChannelPoolSharingThreads(connection, queueName));
    queueName = refreshQueue(channel, queueName);
    execute("Transactional, new Channel per tx", createChannelCreatingThreads(connection, queueName, true));
    queueName = refreshQueue(channel, queueName);
    execute("Non-transactional, new Channel per tx",
            createChannelCreatingThreads(connection, queueName, false));
    queueName = refreshQueue(channel, queueName);
    execute("Non-transactional, single Channel", createChannelSharingThreads(connection, queueName));
    channel.confirmSelect();/*from w w  w .ja  v a2s.  c  om*/
    connection.close();
}

From source file:org.axonframework.eventhandling.amqp.RabbitMQBenchmark.java

License:Apache License

public static void main(String[] args) throws IOException, InterruptedException {
    final Connection connection = new ConnectionFactory().newConnection();
    final Channel channel = connection.createChannel();
    String queueName = channel.queueDeclare().getQueue();
    execute("Transactional and Channel pooling", createChannelPoolSharingThreads(connection, queueName));
    queueName = refreshQueue(channel, queueName);
    execute("Transactional, new Channel per tx", createChannelCreatingThreads(connection, queueName, true));
    queueName = refreshQueue(channel, queueName);
    execute("Non-transactional, new Channel per tx",
            createChannelCreatingThreads(connection, queueName, false));
    queueName = refreshQueue(channel, queueName);
    execute("Non-transactional, single Channel", createChannelSharingThreads(connection, queueName));
    channel.confirmSelect();//from www.  j  a v  a2 s  . c  o m
    connection.close();
}

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   w ww  .  j ava2s .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);
    }
}