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:com.adr.datatest.SourceLink.java

License:Apache License

public static Connection getConnection() {
    if (connection == null) {
        try {//from w w w . jav  a  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);/*  w  w  w. j a  va 2s .  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  ww  w. 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 + "'");

    }

}

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

License:Apache License

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

    // /*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);

    String message = "Hello World!";

    // ???Exchange??""?
    channel.basicPublish("", QUEUE_NAME, null, message.getBytes());

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

    // ??
    channel.close();
    connection.close();

}

From source file:com.anton.dev.tqrb.MessageListener.java

public static void main(String[] args) throws IOException, TimeoutException, ShutdownSignalException,
        ConsumerCancelledException, InterruptedException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] A la espera de mensajes. Para salir pulse: CTRL+C");
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, true, consumer);

    while (true) {
        System.out.println("Obteniendo siguiente mensaje.");
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println(" [x] Recibido: '" + message + "'");
        doWork(message);/*from w  w  w .  j  a  va2  s.co  m*/
        System.out.println(" [x] Hecho!!! ");
    }
}

From source file:com.anton.dev.tqrb.MessageProducer.java

public static void main(String[] args) throws IOException, TimeoutException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, true, false, false, null);
    String message = "Hola!";
    channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
    System.out.println(" [x] Enviar '" + message + "'");
    channel.close();//from  w w w  .j av a 2s  .  c  om
    connection.close();
}

From source file:com.arakelian.docker.junit.RabbitIntegrationTest.java

License:Apache License

@Test
public void testConnectsToDocker() throws ExecutionException, RetryException {
    final Retryer<Void> retryer = RetryerBuilder.<Void>newBuilder() //
            .retryIfException() //
            .withStopStrategy(StopStrategies.stopAfterDelay(1, TimeUnit.MINUTES)) //
            .withWaitStrategy(WaitStrategies.fixedWait(5, TimeUnit.SECONDS)) //
            .build();/*from   www .  j  av a 2 s .  c  om*/

    // wait for RabbitMQ
    retryer.call(() -> {
        final ConnectionFactory factory = new ConnectionFactory();
        final Container container = rabbitmq.getContainer();
        final Binding binding = container.getPortBinding("5672/tcp");
        factory.setHost(binding.getHost());
        factory.setPort(binding.getPort());
        final Connection connection = factory.newConnection();
        connection.close();
        return null;
    });
}

From source file:com.boulmier.machinelearning.jobexecutor.consumer.RequestConsumer.java

public RequestConsumer() throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(DEFAULT_HOST);
    connection = factory.newConnection();
    channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, true, false, false, null);
    consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, false, consumer);
}

From source file:com.boulmier.machinelearning.jobexecutor.consumer.RequestConsumer.java

public RequestConsumer(InetAddress vmscheduler_ip, Integer port) throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(vmscheduler_ip.getHostAddress());
    if (port != null) {
        factory.setPort(port);//from  ww w  .j  a  v  a 2  s.com
    }
    connection = factory.newConnection();
    channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, true, false, false, null);
    consumer = new QueueingConsumer(channel);
    channel.basicConsume(QUEUE_NAME, false, consumer);
}

From source file:com.buzz.buzzdata.RMQBuzz.java

private Channel getChannel(String queue) throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("hyena.rmq.cloudamqp.com");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(queue, false, false, false, null);
    return channel;
}