Example usage for com.rabbitmq.client ConnectionFactory setHost

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

Introduction

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

Prototype

public void setHost(String host) 

Source Link

Usage

From source file:it.txt.ens.core.util.AMQPFactory.java

License:Apache License

public static ConnectionFactory createConnectionFactory(ENSAuthzServiceConnectionParameters params) {
    //create and configure the RabbitMQ Connection Factory
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(params.getSubjectID());
    factory.setPassword(params.getAccessToken());
    factory.setPort(params.getBrokerPort());
    factory.setHost(params.getBrokerHost());
    factory.setVirtualHost(params.getVirtualHost());
    return factory;
}

From source file:it.txt.ens.core.util.AMQPFactory.java

License:Apache License

public static Connection createConnection(ENSAuthzServiceConnectionParameters params) throws IOException {
    //create and configure the RabbitMQ Connection Factory
    ConnectionFactory factory = new ConnectionFactory();
    factory.setUsername(params.getSubjectID());
    factory.setPassword(params.getAccessToken());
    factory.setPort(params.getBrokerPort());
    factory.setHost(params.getBrokerHost());
    factory.setVirtualHost(params.getVirtualHost());
    return factory.newConnection();
}

From source file:javarpc_client.RPCClient.java

public RPCClient() throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    connection = (Connection) factory.newConnection();
    channel = connection.createChannel();

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

From source file:javarpc_server.JavaRPC_Server.java

/**
 * @param args the command line arguments
 * @throws java.io.IOException/*from ww  w  .  j av a 2 s.com*/
 * @throws java.lang.InterruptedException
 */
public static void main(String[] args) throws IOException, InterruptedException {
    // TODO code application logic here

    ConnectionFactory factory = new ConnectionFactory();
    System.out.println(factory.getUsername() + " " + factory.getPassword());
    factory.setHost("localhost");

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

    channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);

    channel.basicQos(1);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(RPC_QUEUE_NAME, false, consumer);

    System.out.println(" [x] Awaiting RPC requests");

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();

        BasicProperties props = delivery.getProperties();
        BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId())
                .build();

        String message = new String(delivery.getBody());

        System.out.println(" [.] convert(" + message + ")");
        String response = "" + convert(message);

        channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes());

        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
}

From source file:li.barter.chat.AbstractRabbitMQConnector.java

License:Open Source License

/**
 * Connect to the broker and create the exchange
 * //www.jav a  2 s  .  com
 * @return success
 */
protected boolean connectToRabbitMQ(final String userName, final String password) {
    if ((mChannel != null) && mChannel.isOpen()) {
        return true;
    }
    try {
        final ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(mServer);
        connectionFactory.setUsername(userName);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(mVirtualHost);
        connectionFactory.setPort(mPort);
        connectionFactory.setRequestedHeartbeat(AppConstants.HEART_BEAT_INTERVAL);
        mConnection = connectionFactory.newConnection();
        mChannel = mConnection.createChannel();
        mChannel.exchangeDeclare(mExchange, mExchangeType.key);

        return true;
    } catch (final Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:loanbroker.Aggregator.java

private void send(entity.Message m, String routingKey)
        throws IOException, TimeoutException, InterruptedException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostName);
    factory.setPort(5672);/*from  w w w .ja  v  a2  s.  c o  m*/
    factory.setUsername("student");
    factory.setPassword("cph");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(ExchangeName.GLOBAL, "direct");

    //creating LoanResponse object
    LoanResponse lp = new LoanResponse(parseInt(m.getSsn()), m.getCreditScore(), m.getLoanDuration(), "");

    Gson gson = new GsonBuilder().create();
    String fm = gson.toJson(lp);
    BasicProperties props = new BasicProperties.Builder().build();

    channel.basicPublish(ExchangeName.GLOBAL, routingKey, props, fm.getBytes());

    System.out.println(" [x] Sent '" + ExchangeName.GLOBAL + routingKey + "':'" + fm + "'");

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

}

From source file:loanbroker.Aggregator.java

public void reciveFromNormalizer(Hashtable<String, Message> messageFroumBankList,
        Hashtable<String, Message> messagesFromNormalizer, ArrayList<Message> foundMessages)
        throws IOException, TimeoutException, Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostName);
    factory.setPort(5672);//w  ww  . j  a  va 2 s .  c o m
    factory.setUsername("student");
    factory.setPassword("cph");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    // channel.exchangeDeclare(inputEXCHANGE_NAME, "direct");
    String queueName = channel.queueDeclare().getQueue();

    channel.queueBind(queueName, ExchangeName.GLOBAL, RoutingKeys.NormalizerToAggregator);
    System.out.println(" [*] Waiting for messages on " + ExchangeName.GLOBAL
            + RoutingKeys.NormalizerToAggregator + ". 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 m = new String(body, "UTF-8");

            Gson gson = new GsonBuilder().create();
            LoanResponse lp = gson.fromJson(m, LoanResponse.class);
            Message fm = new Message("" + lp.getSsn(), (int) lp.getInterestRate(), 0, lp.getBank());
            messagesFromNormalizer.put(lp.getCorrelationId(), fm);

            System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + fm.toString() + "'");
            try {
                checkLoanMessages(messageFroumBankList, messagesFromNormalizer, foundMessages);
            } catch (InterruptedException ex) {
                Logger.getLogger(Aggregator.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TimeoutException ex) {
                Logger.getLogger(Aggregator.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(Aggregator.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:loanbroker.Aggregator.java

public void reciveFromRecieptList(Hashtable<String, Message> messagesFromBankList)
        throws IOException, TimeoutException, Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostName);
    factory.setPort(5672);/*from  www . j  a  v a2  s  . co  m*/
    factory.setUsername("student");
    factory.setPassword("cph");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(ExchangeName.GLOBAL, "direct");
    String queueName = channel.queueDeclare().getQueue();

    channel.queueBind(queueName, ExchangeName.GLOBAL, RoutingKeys.RecipientListToAggregator);
    System.out.println(" [*] Waiting for messages on " + ExchangeName.GLOBAL
            + RoutingKeys.RecipientListToAggregator + ".. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String m = new String(body, "UTF-8");
            // System.out.println("reciveFromRecieptList" + m);
            String p = properties.getCorrelationId();
            if (p != null) {
                //send to translator
                Gson g = new Gson();
                Message fm = g.fromJson(m, Message.class);
                if (fm.getBanks() != null) {
                    Message k = new Message(fm.getSsn(), fm.getCreditScore(), fm.getLoanAmount(),
                            fm.getLoanDuration());

                    k.setBanks(fm.getBanks());

                    messagesFromBankList.put(p, k);
                }

                System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + fm.toString() + "'");
                //   System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + m + "'");
            } else {
                System.out.println("No correlationId");
            }

        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:loanbroker.GetBanks.java

private static void init() throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    connection = factory.newConnection();
    inputChannel = connection.createChannel();
    outputChannel = connection.createChannel();

    inputChannel.exchangeDeclare(ExchangeName.OUTPUT_GET_CREDITSCORE, "fanout");
    String queueName = inputChannel.queueDeclare().getQueue();
    inputChannel.queueBind(queueName, ExchangeName.OUTPUT_GET_CREDITSCORE, "");

    outputChannel.exchangeDeclare(ExchangeName.OUTPUT_GET_BANKS, "fanout");

    consumer = new QueueingConsumer(inputChannel);
    inputChannel.basicConsume(queueName, true, consumer);

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

From source file:loanbroker.GetCreditScore.java

void recive() throws IOException, TimeoutException, InterruptedException, Exception {
    //setting the connection to the RabbitMQ server
    ConnectionFactory connfac = new ConnectionFactory();
    connfac.setHost(hostName);
    connfac.setUsername("student");
    connfac.setPassword("cph");

    //make the connection
    Connection conn = connfac.newConnection();
    //make the channel for messaging
    Channel chan = conn.createChannel();

    //Declare a queue
    chan.exchangeDeclare(ExchangeName.OUTPUT_LOAN_REQUEST, "fanout");
    String queueName = chan.queueDeclare().getQueue();
    chan.queueBind(queueName, ExchangeName.OUTPUT_LOAN_REQUEST, "");

    System.out.println(//from   w w w. ja v a  2  s.  c o m
            " [*] Waiting for messages on " + ExchangeName.OUTPUT_LOAN_REQUEST + ". To exit press CTRL+C");

    QueueingConsumer consumer = new QueueingConsumer(chan);
    chan.basicConsume(queueName, true, consumer);

    //start polling messages
    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String m = new String(delivery.getBody());
        System.out.println(" [x] Received '" + m + "'");
        Gson gson = new GsonBuilder().create();
        Message fm = gson.fromJson(m, Message.class);
        int creditScore = creditScore(fm.getSsn());
        fm.setCreditScore(creditScore);
        fm.setSsn(fm.getSsn().replace("-", ""));
        send(fm);

    }

}