Example usage for com.rabbitmq.client ConnectionFactory newConnection

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

Introduction

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

Prototype

public Connection newConnection() throws IOException, TimeoutException 

Source Link

Document

Create a new broker connection.

Usage

From source file:loanbroker.normalizer.NormalizerOurJsonBank.java

public static void main(String[] args) {
    try {//from w  ww . j a  va  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(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.NormalizerOurSoapXmlBank.java

public static void main(String[] argv) throws IOException, InterruptedException, TimeoutException {
    //Connection//from   w  w w  . j  av  a 2  s  .c o  m
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    //Consumer
    channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
    String queueName = channel.queueDeclare().getQueue();
    //s channel.queueBind(queueName, EXCHANGE_NAME, "OurSoapXmlBank");
    channel.queueBind(queueName, EXCHANGE_NAME, "");
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queueName, true, consumer);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    //Producer
    Channel channelOutput = connection.createChannel();
    channelOutput.exchangeDeclare("TeamFirebug", "direct");

    LoanResponse loanResponse;
    DtoOurSoapXmlBank dtoOurSoapXmlBank;
    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        String routingKey = delivery.getEnvelope().getRoutingKey();

        dtoOurSoapXmlBank = JAXB.unmarshal(new StringReader(message), DtoOurSoapXmlBank.class);
        loanResponse = new LoanResponse(dtoOurSoapXmlBank.getSsn(), dtoOurSoapXmlBank.getInterestRate(),
                "Our Soap Xml bank", delivery.getProperties().getCorrelationId());
        // loanResponse.setBank(routingKey);

        System.out.println("renter: " + loanResponse.getInterestRate());
        System.out.println("ssn: " + loanResponse.getSsn());
        System.out.println("bank : " + loanResponse.getBank());
        JSONObject jsonObj = new JSONObject(loanResponse);
        // channelOutput.basicPublish("", RoutingKeys.Aggregator, null, jsonObj.toString().getBytes());
        channelOutput.basicPublish("TeamFirebug", "normalizerToAggregator", null,
                jsonObj.toString().getBytes());
    }
}

From source file:loanbroker.normalizer.NormalizerTeachersJsonBank.java

public static void main(String[] args) {
    try {/*  w w  w.  j  av  a2 s  .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(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.normalizer.NormalizerTeachersXmlBank.java

public static void main(String[] argv) throws IOException, InterruptedException, TimeoutException {
    //Connection/*from ww w .  ja v  a2 s  . c o  m*/
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

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

    //Producer
    Channel channelOutput = connection.createChannel();
    channelOutput.exchangeDeclare("TeamFirebug", "direct");

    while (true) {
        System.out.println("Reading");
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        String routingKey = delivery.getEnvelope().getRoutingKey();

        DtoTeachersXmlBank dtoOurSoapXmlBank = JAXB.unmarshal(new StringReader(message),
                DtoTeachersXmlBank.class);
        LoanResponse loanResponse = new LoanResponse(dtoOurSoapXmlBank.getSsn(),
                dtoOurSoapXmlBank.getInterestRate(), "Teachers Xml Bank",
                delivery.getProperties().getCorrelationId());
        // loanResponse.setBank(routingKey);
        System.out.println("CorrelationId: " + delivery.getProperties().getCorrelationId());

        System.out.println("renter: " + loanResponse.getInterestRate());
        System.out.println("ssn: " + loanResponse.getSsn());
        System.out.println("bank : " + loanResponse.getBank());
        JSONObject jsonObj = new JSONObject(loanResponse);
        System.out.println("JSON:" + jsonObj);
        // channelOutput.basicPublish("", RoutingKeys.Aggregator, null, jsonObj.toString().getBytes());
        channelOutput.basicPublish("TeamFirebug", "normalizerToAggregator", null,
                jsonObj.toString().getBytes());
        delivery = null;
    }
}

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 v a2 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);
}

From source file:localdomain.localhost.RabbitMQClient.java

License:Apache License

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {/*www.  ja  va2  s.  c om*/
        ConnectionFactory factory = new ConnectionFactory();
        String messages = new String();

        String uri = System.getProperty("CLOUDAMQP_URL");
        factory.setUri(uri);

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

        channel.queueDeclare(QUEUE_NAME, true, false, false, null);

        QueueingConsumer consumer = new QueueingConsumer(channel);

        boolean autoACK = false;
        channel.basicConsume(QUEUE_NAME, autoACK, consumer);

        System.out.println(" [*] Waiting 100ms for a message");
        QueueingConsumer.Delivery delivery = consumer.nextDelivery(100);

        while (delivery != null) {
            String message = new String(delivery.getBody());

            System.out.println(" [x] Received '" + message + "'");

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

            messages = message + " <br/> " + messages;
            delivery = consumer.nextDelivery(100);
        }
        request.setAttribute("messages", messages);
        request.getRequestDispatcher("/index.jsp").forward(request, response);

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

    } catch (Exception e) {
        request.setAttribute("throwable", e);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

}

From source file:localdomain.localhost.RabbitMQServer.java

License:Apache License

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {//from  ww  w  .  ja va 2  s  .c o m
        String message = request.getParameter("message");

        ConnectionFactory factory = new ConnectionFactory();
        String uri = System.getProperty("CLOUDAMQP_URL");
        factory.setUri(uri);

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

        boolean durable = true;
        channel.queueDeclare(QUEUE_NAME, durable, false, false, null);

        channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");

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

        response.sendRedirect(request.getContextPath() + "/index.jsp");
    } catch (Exception e) {
        request.setAttribute("throwable", e);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
}

From source file:lpp.rabbitmq.original.EndPoint.java

License:Open Source License

public EndPoint(String endpointName, RabbitMqConfig config) throws IOException, TimeoutException {

    this.endPointName = endpointName;

    // ?/*from  w ww .j  av  a  2s  .  co  m*/
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(config.getHost());
    factory.setPort(config.getPort());
    factory.setUsername(config.getUserName());
    factory.setPassword(config.getPassword());

    // getting a connection
    connection = factory.newConnection();

    // creating a channel
    channel = connection.createChannel();

    // declaring a queue for this channel. If queue does not exist,
    // it will be created on the server.
    channel.queueDeclare(endpointName, false, false, false, null);
}

From source file:mailmonk.client.MailMonk.java

License:Apache License

/**
 * Connect to a Message Queue and open a channel for 
 * sending Mail Monk jobs./*from ww w  . j a va 2  s  .  c o  m*/
 * */
public void connect() {
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername(getUsername());
        factory.setPassword(getPassword());
        factory.setHost(getHost());

        connection = factory.newConnection();

        channel = connection.createChannel();
        channel.queueDeclare(getQueueName(), false, false, false, null);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:main.TestMain.java

public static void sendMessage(String message) throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    factory.setUsername("student");
    factory.setPassword("cph");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

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

    channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
    System.out.println(" [x] Sent '" + message + "'");

    channel.close();/*from www  .  j a v a 2 s . c  om*/
    connection.close();
}