Example usage for com.rabbitmq.client ConnectionFactory setUri

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

Introduction

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

Prototype

public void setUri(String uriString)
        throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException 

Source Link

Document

Convenience method for setting the fields in an AMQP URI: host, port, username, password and virtual host.

Usage

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.
 *///from  w  ww . j  av a 2  s.  c o  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.pulsar.connect.rabbitmq.RabbitMQSource.java

License:Apache License

@Override
public void open(Map<String, String> config) throws Exception {
    rabbitMQConfig = RabbitMQConfig.load(config);
    if (rabbitMQConfig.getAmqUri() == null || rabbitMQConfig.getQueueName() == null) {
        throw new IllegalArgumentException("Required property not set.");
    }/*from w  w  w.j a  va2  s .  com*/
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setUri(rabbitMQConfig.getAmqUri());
    rabbitMQConnection = connectionFactory.newConnection(rabbitMQConfig.getConnectionName());
    logger.info("A new connection to {}:{} has been opened successfully.",
            rabbitMQConnection.getAddress().getCanonicalHostName(), rabbitMQConnection.getPort());
    rabbitMQChannel = rabbitMQConnection.createChannel();
    rabbitMQChannel.queueDeclare(rabbitMQConfig.getQueueName(), false, false, false, null);
    com.rabbitmq.client.Consumer consumer = new RabbitMQConsumer(this.consumer, rabbitMQChannel);
    rabbitMQChannel.basicConsume(rabbitMQConfig.getQueueName(), consumer);
    logger.info("A consumer for queue {} has been successfully started.", rabbitMQConfig.getQueueName());
}

From source file:org.apache.pulsar.io.rabbitmq.RabbitMQSource.java

License:Apache License

@Override
public void open(Map<String, Object> config, SourceContext sourceContext) throws Exception {
    rabbitMQConfig = RabbitMQConfig.load(config);
    if (rabbitMQConfig.getAmqUri() == null || rabbitMQConfig.getQueueName() == null) {
        throw new IllegalArgumentException("Required property not set.");
    }/*from w w w . j a  v  a 2  s.  c o m*/
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setUri(rabbitMQConfig.getAmqUri());
    rabbitMQConnection = connectionFactory.newConnection(rabbitMQConfig.getConnectionName());
    logger.info("A new connection to {}:{} has been opened successfully.",
            rabbitMQConnection.getAddress().getCanonicalHostName(), rabbitMQConnection.getPort());
    rabbitMQChannel = rabbitMQConnection.createChannel();
    rabbitMQChannel.queueDeclare(rabbitMQConfig.getQueueName(), false, false, false, null);
    com.rabbitmq.client.Consumer consumer = new RabbitMQConsumer(this, rabbitMQChannel);
    rabbitMQChannel.basicConsume(rabbitMQConfig.getQueueName(), consumer);
    logger.info("A consumer for queue {} has been successfully started.", rabbitMQConfig.getQueueName());
}

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);
    }// w w  w.  ja  va2s. c o  m
    return connectionFactory.newConnection(es);
}

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

License:Apache License

protected Connection connection() throws IOException {
    lock.lock();/* w w w  . j av a 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 {/* w w  w  .  j ava  2  s . com*/
        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.eclipse.ditto.services.connectivity.messaging.rabbitmq.ConnectionBasedRabbitConnectionFactoryFactory.java

License:Open Source License

@Override
public ConnectionFactory createConnectionFactory(final Connection connection,
        final ExceptionHandler exceptionHandler) {
    checkNotNull(connection, "Connection");
    checkNotNull(exceptionHandler, "Exception Handler");

    try {/*w ww.  java2 s  . c  om*/
        final ConnectionFactory connectionFactory = new CustomConnectionFactory();
        if (SECURE_AMQP_SCHEME.equalsIgnoreCase(connection.getProtocol())) {
            if (connection.isValidateCertificates()) {
                final SSLContextCreator sslContextCreator = SSLContextCreator.fromConnection(connection, null);
                connectionFactory.useSslProtocol(sslContextCreator.withoutClientCertificate());
            } else {
                // attention: this accepts all certificates whether they are valid or not
                connectionFactory.useSslProtocol();
            }
        }

        connectionFactory.setUri(connection.getUri());

        // this makes no difference as the used newmotion client always sets the AutomaticRecoveryEnabled to false:
        connectionFactory.setAutomaticRecoveryEnabled(connection.isFailoverEnabled());

        connectionFactory.setExceptionHandler(exceptionHandler);

        configureConnectionFactory(connectionFactory, connection.getSpecificConfig());

        return connectionFactory;
    } catch (final NoSuchAlgorithmException | KeyManagementException | URISyntaxException e) {
        LOGGER.warn(e.getMessage());
        throw new IllegalStateException("Failed to create RabbitMQ connection factory.", e);
    }
}

From source file:org.eclipse.flux.client.config.RabbitMQFluxConfig.java

License:Open Source License

/**
 * Configure the AMQP ConnectionFactory with information from this RabbitMQFluxConfig
 *///from www  . j a va 2s . c om
public void applyTo(ConnectionFactory f) throws Exception {
    if (uri != null) {
        f.setUri(uri);
    } else {
        f.setHost("localhost");
    }
}

From source file:org.eclipse.hono.client.AmqpConnectionManagerImpl.java

License:Open Source License

private Connection createConnection(final ConnectionConfig connectionConfig) throws NoSuchAlgorithmException,
        KeyManagementException, URISyntaxException, IOException, TimeoutException {
    final ConnectionFactory factory = new ConnectionFactory();
    final String amqpUri = connectionConfig.getConnectionUri();
    AmqpConnectionManagerImpl.LOGGER.info("Connecting to " + amqpUri);
    factory.setUri(amqpUri);
    factory.setAutomaticRecoveryEnabled(true);
    factory.setExceptionHandler(new DefaultExceptionHandler());
    return factory.newConnection();
}

From source file:org.eclipse.hono.dispatcher.amqp.AmqpConnection.java

License:Open Source License

private static Connection createConnection(final String amqpUri) throws NoSuchAlgorithmException,
        KeyManagementException, URISyntaxException, IOException, TimeoutException {
    final ConnectionFactory factory = new ConnectionFactory();

    AmqpConnection.LOGGER.info("Connecting to " + amqpUri);

    factory.setUri(amqpUri);
    factory.setRequestedHeartbeat(60);/*from w  w  w  .j a v a  2s .c  o  m*/
    factory.setAutomaticRecoveryEnabled(true);
    factory.setTopologyRecoveryEnabled(true);
    factory.setExceptionHandler(new ExceptionHandler() {
        @Override
        public void handleUnexpectedConnectionDriverException(final Connection conn,
                final Throwable exception) {
            AmqpConnection.LOGGER.warn("UnexpectedConnectionDriverException", exception);
        }

        @Override
        public void handleReturnListenerException(final Channel channel, final Throwable exception) {
            AmqpConnection.LOGGER.warn("ReturnListenerException", exception);
        }

        @Override
        public void handleFlowListenerException(final Channel channel, final Throwable exception) {
            AmqpConnection.LOGGER.warn("FlowListenerException", exception);
        }

        @Override
        public void handleConfirmListenerException(final Channel channel, final Throwable exception) {
            AmqpConnection.LOGGER.warn("ConfirmListenerException", exception);
        }

        @Override
        public void handleBlockedListenerException(final Connection connection, final Throwable exception) {
            AmqpConnection.LOGGER.warn("BlockedListenerException", exception);
        }

        @Override
        public void handleConsumerException(final Channel channel, final Throwable exception,
                final Consumer consumer, final String consumerTag, final String methodName) {
            AmqpConnection.LOGGER.warn("ConsumerException", exception);
        }

        @Override
        public void handleConnectionRecoveryException(final Connection conn, final Throwable exception) {
            AmqpConnection.LOGGER.warn("ConnectionRecoveryException", exception);
        }

        @Override
        public void handleChannelRecoveryException(final Channel ch, final Throwable exception) {
            AmqpConnection.LOGGER.warn("ChannelRecoveryException", exception);
        }

        @Override
        public void handleTopologyRecoveryException(final Connection conn, final Channel ch,
                final TopologyRecoveryException exception) {
            AmqpConnection.LOGGER.warn("TopologyRecoveryException", exception);
        }
    });

    return factory.newConnection();
}