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:uk.ac.soton.itinnovation.experimedia.arch.ecc.amqpAPI.impl.amqp.AMQPConnectionFactory.java

public void connectToAMQPHost() throws Exception {
    // Safety first
    if (amqpHostIP == null)
        throw new Exception("AMQP Host IP not correct");
    if (amqpConnection != null)
        throw new Exception("Already connected to host");

    ConnectionFactory amqpFactory = new ConnectionFactory();
    amqpFactory.setHost(amqpHostIP.getHostAddress());
    amqpFactory.setPort(amqpPortNumber);

    // Select login credentials (if available)
    String selUserName = "guest";
    String selPassword = "guest";

    if (userName != null && userPass != null) {
        selUserName = userName;//from w w  w . j a  va  2  s  . c  om
        selPassword = userPass;
    }

    // Set login details
    amqpFactory.setUsername(selUserName);
    amqpFactory.setPassword(selPassword);
    factoryLog.info("Logging into RabbitMQ as \'" + userName + "\'");

    // Set heartbeat rate, if available
    if (heartbeatRate != null)
        amqpFactory.setRequestedHeartbeat(heartbeatRate);

    // Execute log-in
    try {
        amqpConnection = amqpFactory.newConnection();
    } catch (Exception ex) {
        throw new Exception("Could not create AMQP host connection", ex);
    }
}

From source file:uk.org.openeyes.oink.hl7v2.test.Hl7ITSupport.java

License:Open Source License

protected static ConnectionFactory initRabbit(String host, int port, String uname, String pwd, String vhost) {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);//w  ww  .j a  v  a  2s. com
    factory.setPort(port);
    factory.setUsername(uname);
    factory.setPassword(pwd);
    factory.setVirtualHost(vhost);
    return factory;
}

From source file:uk.trainwatch.rabbitmq.RabbitConnection.java

License:Apache License

private void createConnection() throws IOException {
    if (connection != null) {
        close();//from   w w  w.  j  ava  2  s  .  c o  m
    }

    LOG.log(Level.FINE, "building connection");
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(username);
    factory.setPassword(password);
    if (virtualHost != null) {
        factory.setVirtualHost(virtualHost);
    }
    factory.setHost(host);
    if (portNumber > 0) {
        factory.setPort(portNumber);
    }

    channels.clear();

    LOG.log(Level.FINE, "creating connection");
    try {
        connection = factory.newConnection();
    } catch (TimeoutException ex) {
        throw new IOException(ex);
    }
}

From source file:user.Client.java

public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("rabbitmq.akhfa.me");
    factory.setUsername(username);
    factory.setPassword(password);//from  ww  w.  ja  v a2 s  .co  m
    factory.setVirtualHost("pat");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    ArrayList<String> daftarNick = getAllQueues();

    String nick = "";

    while (true) {
        Scanner in = new Scanner(System.in);

        System.out.print("Please enter your command: ");
        String command = in.nextLine();

        String[] com = command.split(" ", 2);
        try {
            switch (com[0]) {
            case "/NICK":
                if (!nick.equals("")) {
                    System.out.println("You have registered with nickname: " + nick);
                } else {
                    if (!daftarNick.contains(com[1])) {
                        channel.queueDeclare(com[1], false, false, true, null);
                        nick = com[1];
                        System.out.println("Your nickname is " + nick);

                        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

                        Consumer consumer = new DefaultConsumer(channel) {
                            @Override
                            public void handleDelivery(String consumerTag, Envelope envelope,
                                    AMQP.BasicProperties properties, byte[] body) throws IOException {
                                String message = new String(body, "UTF-8");
                                filterChannel(message);
                            }
                        };
                        channel.basicConsume(nick, true, consumer);
                    } else {
                        System.out.println("Nickname exists.");
                    }
                }
                break;
            case "/JOIN":
                channel.exchangeDeclare(com[1], "fanout", false, false, false, null);
                channel.queueBind(nick, com[1], "");
                System.out.println("You have successfully join " + com[1]);
                break;
            case "/LEAVE":
                channel.queueUnbind(nick, com[1], "");
                System.out.println("Leave " + com[1]);
                break;
            case "/EXIT":
                System.out.println("bye bye...  :D");
                System.exit(0);
            default:
                String message = nick + ' ' + command;
                channel.basicPublish(com[0].substring(1), "", null, message.getBytes("UTF-8"));
                break;
            }
        } catch (Exception e) {
            if (command.compareTo("/NICK") == 0) {
                //random nick
                String random;

                if (!nick.equals("")) {
                    System.out.println("You have registered with nickname: " + nick);
                } else {
                    do {
                        random = randomNick();
                    } while (daftarNick.contains(random));

                    nick = random;

                    channel.queueDeclare(random, false, false, true, null);
                    System.out.println("Your nickname is " + random);

                    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

                    Consumer consumer = new DefaultConsumer(channel) {
                        @Override
                        public void handleDelivery(String consumerTag, Envelope envelope,
                                AMQP.BasicProperties properties, byte[] body) throws IOException {
                            String message = new String(body, "UTF-8");
                            filterChannel(message);
                        }
                    };
                    channel.basicConsume(nick, true, consumer);
                }

            } else if ((command.compareTo("/JOIN") == 0) || (command.compareTo("/LEAVE") == 0)) {
                //error
                System.out.println("Please enter channel name!");
            } else if (command.charAt(0) == '@') {
                System.out.println("Please enter your command for the channel.");
            } else {
                System.out.println("Invalid command.");
            }
        }
    }
}

From source file:wiki.messaging.NewTask.java

License:Open Source License

public static void main(String[] args) throws IOException {
    int n = 500;/*from w w w .ja v  a 2  s .  c  om*/
    String url = null;
    String rabbitMqUrl = null;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("ds")) {
            url = args[i + 1];
        } else if (args[i].equals("rq")) {
            rabbitMqUrl = args[i + 1];
        } else if (args[i].equals("n")) {
            n = Integer.valueOf(args[i + 1]);
        }
    }
    DbConnector ds = new DbConnector(url);

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(rabbitMqUrl);
    factory.setUsername("wiki");
    factory.setPassword("wiki");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);

    RandomDocGetter rdg = new RandomDocGetter(ds);
    for (int i = 0; i < n; i++) {
        String message = getMessage(rdg);
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
    }

    channel.close();
    connection.close();
}

From source file:wiki.messaging.Receive.java

License:Open Source License

public static void main(String[] args) throws IOException, InterruptedException {
    String url = null;/* w w  w .ja v  a 2 s .c  o m*/
    String rabbitMqUrl = null;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("ds")) {
            url = args[i + 1];
        } else if (args[i].equals("rq")) {
            rabbitMqUrl = args[i + 1];
        } else if (args[i].equals("mj")) {
            maxJobs = Integer.valueOf(args[i + 1]);
        }
    }
    if (rabbitMqUrl == null) {
        rabbitMqUrl = "192.168.1.108";
    }
    if (url == null) {
        url = "localhost";
    }
    //        if (resultUrl == null) {
    //            resultUrl = url;
    //        }
    threadPool = new ExecutorCompletionService<>(Executors.newFixedThreadPool(maxJobs));
    System.out.println("DataSource: " + url);
    //        System.out.println("ResultWrite: " + resultUrl);
    DbConnector ds = new DbConnector(url);
    //        DbConnector rs = new DbConnector(resultUrl);

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(rabbitMqUrl);
    factory.setUsername("wiki");
    factory.setPassword("wiki");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    channel.queueDeclare(RESULT_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages.");

    channel.basicQos(maxJobs);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    boolean autoAck = false;
    channel.basicConsume(QUEUE_NAME, autoAck, consumer);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println(" [x] Received '" + message + "'");
        doWork(message, ds);
        saveResult(channel);
        System.out.println(" [x] Done");
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
}

From source file:wiki.messaging.ResultConsumer.java

License:Open Source License

public static void main(String[] args) throws IOException, InterruptedException {
    String rabbitMqUrl = null;// w  w w.  j a v  a  2 s.c  om
    String resultUrl = null;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("rs")) {
            resultUrl = args[i + 1];
        } else if (args[i].equals("rq")) {
            rabbitMqUrl = args[i + 1];
        }
    }

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(rabbitMqUrl);
    factory.setUsername("wiki");
    factory.setPassword("wiki");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(RESULT_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages.");

    QueueingConsumer consumer = new QueueingConsumer(channel);
    boolean autoAck = false;
    channel.basicConsume(RESULT_QUEUE_NAME, autoAck, consumer);

    DbConnector dbc = new DbConnector(resultUrl);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery(1000);
        if (delivery == null)
            break;
        String message = new String(delivery.getBody());
        System.out.println(" [x] Received '" + message + "'");
        saveResult(message, dbc);
        System.out.println(" [x] Done");
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
    channel.close();
    connection.close();
}

From source file:wiki.messaging.Treeceive.java

License:Open Source License

public static void main(String[] args) throws IOException, InterruptedException {
    String url = null;// w  w  w  .ja  va2 s  .  co m
    String rabbitMqUrl = null;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("ds")) {
            url = args[i + 1];
        } else if (args[i].equals("rq")) {
            rabbitMqUrl = args[i + 1];
        } else if (args[i].equals("mj")) {
            maxJobs = Integer.valueOf(args[i + 1]);
        }
    }
    if (rabbitMqUrl == null) {
        rabbitMqUrl = "192.168.1.108";
    }
    if (url == null) {
        url = "localhost";
    }
    //        if (resultUrl == null) {
    //            resultUrl = url;
    //        }
    threadPool = new ExecutorCompletionService<>(Executors.newFixedThreadPool(maxJobs));
    System.out.println("DataSource: " + url);
    //        System.out.println("ResultWrite: " + resultUrl);
    DbConnector ds = new DbConnector(url);
    //        DbConnector rs = new DbConnector(resultUrl);

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(rabbitMqUrl);
    factory.setUsername("wiki");
    factory.setPassword("wiki");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    channel.queueDeclare(RESULT_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages.");

    channel.basicQos(maxJobs);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    boolean autoAck = false;
    channel.basicConsume(QUEUE_NAME, autoAck, consumer);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println(" [x] Received '" + message + "'");
        doWork(message, ds);
        saveResults(channel);
        System.out.println(" [x] Done");
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
}

From source file:ws.ament.hammock.rabbitmq.ConnectionFactoryProducer.java

License:Apache License

@Produces
@ApplicationScoped// w  ww  .jav  a2 s .c o m
public ConnectionFactory createConnectionFactory(RabbitMQConfiguration rabbitMQConfiguration) {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setAutomaticRecoveryEnabled(rabbitMQConfiguration.isAutomaticRecovery());
    connectionFactory.setClientProperties(rabbitMQConfiguration.getClientProperties());
    connectionFactory.setConnectionTimeout(rabbitMQConfiguration.getConnectionTimeout());
    connectionFactory.setExceptionHandler(rabbitMQConfiguration.getExceptionHandler());
    connectionFactory.setHandshakeTimeout(rabbitMQConfiguration.getHandshakeTimeout());
    connectionFactory.setHeartbeatExecutor(rabbitMQConfiguration.getHeartbeatExecutor());
    connectionFactory.setMetricsCollector(rabbitMQConfiguration.getMetricsCollector());
    connectionFactory.setHost(rabbitMQConfiguration.getHost());
    connectionFactory.setNetworkRecoveryInterval(rabbitMQConfiguration.getNetworkRecoveryInterval());
    if (rabbitMQConfiguration.isNio()) {
        connectionFactory.useNio();
        connectionFactory.setNioParams(rabbitMQConfiguration.getNioParams());
    }
    connectionFactory.setPassword(rabbitMQConfiguration.getPassword());
    connectionFactory.setPort(rabbitMQConfiguration.getPort());
    connectionFactory.setRequestedChannelMax(rabbitMQConfiguration.getRequestedChannelMax());
    connectionFactory.setRequestedFrameMax(rabbitMQConfiguration.getRequestedFrameMax());
    connectionFactory.setRequestedHeartbeat(rabbitMQConfiguration.getRequestedHeartbeat());
    connectionFactory.setSaslConfig(rabbitMQConfiguration.getSaslConfig());
    connectionFactory.setSharedExecutor(rabbitMQConfiguration.getSharedExecutor());
    connectionFactory.setShutdownExecutor(rabbitMQConfiguration.getShutdownExecutor());
    connectionFactory.setShutdownTimeout(rabbitMQConfiguration.getShutdownTimeout());
    connectionFactory.setSocketConfigurator(rabbitMQConfiguration.getSocketConf());
    connectionFactory.setSocketFactory(rabbitMQConfiguration.getFactory());
    connectionFactory.setThreadFactory(rabbitMQConfiguration.getThreadFactory());
    connectionFactory.setTopologyRecoveryEnabled(rabbitMQConfiguration.isTopologyRecovery());
    try {
        connectionFactory.setUri(rabbitMQConfiguration.getUri());
    } catch (Exception e) {
        throw new RuntimeException("Unable to populate URI ", e);
    }
    connectionFactory.setUsername(rabbitMQConfiguration.getUsername());
    connectionFactory.setVirtualHost(rabbitMQConfiguration.getVirtualHost());
    if (rabbitMQConfiguration.getSslContext() != null) {
        connectionFactory.useSslProtocol(rabbitMQConfiguration.getSslContext());
    }
    return connectionFactory;
}

From source file:zebrogamq.gamelogic.ChannelsManager.java

License:Open Source License

private ConnectionFactory getConnectionFactory(final GameLogicState state) {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(Util.getRabbitMQProperties().getProperty("gameLogicBrokerHost"));
    factory.setUsername(state.login);
    factory.setPassword(state.password);
    factory.setVirtualHost(state.virtualHost);
    factory.setPort(Integer.valueOf(Util.getRabbitMQProperties().getProperty("gameLogicBrokerPort",
            String.valueOf(factory.getPort()))));
    factory.setConnectionTimeout(CONNECTION_ESTABLISHMENT_TIMEOUT);
    // set the heartbeat value for the amqp connections
    // to avoid the keeping of obsolete consumers
    factory.setRequestedHeartbeat(Integer.valueOf(Util.getRabbitMQProperties()
            .getProperty("amqpConnectionHeartbeat", String.valueOf(factory.getRequestedHeartbeat()))));
    return factory;
}