Example usage for com.rabbitmq.client ConnectionFactory ConnectionFactory

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

Introduction

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

Prototype

ConnectionFactory

Source Link

Usage

From source file:Colas.Colas.java

private String reciver(String enviar) {
    try {//from  w w  w. j av a2 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 {/*from  ww w  .  java 2s.  com*/
        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.  j a v a  2  s .co m
        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);/*  w  w  w .  j ava2 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 {//from  w ww  . j  a  v 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 w  w  . j av a2 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.datatest.SourceLink.java

License:Apache License

public static Connection getConnection() {
    if (connection == null) {
        try {//  w  w w . j ava  2s  .co m
            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 va2s .  co m
    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.almende.eve.transport.amqp.AmqpTransport.java

License:Apache License

/**
 * Instantiates a new AMQP transport.//from   w  ww .j a  v a 2  s . c o  m
 *
 * @param config
 *            the config
 * @param newHandle
 *            the new handle
 * @param amqpService
 *            the amqp service
 */
public AmqpTransport(AmqpTransportConfig config, Handler<Receiver> newHandle, AmqpService amqpService) {
    super(URIUtil.create("amqp:" + config.getId()), newHandle, amqpService, config);
    myId = config.getId();
    factory = new ConnectionFactory();
    try {
        factory.setUri(config.getHostUri());
    } catch (KeyManagementException | NoSuchAlgorithmException | URISyntaxException e) {
        LOG.log(Level.WARNING, "AMQP initialisation problem", e);
    }

}

From source file:com.analogmountains.flume.RabbitMQSource.java

License:Open Source License

@Override
public void configure(Context context) {
    exchangeName = context.getString("exchangeName");
    exchangeType = context.getString("exchangeType");
    queueName = context.getString("queueName");
    bindingKey = context.getString("bindingKey");

    factory = new ConnectionFactory();
    factory.setUsername(context.getString("userName"));
    factory.setPassword(context.getString("password"));
    factory.setVirtualHost(context.getString("virtualHost"));
    factory.setHost(context.getString("hostName"));
    factory.setPort(context.getInteger("port", 5672));
}