Example usage for com.rabbitmq.client ConnectionFactory setPort

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

Introduction

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

Prototype

public void setPort(int port) 

Source Link

Document

Set the target port.

Usage

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:li.barter.chat.AbstractRabbitMQConnector.java

License:Open Source License

/**
 * Connect to the broker and create the exchange
 * /*w  w w  . j  ava2 s .  c  om*/
 * @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);//from  ww  w  .j a  v  a 2 s. c o m
    factory.setPort(5672);
    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);//from  w w  w  .  ja  va2  s.  c o  m
    factory.setPort(5672);
    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);//  ww  w . j av  a  2 s. c om
    factory.setPort(5672);
    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.GetCreditScore.java

public void send(entity.Message creditScoreMessage) throws IOException, TimeoutException, Exception {
    ConnectionFactory connfac = new ConnectionFactory();
    connfac.setHost(hostName);// www. jav  a 2  s .co m
    connfac.setPort(5672);
    connfac.setUsername("student");
    connfac.setPassword("cph");
    Gson gson = new GsonBuilder().create();

    String fm = gson.toJson(creditScoreMessage);

    Connection connection = connfac.newConnection();
    Channel channel = connection.createChannel();
    channel.exchangeDeclare(ExchangeName.OUTPUT_GET_CREDITSCORE, "fanout");
    channel.basicPublish(ExchangeName.OUTPUT_GET_CREDITSCORE, "", null, fm.getBytes());

    System.out.println(" [x] Sent '" + fm.toString() + "'");

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

}

From source file:loanbroker.normalizer.CallTeachersJsonBank.java

public static void main(String[] args) {
    Gson gson = new Gson();
    DtoJsonBank jsonBank = null;// ww w .  j  a v  a 2 s  .c o  m
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("datdb.cphbusiness.dk");
        factory.setPort(5672);
        factory.setUsername("student");
        factory.setPassword("cph");

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

        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

        //reply
        String corrId = java.util.UUID.randomUUID().toString();
        BasicProperties props = new BasicProperties.Builder().correlationId("test1122").replyTo(replyQueueName)
                .build();

        String message = gson.toJson(new DtoJsonBank("1605789787", 598, 10.0, ""));
        channel.basicPublish(EXCHANGE_NAME, "", props, message.getBytes());

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

    } catch (IOException | TimeoutException e) {
        e.printStackTrace();
    }

}

From source file:loanbroker.normalizer.NormalizerOurJsonBank.java

public static void main(String[] args) {
    try {//  ww  w . j  a va 2s . c o m
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("datdb.cphbusiness.dk");
        factory.setPort(5672);
        factory.setUsername("student");
        factory.setPassword("cph");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, EXCHANGE_NAME, "");
        //channel.queueDeclare(RPC_QUEUE_NAME,false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

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

        //producer 
        Channel channelOutput = connection.createChannel();
        channelOutput.exchangeDeclare(ExchangeName.GLOBAL, "direct");
        String queueNameProducer = channelOutput.queueDeclare().getQueue();
        channelOutput.queueBind(queueNameProducer, ExchangeName.GLOBAL, RoutingKeys.NormalizerToAggregator);

        LoanResponse loanResponse;
        while (true) {
            System.out.println("Reading");
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            System.out.println("CorrelationId: " + delivery.getProperties().getCorrelationId());

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

            JSONObject jsonObj = new JSONObject(message);

            loanResponse = new LoanResponse(jsonObj.getInt("ssn"), Math.ceil(jsonObj.getDouble("interestRate")),
                    "Our Json Bank", delivery.getProperties().getCorrelationId());
            System.out.println("renter: " + loanResponse.getInterestRate());
            System.out.println("ssn: " + loanResponse.getSsn());
            System.out.println("bank : " + loanResponse.getBank());
            System.out.println("JSON:" + loanResponse);
            System.out.println("TOstring:" + jsonObj.toString());
            Gson g = new Gson();
            String fm = g.toJson(loanResponse);
            channelOutput.basicPublish(ExchangeName.GLOBAL, RoutingKeys.NormalizerToAggregator, null,
                    fm.getBytes());

        }

    } catch (IOException | TimeoutException | InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:loanbroker.normalizer.NormalizerTeachersJsonBank.java

public static void main(String[] args) {
    try {/*from   ww w. j  ava 2 s .  co m*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("datdb.cphbusiness.dk");
        factory.setPort(5672);
        factory.setUsername("student");
        factory.setPassword("cph");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        //String queueName = channel.queueDeclare().getQueue();
        //channel.queueBind(queueName, EXCHANGE_NAME, "");   
        channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(RPC_QUEUE_NAME, true, consumer);
        //producer 
        Channel channelOutput = connection.createChannel();
        channelOutput.exchangeDeclare(ExchangeName.GLOBAL, "direct");
        String queueName = channelOutput.queueDeclare().getQueue();
        channelOutput.queueBind(queueName, ExchangeName.GLOBAL, "normalizerToAggregator");

        LoanResponse loanResponse;
        while (true) {
            System.out.println("Reading");
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            System.out.println("CorrelationId: " + delivery.getProperties().getCorrelationId());

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

            JSONObject jsonObj = new JSONObject(message);

            loanResponse = new LoanResponse(jsonObj.getInt("ssn"), jsonObj.getDouble("interestRate"),
                    "Teachers Json Bank", delivery.getProperties().getCorrelationId());
            System.out.println("renter: " + loanResponse.getInterestRate());
            System.out.println("ssn: " + loanResponse.getSsn());
            System.out.println("bank : " + loanResponse.getBank());
            //             System.out.println(" [x] Received '" + message + "'");
            System.out.println("JSON:" + loanResponse);
            System.out.println("TOstring:" + jsonObj.toString());
            Gson g = new Gson();
            String fm = g.toJson(loanResponse);
            channelOutput.basicPublish("TeamFirebug", "normalizerToAggregator", null, fm.getBytes());

        }

    } catch (IOException | TimeoutException | InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:loanbroker.Result.java

public void reciveResultFromAggregator() throws IOException, TimeoutException, Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(hostName);//w w  w .j  a va 2  s . co m
    factory.setPort(5672);
    factory.setUsername("student");
    factory.setPassword("cph");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, ExchangeName.GLOBAL, RoutingKeys.Result);
    System.out.println(" [*] Waiting for messages on " + ExchangeName.GLOBAL + RoutingKeys.Result
            + ".. 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();

            Gson gson = new GsonBuilder().create();
            LoanResponse fm = gson.fromJson(m, LoanResponse.class);

            // System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + fm.toString() + "'");
            //   System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + m + "'");
            System.out.println("**** [info]**** You're best interestrate is '" + fm.getInterestRate()
                    + "' on the CPR number. '" + fm.getSsn() + "' at '" + fm.getBank() + "'");
        }
    };
    channel.basicConsume(queueName, true, consumer);
}