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.graylog2.radio.transports.amqp.AMQPSender.java

License:Open Source License

public void connect() throws IOException {
    final ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostname);//www.j  a va2s .  co m
    factory.setPort(port);

    factory.setVirtualHost(vHost);

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

    factory.setConnectionTimeout((int) connectTimeout.getMillis());

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

    // It's ok if the queue or exchange already exist.
    channel.queueDeclare(queueName, true, false, false, null);
    channel.exchangeDeclare(exchangeName, queueType, false, false, null);

    channel.queueBind(queueName, exchangeName, routingKey);
}

From source file:org.iplantcollaborative.ClientRegistrar.java

License:Apache License

public void connect() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(this.serverConf.getHostname());
    factory.setPort(this.serverConf.getPort());
    factory.setUsername(this.serverConf.getUserId());
    factory.setPassword(this.serverConf.getUserPwd());
    factory.setVirtualHost(this.serverConf.getVhostPublish());

    factory.setAutomaticRecoveryEnabled(true);

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

    LOG.info("client registrar connected - " + this.serverConf.getHostname() + ":" + this.serverConf.getPort());

    this.channel.basicQos(1);
    this.channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "#");

    this.consumer = new DefaultConsumer(this.channel) {
        @Override/*w  w w .  j av a  2  s  .co  m*/
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");

            LOG.info("registration - " + message);

            BasicProperties replyProps = new BasicProperties.Builder()
                    .correlationId(properties.getCorrelationId()).build();

            ARequest request = RequestFactory.getRequestInstance(message);

            // handle lease request
            if (request instanceof RequestLease) {
                ResponseLease res = lease((RequestLease) request);
                String response_json = serializer.toJson(res);

                if (properties.getReplyTo() != null) {
                    channel.basicPublish("", properties.getReplyTo(), replyProps, response_json.getBytes());
                } else {
                    LOG.error("unable to return response. reply_to field is null");
                }
            } else {
                LOG.error("Unknown request : " + message);
            }

            channel.basicAck(envelope.getDeliveryTag(), false);
        }
    };

    this.workerThread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                channel.basicConsume(QUEUE_NAME, false, consumer);
                LOG.info("Waiting for registrations");
            } catch (IOException ex) {
                LOG.error("Exception occurred while consuming message", ex);
            }
        }
    });
    this.workerThread.start();
}

From source file:org.iplantcollaborative.DataStoreMessageReceiver.java

License:Apache License

public void connect() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(this.serverConf.getHostname());
    factory.setPort(this.serverConf.getPort());
    factory.setUsername(this.serverConf.getUserId());
    factory.setPassword(this.serverConf.getUserPwd());
    factory.setVirtualHost(this.serverConf.getVhostSubscribe());

    factory.setAutomaticRecoveryEnabled(true);

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

    LOG.info("subscriber connected - " + this.serverConf.getHostname() + ":" + this.serverConf.getPort());

    this.channel.basicQos(1);
    this.queueName = this.channel.queueDeclare().getQueue();
    this.channel.queueBind(this.queueName, EXCHANGE_NAME, "#");

    this.consumer = new DefaultConsumer(this.channel) {
        @Override//from   w  ww  .  j  av a2  s.co m
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");

            LOG.debug("subscribe - " + envelope.getRoutingKey() + ":" + message);

            DataStoreMessageProcessor processor = binder.getProcessor();
            if (processor != null) {
                processor.process(envelope.getRoutingKey(), message);
            } else {
                LOG.error("processor not registered");
            }

            channel.basicAck(envelope.getDeliveryTag(), false);
        }
    };

    this.workerThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                channel.basicConsume(queueName, consumer);
                LOG.info("Waiting for messages");
            } catch (IOException ex) {
                LOG.error("Exception occurred while consuming a message", ex);
            }
        }
    });
    this.workerThread.start();
}

From source file:org.iplantcollaborative.MessagePublisher.java

License:Apache License

public void connect() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(this.serverConf.getHostname());
    factory.setPort(this.serverConf.getPort());
    factory.setUsername(this.serverConf.getUserId());
    factory.setPassword(this.serverConf.getUserPwd());
    factory.setVirtualHost(this.serverConf.getVhostPublish());

    factory.setAutomaticRecoveryEnabled(true);

    this.connection = factory.newConnection();
    this.channel = this.connection.createChannel();
    this.channel.basicQos(1);

    LOG.info("publisher connected - " + this.serverConf.getHostname() + ":" + this.serverConf.getPort());
}

From source file:org.kairosdb.plugin.rabbitmq.core.RabbitmqService.java

License:MIT License

@Override
public void start() throws KairosDBException {

    try {//from  w  w  w  .  j a  va2s  .c om
        LOGGER.info("[KRMQ] Starting to RabbitMQ consumer thread.");

        // Socket abstract connection with broker
        ConnectionFactory rabbitmqConnectionFactory = new ConnectionFactory();
        rabbitmqConnectionFactory.setHost(rabbmitmqHost);
        rabbitmqConnectionFactory.setVirtualHost(rabbitmqVirtualHost);
        rabbitmqConnectionFactory.setUsername(rabbitmqUser);
        rabbitmqConnectionFactory.setPassword(rabbitmqPassword);
        rabbitmqConnectionFactory.setPort(rabbitmqPort);
        rabbitmqConnectionFactory.setConnectionTimeout(rabbitmqTimeout);
        rabbitmqConnectionFactory.setRequestedChannelMax(rabbitmqChannelMax);
        rabbitmqConnectionFactory.setRequestedFrameMax(rabbitmqFrameMax);
        rabbitmqConnectionFactory.setRequestedHeartbeat(rabbitmqHearbeat);
        rabbitmqConnectionFactory.setAutomaticRecoveryEnabled(true);

        // Get KairosDatastore implementation
        KairosDatastore kairosDatabase = googleInjector.getInstance(KairosDatastore.class);

        // Create consumer thread
        RabbitmqConsumer consumer = new RabbitmqConsumer(kairosDatabase, rabbitmqConnectionFactory,
                bindingsFile, configurationJSONFieldValue, configurationJSONTimeStamp, configurationJSONTags,
                configurationCSVSeperator, configurationDefaultContentType);

        // Start consumer thread
        consumerThread = new Thread(consumer);
        consumerThread.start();

    } catch (Exception e) {
        LOGGER.error("[KRMQ] An error occurred: ", e);
    }
}

From source file:org.mule.transport.amqp.harness.TestConnectionManager.java

License:Open Source License

public Connection getConnection() throws IOException {
    if (connection != null && connection.isOpen()) {
        return connection;
    }// w w  w . ja  v a 2 s . c o  m

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(System.getProperty("amqpHost"));
    factory.setPort(Integer.valueOf(System.getProperty("amqpPort")));
    factory.setUsername(System.getProperty("amqpUserName"));
    factory.setPassword(System.getProperty("amqpPassword"));
    factory.setVirtualHost(System.getProperty("amqpVirtualHost"));
    connection = factory.newConnection();

    return connection;
}

From source file:org.mule.transport.rmq.RmqConnector.java

License:Open Source License

private ConnectionFactory createConnectionFactory() {
    ConnectionFactory factory = new ConnectionFactory();
    //Construct the factory from the connector URI if available
    //else from the variables.
    if (connectorURI != null) {
        RmqConnectorParser rcp = new RmqConnectorParser(connectorURI);
        host = rcp.getHost();/*w  w  w  .  ja  v a 2  s  .  c  om*/
        port = rcp.getPort();
        vhost = rcp.getVhost();
        username = rcp.getUsername();
        password = rcp.getPassword();
    }
    factory.setHost(host);
    factory.setPort(port);
    if (vhost != null)
        factory.setVirtualHost(vhost);
    if (password != null)
        factory.setPassword(password);
    if (username != null)
        factory.setUsername(username);
    return factory;
}

From source file:org.objectweb.proactive.extensions.amqp.remoteobject.ConnectionAndChannelFactory.java

License:Open Source License

private synchronized CachedConnection getConnection(AMQPConnectionParameters connectionParameters)
        throws IOException {
    String key = connectionParameters.getKey();
    CachedConnection connection = cachedConnections.get(key);
    if (connection == null) {
        logger.debug(String.format("creating a new connection %s", key));

        ConnectionFactory factory = new ConnectionFactory();
        if (socketFactory != null) {
            factory.setSocketFactory(socketFactory);
        }/* w  ww. j av a  2s. c om*/
        factory.setHost(connectionParameters.getHost());
        factory.setPort(connectionParameters.getPort());
        factory.setUsername(connectionParameters.getUsername());
        factory.setPassword(connectionParameters.getPassword());
        factory.setVirtualHost(connectionParameters.getVhost());
        Connection c = factory.newConnection();
        c.addShutdownListener(new AMQPShutDownListener(c.toString()));

        connection = new CachedConnection(this, c);
        cachedConnections.put(key, connection);
    }

    return connection;
}

From source file:org.openbaton.plugin.PluginListener.java

License:Apache License

private void initRabbitMQ() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(brokerIp);/*from  w  ww.jav  a 2 s  . c  o m*/
    factory.setPort(brokerPort);
    factory.setPassword(password);
    factory.setUsername(username);
    factory.setVirtualHost(virtualHost);

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

    channel.queueDeclare(pluginId, this.durable, false, true, null);
    channel.queueBind(pluginId, exchange, pluginId);

    channel.basicQos(1);
}

From source file:org.openbaton.plugin.utils.PluginCaller.java

License:Apache License

public PluginCaller(String pluginId, String brokerIp, String username, String password, int port,
        int managementPort) throws IOException, TimeoutException, NotFoundException {
    this.pluginId = getFullPluginId(pluginId, brokerIp, username, password, managementPort);
    this.managementPort = managementPort;
    this.brokerIp = brokerIp;
    this.username = username;
    this.password = password;
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(brokerIp);/*from w  ww. j  a  v a  2 s.  c o  m*/
    if (username != null)
        factory.setUsername(username);
    else
        factory.setUsername("admin");
    if (password != null)
        factory.setPassword(password);
    else
        factory.setPassword("openbaton");
    if (port > 1024)
        factory.setPort(port);
    else
        factory.setPort(5672);
    connection = factory.newConnection();

    //        replyQueueName = channel.queueDeclare().getQueue();
    //        channel.queueBind(replyQueueName,exchange,replyQueueName);
    //        consumer = new QueueingConsumer(channel);
    //        channel.basicConsume(replyQueueName, true, consumer);
}