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:co.lp.arch.TestUtils.java

static void spawnThreadWithNewChannel(ConnectionFactory factory, ConsumeWithEx<Channel> f) {
    new Thread(() -> {
        try {//from  w  w  w. ja v  a  2 s  . c  om
            Connection conn = factory.newConnection();
            Channel chan = conn.createChannel();
            f.accept(chan);
            chan.close();
            conn.close();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }).start();
}

From source file:Colas.Colas.java

private String reciver(String enviar) {
    try {/*  w  ww .j  a v a 2 s  . c o  m*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(IP);
        factory.setPort(5672);
        factory.setUsername("valencia");
        factory.setPassword("admin123");

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

        channel.queueDeclare("SC", false, false, false, null);

        channel.basicPublish("", "SC", null, enviar.getBytes());
        System.out.println(" [x] Sent '" + enviar + "'");
        channel.close();
        connection.close();
    } catch (Exception e) {
        System.out.println("Error enviando ");
        e.printStackTrace();
    }
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setUsername("guest");
        factory.setPassword("admin123");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("CF", false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume("CF", true, consumer);
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" [x] Received '" + message + "'");
            return message;

        }
    } catch (Exception e) {
        System.out.println("Error reciviendo ");
        e.printStackTrace();
    }
    return "Error";

}

From source file:Colas.Colas.java

private void sender(String string) {
    try {// ww  w . j a  v  a  2  s  .  co  m
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("valencia");
        factory.setPassword("admin123");

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

        channel.queueDeclare("SC", false, false, false, null);

        channel.basicPublish("", "SC", null, string.getBytes());
        System.out.println(" [x] Sent '" + string + "'");
        channel.close();
        connection.close();
    } catch (Exception e) {
        System.out.println("Error enviando ");
        e.printStackTrace();
    }
}

From source file:Colas.Colas.java

@Override
public void run() {
    try {// w w w.ja  va  2s .  c om
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setUsername("guest");
        factory.setPassword("admin123");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("CF", false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume("CF", true, consumer);
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" [x] Received '" + message + "'");
            String[] resp = message.split(";");
            String p = resp[0];
            if (p.equals("Q1")) {
                pregunta1(resp[1]);
            }
            if (p.equals("Q2")) {
                pregunta2(resp[1]);
            }
            if (p.equals("Q3")) {
                pregunta3(resp[1]);
            }
            if (p.equals("Q4")) {
                pregunta4(resp[1]);
            }

        }
    } catch (Exception e) {
        System.out.println("Error reciviendo ");
        e.printStackTrace();
    }
}

From source file:com.adr.data.rabbitmq.RabbitServer.java

License:Apache License

protected Connection connect() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);/*from   w  w  w .j a va 2 s.co  m*/
    factory.setPort(port);
    return factory.newConnection();
}

From source file:com.adr.data.testlinks.DataQueryLinkMQ.java

License:Apache License

public DataQueryLinkMQ(String host, String dataexchange, String queryexchange) {

    this.dataexchange = dataexchange;
    this.queryexchange = queryexchange;

    try {//  www  .j  av a 2 s  .  c  o  m
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(host);
        connection = factory.newConnection();
    } catch (IOException | TimeoutException ex) {
        LOG.log(Level.SEVERE, null, ex);
        throw new RuntimeException(ex);
    }
}

From source file:com.adr.data.testlinks.DataQueryLinkMQAsync.java

License:Apache License

public DataQueryLinkMQAsync(String host, String dataexchange, String queryexchange) {

    this.dataexchange = dataexchange;
    this.queryexchange = queryexchange;

    try {/*from  w ww  . ja  v  a 2 s .  c om*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(host);
        connection = factory.newConnection();
    } catch (IOException | TimeoutException ex) {
        LOG.log(Level.SEVERE, null, ex);
        throw new RuntimeException(ex);
    }
}

From source file:com.adr.datatest.SourceLink.java

License:Apache License

public static Connection getConnection() {
    if (connection == null) {
        try {/*from   w  ww  .ja v a 2s. com*/
            String host = System.getProperty("rabbitmq.host");
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(host);
            connection = factory.newConnection();
        } catch (IOException | TimeoutException ex) {
            Logger.getLogger(SourceLink.class.getName()).log(Level.SEVERE, null, ex);
            throw new RuntimeException(ex);
        }
    }
    return connection;
}

From source file:com.akash.sparktutorial.AppClass.java

public static void main(String[] args) throws Exception {
    port(3990);/*from  w  w w  .  j a  v  a 2  s.c om*/
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    com.rabbitmq.client.Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    final String QUEUE_NAME = "hello";

    // test route for heroku
    get("/default/:name", (req, res) -> {
        return "Hello " + req.params(":name") + " from heroku";
    });

    //sample route
    get("/hello/:name", (req, res) -> {
        channel.basicPublish("", QUEUE_NAME, null, "hello world".getBytes()); //test
        System.out.println("[x] Sent"); //test
        return "Hello:" + req.params(":name") + "\n New message publishes to RabbitMQ";
    });

    //route to take in the dashboard requets
    post("/request", (req, res) -> {
        String payload = null;
        if (req.contentType().equals("application/json")) {
            //payload in proper format, send request as message to rabbit
            payload = req.body();
            channel.basicPublish("", QUEUE_NAME, null, payload.getBytes());
        } else {
            //payload in incorrect format, send response error
        }
        System.out.println(req.contentType() + "\n" + payload);
        return "hello";
    });
}

From source file:com.anteam.demo.rabbitmq.RabbitMQConsumer.java

License:Apache License

public static void main(String[] args) throws java.io.IOException, java.lang.InterruptedException {

    // //from  w ww  .  j  a v a  2  s . c o m
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("127.0.0.1");
    // 
    Connection connection = factory.newConnection();

    // ??
    Channel channel = connection.createChannel();

    // ?
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println("[*] Waiting for message. To exist press CTRL+C");

    // ?????
    QueueingConsumer consumer = new QueueingConsumer(channel);

    channel.basicConsume(QUEUE_NAME, true, consumer);

    while (true) {

        // ???
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println("[x] Received '" + message + "'");

    }

}