Example usage for com.rabbitmq.client ConnectionFactory setUsername

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

Introduction

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

Prototype

public void setUsername(String username) 

Source Link

Document

Set the user name.

Usage

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.
 *///ww  w .ja  v  a2s . c o 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.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  w w . j  a v  a  2s.  c om*/
 */
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);
    }
}

From source file:org.belio.mq.RabbitConsumer.java

@Override
public void open() throws IOException {

    try {//from   w ww  .j av a2 s  . c  om
        com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory();
        factory.setHost(mqproperties.getProperty("host"));
        factory.setVirtualHost(mqproperties.getProperty("vhost"));
        factory.setUsername(mqproperties.getProperty("username"));
        factory.setPassword(mqproperties.getProperty("password"));
        factory.setPort(Integer.parseInt(mqproperties.getProperty("port")));
        factory.setAutomaticRecoveryEnabled(true);
        factory.setNetworkRecoveryInterval(1);
        // Create a new connection to MQ
        connection = factory.newConnection();
        // Create a new channel and declare it's type and exhange as well
        //Create a new rabbit publisher           
        executor = Executors.newScheduledThreadPool(
                Integer.parseInt(threadproperties.getProperty(queueType.name().toLowerCase()).split(",")[0]));

    } catch (IOException ex) {
        Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ShutdownSignalException ex) {
        Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ConsumerCancelledException ex) {
        Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:org.belio.mq.RabbitConsumer.java

private void openPublisher() {
    try {//from   w  w  w .  j av  a2  s  .  c  o  m
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(mqproperties.getProperty("host"));
        factory.setVirtualHost(mqproperties.getProperty("vhost"));
        factory.setUsername(mqproperties.getProperty("username"));
        factory.setPassword(mqproperties.getProperty("password"));
        factory.setPort(Integer.parseInt(mqproperties.getProperty("port")));
        // Create a new connection to MQ
        publishingConnection = factory.newConnection();
        // Create a new channel and declare it's type and exhange as well
        publishingChannel = connection.createChannel();
        publishingChannel.queueDeclare(queueType.name().concat(QUEUE_SUFFIX), true, false, false, null);
        publishingChannel.exchangeDeclare(queueType.name().concat(EXCHANGE_SUFFIX),
                mqproperties.getProperty("type"));
        publishingChannel.queueBind(queueType.name().concat(QUEUE_SUFFIX),
                queueType.name().concat(EXCHANGE_SUFFIX), "");
    } catch (IOException exception) {
        Launcher.LOG.error(exception.getLocalizedMessage(), exception);
    }
}

From source file:org.belio.mq.RabbitPublisher.java

@Override
public void open() throws IOException {
    synchronized (RabbitPublisher.class) {
        try {//  w  ww . j  a  va2  s.  co m
            // Create a connection factory and 
            hosts = mqproperties.getProperty("host").split(",");

            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(mqproperties.getProperty("host"));
            factory.setVirtualHost(mqproperties.getProperty("vhost"));
            factory.setUsername(mqproperties.getProperty("username"));
            factory.setPassword(mqproperties.getProperty("password"));
            factory.setPort(Integer.parseInt(mqproperties.getProperty("port")));
            // Create a new connection to MQ
            connection = factory.newConnection();
            // Create a new channel and declare it's type and exhange as well
            channel = connection.createChannel();
            channel.queueDeclare(queueType.name().concat(QUEUE_SUFFIX), true, false, false, null);
            channel.exchangeDeclare(queueType.name().concat(EXCHANGE_SUFFIX), mqproperties.getProperty("type"));
            channel.queueBind(queueType.name().concat(QUEUE_SUFFIX), queueType.name().concat(EXCHANGE_SUFFIX),
                    "");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

From source file:org.graylog2.inputs.amqp.AMQPConsumer.java

License:Open Source License

private Channel connect() throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(server.getConfiguration().getAmqpUsername());
    factory.setPassword(server.getConfiguration().getAmqpPassword());
    factory.setVirtualHost(server.getConfiguration().getAmqpVirtualhost());
    factory.setHost(server.getConfiguration().getAmqpHost());
    factory.setPort(server.getConfiguration().getAmqpPort());

    connection = factory.newConnection(Executors.newCachedThreadPool(
            new ThreadFactoryBuilder().setNameFormat("amqp-consumer-" + queueConfig.getId() + "-%d").build()));

    return connection.createChannel();
}

From source file:org.graylog2.inputs.amqp.Consumer.java

License:Open Source License

public void connect() throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostname);/*from  w  w w.  j  av  a 2  s  .c om*/
    factory.setPort(port);

    // Authenticate?
    if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
        factory.setUsername(username);
        factory.setPassword(password);
    }

    connection = factory.newConnection();
    channel = connection.createChannel();

    if (prefetchCount > 0) {
        channel.basicQos(prefetchCount);

        LOG.info("AMQP prefetch count overriden to <{}>.", prefetchCount);
    }

    connection.addShutdownListener(new ShutdownListener() {
        @Override
        public void shutdownCompleted(ShutdownSignalException cause) {
            while (true) {
                try {
                    LOG.error("AMQP connection lost! Trying reconnect in 1 second.");

                    Thread.sleep(1000);

                    connect();

                    LOG.info("Connected! Re-starting consumer.");

                    run();

                    LOG.info("Consumer running.");
                    break;
                } catch (IOException e) {
                    LOG.error("Could not re-connect to AMQP broker.", e);
                } catch (InterruptedException ignored) {
                }
            }
        }
    });
}

From source file:org.graylog2.inputs.transports.AmqpConsumer.java

License:Open Source License

public void connect() throws IOException {
    final ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostname);// w  w  w  . j a  v a2  s .c o  m
    factory.setPort(port);
    factory.setVirtualHost(virtualHost);

    if (tls) {
        try {
            LOG.info("Enabling TLS for AMQP input [{}/{}].", sourceInput.getName(), sourceInput.getId());
            factory.useSslProtocol();
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            throw new IOException("Couldn't enable TLS for AMQP input.", e);
        }
    }

    // Authenticate?
    if (!isNullOrEmpty(username) && !isNullOrEmpty(password)) {
        factory.setUsername(username);
        factory.setPassword(password);
    }

    connection = factory.newConnection();
    channel = connection.createChannel();

    if (null == channel) {
        LOG.error("No channel descriptor available!");
    }

    if (null != channel && prefetchCount > 0) {
        channel.basicQos(prefetchCount);

        LOG.info("AMQP prefetch count overriden to <{}>.", prefetchCount);
    }

    connection.addShutdownListener(new ShutdownListener() {
        @Override
        public void shutdownCompleted(ShutdownSignalException cause) {
            if (cause.isInitiatedByApplication()) {
                LOG.info("Not reconnecting connection, we disconnected explicitly.");
                return;
            }
            while (true) {
                try {
                    LOG.error("AMQP connection lost! Trying reconnect in 1 second.");

                    Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);

                    connect();

                    LOG.info("Connected! Re-starting consumer.");

                    run();

                    LOG.info("Consumer running.");
                    break;
                } catch (IOException e) {
                    LOG.error("Could not re-connect to AMQP broker.", e);
                }
            }
        }
    });
}

From source file:org.graylog2.messagehandlers.amqp.AMQPBroker.java

License:Open Source License

public Connection getConnection() throws IOException {
    if (connection == null || !connection.isOpen()) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername(getUsername());
        factory.setPassword(getPassword());
        factory.setVirtualHost(getVirtualHost());
        factory.setHost(getHost());//ww w  . j  a  v a2 s  .  c  om
        factory.setPort(getPort());
        this.connection = factory.newConnection();
    }

    return this.connection;
}

From source file:org.graylog2.radio.Radio.java

License:Open Source License

public Connection getBroker() throws IOException {
    if (brokerConnection == null || !brokerConnection.isOpen()) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(configuration.getAMQPHost());
        factory.setPort(configuration.getAMQPPort());
        factory.setUsername(configuration.getAMQPUser());
        factory.setPassword(configuration.getAMQPPassword());

        brokerConnection = factory.newConnection();
    }//from   w w  w  .j av a2s  .  com

    return brokerConnection;
}