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:net.roboconf.messaging.internal.utils.RabbitMqUtils.java

License:Apache License

/**
 * Configures the connection factory with the right settings.
 * @param factory the connection factory
 * @param messageServerIp the message server IP (can contain a port)
 * @param messageServerUsername the user name for the message server
 * @param messageServerPassword the password for the message server
 * @throws IOException if something went wrong
 */// w w  w.  ja va 2  s.  co m
public static void configureFactory(ConnectionFactory factory, String messageServerIp,
        String messageServerUsername, String messageServerPassword) throws IOException {

    Map.Entry<String, Integer> entry = findUrlAndPort(messageServerIp);
    factory.setHost(entry.getKey());
    if (entry.getValue() > 0)
        factory.setPort(entry.getValue());

    factory.setUsername(messageServerUsername);
    factory.setPassword(messageServerPassword);
}

From source file:net.roboconf.messaging.rabbitmq.internal.utils.RabbitMqTestUtils.java

License:Apache License

/**
 * Creates a channel to interact with a RabbitMQ server for tests.
 * @param messageServerIp the message server's IP address
 * @param username the user name for the messaging server
 * @param password the password for the messaging server
 * @return a non-null channel//from  w  ww.  jav a  2s.  c  om
 * @throws IOException if the creation failed
 */
public static Channel createTestChannel(String messageServerIp, String username, String password)
        throws IOException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(messageServerIp);
    factory.setUsername(username);
    factory.setPassword(password);

    return factory.newConnection().createChannel();
}

From source file:net.roboconf.messaging.rabbitmq.internal.utils.RabbitMqUtils.java

License:Apache License

/**
 * Configures the connection factory with the right settings.
 * @param factory the connection factory
 * @param configuration the messaging configuration
 * @throws IOException if something went wrong
 * @see RabbitMqConstants/*from w ww  . j  a va 2s.c om*/
 */
public static void configureFactory(ConnectionFactory factory, Map<String, String> configuration)
        throws IOException {

    final Logger logger = Logger.getLogger(RabbitMqUtils.class.getName());
    logger.fine("Configuring a connection factory for RabbitMQ.");

    String messageServerIp = configuration.get(RABBITMQ_SERVER_IP);
    if (messageServerIp != null) {
        Map.Entry<String, Integer> entry = Utils.findUrlAndPort(messageServerIp);
        factory.setHost(entry.getKey());
        if (entry.getValue() > 0)
            factory.setPort(entry.getValue());
    }

    factory.setUsername(configuration.get(RABBITMQ_SERVER_USERNAME));
    factory.setPassword(configuration.get(RABBITMQ_SERVER_PASSWORD));

    // Timeout for connection establishment: 5s
    factory.setConnectionTimeout(5000);

    // Configure automatic reconnection
    factory.setAutomaticRecoveryEnabled(true);

    // Recovery interval: 10s
    factory.setNetworkRecoveryInterval(10000);

    // Exchanges and so on should be redeclared if necessary
    factory.setTopologyRecoveryEnabled(true);

    // SSL
    if (Boolean.parseBoolean(configuration.get(RABBITMQ_USE_SSL))) {
        logger.fine("Connection factory for RabbitMQ: SSL is used.");

        InputStream clientIS = null;
        InputStream storeIS = null;
        try {
            clientIS = new FileInputStream(configuration.get(RABBITMQ_SSL_KEY_STORE_PATH));
            storeIS = new FileInputStream(configuration.get(RABBITMQ_SSL_TRUST_STORE_PATH));

            char[] keyStorePassphrase = configuration.get(RABBITMQ_SSL_KEY_STORE_PASSPHRASE).toCharArray();
            KeyStore ks = KeyStore.getInstance(
                    getValue(configuration, RABBITMQ_SSL_KEY_STORE_TYPE, DEFAULT_SSL_KEY_STORE_TYPE));
            ks.load(clientIS, keyStorePassphrase);

            String value = getValue(configuration, RABBITMQ_SSL_KEY_MNGR_FACTORY, DEFAULT_SSL_MNGR_FACTORY);
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(value);
            kmf.init(ks, keyStorePassphrase);

            char[] trustStorePassphrase = configuration.get(RABBITMQ_SSL_TRUST_STORE_PASSPHRASE).toCharArray();
            KeyStore tks = KeyStore.getInstance(
                    getValue(configuration, RABBITMQ_SSL_TRUST_STORE_TYPE, DEFAULT_SSL_TRUST_STORE_TYPE));
            tks.load(storeIS, trustStorePassphrase);

            value = getValue(configuration, RABBITMQ_SSL_TRUST_MNGR_FACTORY, DEFAULT_SSL_MNGR_FACTORY);
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(value);
            tmf.init(tks);

            SSLContext c = SSLContext
                    .getInstance(getValue(configuration, RABBITMQ_SSL_PROTOCOL, DEFAULT_SSL_PROTOCOL));
            c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
            factory.useSslProtocol(c);

        } catch (GeneralSecurityException e) {
            throw new IOException("SSL configuration for the RabbitMQ factory failed.", e);

        } finally {
            Utils.closeQuietly(storeIS);
            Utils.closeQuietly(clientIS);
        }
    }
}

From source file:net.yacy.grid.io.messages.RabbitQueueFactory.java

License:Open Source License

private void init() throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(this.server);
    if (this.port > 0)
        factory.setPort(this.port);
    if (this.username != null && this.username.length() > 0)
        factory.setUsername(this.username);
    if (this.password != null && this.password.length() > 0)
        factory.setPassword(this.password);
    try {//from  www .  j  av  a  2  s . c  om
        this.connection = factory.newConnection();
        this.channel = connection.createChannel();
        this.queues = new ConcurrentHashMap<>();
    } catch (TimeoutException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:nl.uva.sne.drip.drip.component_example.RPCServer.java

License:Apache License

private static void start() {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(HOST);//from  ww w.  j a v  a  2s. co m
    factory.setPassword("guest");
    factory.setUsername("guest");
    factory.setPort(AMQP.PROTOCOL.PORT);
    try (Connection connection = factory.newConnection()) {
        Channel channel = connection.createChannel();
        //We define the queue name 
        channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
        //Set our own customized consummer 
        Consumer c = new Consumer(channel);
        //Start listening for messages 
        channel.basicConsume(RPC_QUEUE_NAME, false, c);

        //Block so we don't close the channel
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException _ignore) {
            }
        }

    } catch (IOException | TimeoutException ex) {
        Logger.getLogger(RPCServer.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:nl.uva.sne.drip.drip.provisioner.RPCServer.java

License:Apache License

private static void start() {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(PropertyValues.HOST);
    factory.setPassword("guest");
    factory.setUsername("guest");
    factory.setPort(AMQP.PROTOCOL.PORT);
    Logger.getLogger(RPCServer.class.getName()).log(Level.INFO, "Connected to: {0}", PropertyValues.HOST);
    try (Connection connection = factory.newConnection()) {
        Channel channel = connection.createChannel();
        //We define the queue name 
        channel.queueDeclare(PropertyValues.RPC_QUEUE_NAME, false, false, false, null);
        DefaultConsumer c;// ww  w .ja va  2  s  .  co  m
        if (PropertyValues.RPC_QUEUE_NAME.endsWith("v0")) {
            c = new nl.uva.sne.drip.drip.provisioner.v0.Consumer(channel);
        } else {
            c = new nl.uva.sne.drip.drip.provisioner.v1.Consumer(channel, PropertyValues.HOST);
        }

        //Start listening for messages 
        channel.basicConsume(PropertyValues.RPC_QUEUE_NAME, false, c);

        //Block so we don't close the channel
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException _ignore) {
            }
        }

    } catch (IOException | TimeoutException ex) {
        Logger.getLogger(RPCServer.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:org.apache.axis2.transport.rabbitmq.ConnectionFactoryManager.java

License:Open Source License

/**
 * Get the connection factory that matches the given name, i.e. referring to
 * the same underlying connection factory. Used by the RabbitMQSender to determine if already
 * available resources should be used for outgoing messages. If no factory instance is
 * found then a new one will be created and added to the connection factory map
 *
 * @param props a Map of connection factory properties and name
 * @return the connection factory or null if no connection factory compatible
 *         with the given properties exists
 *//*from ww  w. ja va  2s.co  m*/
public ConnectionFactory getAMQPConnectionFactory(Hashtable<String, String> props) {
    ConnectionFactory connectionFactory = null;
    String hostName = props.get(RabbitMQConstants.SERVER_HOST_NAME);
    String portValue = props.get(RabbitMQConstants.SERVER_PORT);
    String hostAndPort = hostName + ":" + portValue;
    connectionFactory = connectionFactories.get(hostAndPort);

    if (connectionFactory == null) {
        com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory();
        if (hostName != null && !hostName.equals("")) {
            factory.setHost(hostName);
        } else {
            throw new AxisRabbitMQException("Host name is not correctly defined");
        }
        int port = Integer.parseInt(portValue);
        if (port > 0) {
            factory.setPort(port);
        }
        String userName = props.get(RabbitMQConstants.SERVER_USER_NAME);

        if (userName != null && !userName.equals("")) {
            factory.setUsername(userName);
        }

        String password = props.get(RabbitMQConstants.SERVER_PASSWORD);

        if (password != null && !password.equals("")) {
            factory.setPassword(password);
        }
        String virtualHost = props.get(RabbitMQConstants.SERVER_VIRTUAL_HOST);

        if (virtualHost != null && !virtualHost.equals("")) {
            factory.setVirtualHost(virtualHost);
        }
        factory.setAutomaticRecoveryEnabled(true);
        factory.setTopologyRecoveryEnabled(false);
        connectionFactory = new ConnectionFactory(hostAndPort, factory);
        connectionFactories.put(connectionFactory.getName(), connectionFactory);
    }

    return connectionFactory;
}

From source file:org.apache.camel.component.rabbitmq.RabbitMQConsumerIntTest.java

License:Apache License

@Test
public void sentMessageIsReceived() throws InterruptedException, IOException {

    to.expectedMessageCount(1);//ww  w  .  ja  v  a2 s  . c  o m
    to.expectedHeaderReceived(RabbitMQConstants.REPLY_TO, "myReply");

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setPort(5672);
    factory.setUsername("cameltest");
    factory.setPassword("cameltest");
    factory.setVirtualHost("/");
    Connection conn = factory.newConnection();

    AMQP.BasicProperties.Builder properties = new AMQP.BasicProperties.Builder();
    properties.replyTo("myReply");

    Channel channel = conn.createChannel();
    channel.basicPublish(EXCHANGE, "", properties.build(), "hello world".getBytes());

    to.assertIsSatisfied();
}

From source file:org.apache.camel.component.rabbitmq.RabbitMQProducerIntTest.java

License:Apache License

@Test
public void producedMessageIsReceived() throws InterruptedException, IOException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setPort(5672);//from   ww w.  j  av  a  2s. c  om
    factory.setUsername("cameltest");
    factory.setPassword("cameltest");
    factory.setVirtualHost("/");
    Connection conn = factory.newConnection();

    final List<Envelope> received = new ArrayList<Envelope>();

    Channel channel = conn.createChannel();
    channel.queueDeclare("sammyq", false, false, true, null);
    channel.queueBind("sammyq", EXCHANGE, "route1");
    channel.basicConsume("sammyq", true, new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            received.add(envelope);
        }
    });

    template.sendBodyAndHeader("new message", RabbitMQConstants.EXCHANGE_NAME, "ex1");
    Thread.sleep(500);
    assertEquals(1, received.size());
}

From source file:org.apache.cloudstack.mom.rabbitmq.RabbitMQEventBus.java

License:Apache License

private synchronized Connection createConnection() throws Exception {
    try {//from w w w . jav a2s .c  om
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername(username);
        factory.setPassword(password);
        factory.setVirtualHost("/");
        factory.setHost(amqpHost);
        factory.setPort(port);
        Connection connection = factory.newConnection();
        connection.addShutdownListener(disconnectHandler);
        _connection = connection;
        return _connection;
    } catch (Exception e) {
        throw e;
    }
}