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:com.anteam.demo.rabbitmq.RabbitMQProducer.java

License:Apache License

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

    // //from   w w  w. j  av  a2s  . co 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);/*  w  w w . ja  v  a2  s  . c  om*/
        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 a v a2s .  co m
    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();//  ww w .  j  av  a 2 s . c o m

    // 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.audaexplore.b2b.GenAddFnol.java

private void publishFnol(List<Fnol> fnolList) {
    String amqpURI = null;//w  w  w. ja va  2s . c om
    ConnectionFactory factory = null;
    Channel channel = null;
    Connection connection = null;
    String message;
    String BOUND_SERVICES_ENV_VARIABLE_NAME = "VCAP_SERVICES";
    Map<String, String> env = System.getenv();
    String boundServicesJson = env.get(BOUND_SERVICES_ENV_VARIABLE_NAME);
    //String boundServicesJson="{\"staging_env_json\":{},\"running_env_json\":{},\"system_env_json\":{\"VCAP_SERVICES\":{\"cloudamqp\":[{\"name\":\"MQRabbit\",\"label\":\"cloudamqp\",\"tags\":[\"Web-based\",\"UserProvisioning\",\"MessagingandQueuing\",\"amqp\",\"Backup\",\"SingleSign-On\",\"NewProduct\",\"rabbitmq\",\"CertifiedApplications\",\"Android\",\"DeveloperTools\",\"DevelopmentandTestTools\",\"Buyable\",\"Messaging\",\"Importable\",\"ITManagement\"],\"plan\":\"lemur\",\"credentials\":{\"uri\":\"amqp://kujcbqju:xr-HKmBcq5Lv87CCrYlQ6NaVmunhU8cv@moose.rmq.cloudamqp.com/kujcbqju\",\"http_api_uri\":\"https://kujcbqju:xr-HKmBcq5Lv87CCrYlQ6NaVmunhU8cv@moose.rmq.cloudamqp.com/api/\"}}],\"newrelic\":[{\"name\":\"NewRelic\",\"label\":\"newrelic\",\"tags\":[\"Monitoring\"],\"plan\":\"standard\",\"credentials\":{\"licenseKey\":\"a8a96a124d1b58d708a2c4c07c6cff8938e2e2f4\"}}],\"mongolab\":[{\"name\":\"MongoDB\",\"label\":\"mongolab\",\"tags\":[\"DataStore\",\"document\",\"mongodb\"],\"plan\":\"sandbox\",\"credentials\":{\"uri\":\"mongodb://CloudFoundry_31lvrquo_j44bi0vu_3gjp7i4s:6RAtFVBfQUCe_DV7LAq5uCffOXaEXdly@ds047315.mongolab.com:47315/CloudFoundry_31lvrquo_j44bi0vu\"}}]}},\"application_env_json\":{\"VCAP_APPLICATION\":{\"limits\":{\"mem\":512,\"disk\":1024,\"fds\":16384},\"application_id\":\"87bdc475-83c4-4df9-92d1-40ff9bf82249\",\"application_version\":\"52891578-5906-4846-9231-afe7048f29bf\",\"application_name\":\"vinservice\",\"application_uris\":[\"vinservice.cfapps.io\"],\"version\":\"52891578-5906-4846-9231-afe7048f29bf\",\"name\":\"vinservice\",\"space_name\":\"development\",\"space_id\":\"d33d438c-860a-46d3-ab33-4c2efac841be\",\"uris\":[\"vinservice.cfapps.io\"],\"users\":null}}}";

    if (StringUtils.isNotBlank(boundServicesJson)) {
        //amqpURI =  JsonPath.read(boundServicesJson, "$..cloudamqp[0].credentials.uri",String.class);
        JSONArray jsonArray = JsonPath.read(boundServicesJson, "$..cloudamqp[0].credentials.uri");
        amqpURI = jsonArray.get(0).toString();
    } else {
        amqpURI = "amqp://localhost";
    }

    System.out.println("Sending messages to " + amqpURI);
    //System.exit(0);

    try {
        factory = new ConnectionFactory();
        factory.setUri(amqpURI);
        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.queueDeclare("fnol", true, false, false, null);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (TimeoutException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (KeyManagementException e1) {
        e1.printStackTrace();
    } catch (NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    } catch (URISyntaxException e1) {
        e1.printStackTrace();
    }

    for (Fnol fnol : fnolList) {
        message = new Gson().toJson(fnol);

        try {
            channel.basicPublish("amq.direct", "fnolKey",
                    new AMQP.BasicProperties.Builder().contentType("text/plain").deliveryMode(2).build(),
                    //MessageProperties.PERSISTENT_TEXT_PLAIN,
                    message.getBytes());
            System.out.println("message " + message + " was published");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    try {
        channel.close();
        connection.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TimeoutException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.audaexplore.b2b.GenAddFnol.java

private void publishCars(List<Car> carList) {
    String amqpURI = null;/*from   w w  w . ja  v a2  s.co m*/
    ConnectionFactory factory = null;
    Channel channel = null;
    Connection connection = null;
    String message;
    String BOUND_SERVICES_ENV_VARIABLE_NAME = "VCAP_SERVICES";
    Map<String, String> env = System.getenv();
    String boundServicesJson = env.get(BOUND_SERVICES_ENV_VARIABLE_NAME);
    //String boundServicesJson="{\"staging_env_json\":{},\"running_env_json\":{},\"system_env_json\":{\"VCAP_SERVICES\":{\"cloudamqp\":[{\"name\":\"MQRabbit\",\"label\":\"cloudamqp\",\"tags\":[\"Web-based\",\"UserProvisioning\",\"MessagingandQueuing\",\"amqp\",\"Backup\",\"SingleSign-On\",\"NewProduct\",\"rabbitmq\",\"CertifiedApplications\",\"Android\",\"DeveloperTools\",\"DevelopmentandTestTools\",\"Buyable\",\"Messaging\",\"Importable\",\"ITManagement\"],\"plan\":\"lemur\",\"credentials\":{\"uri\":\"amqp://kujcbqju:xr-HKmBcq5Lv87CCrYlQ6NaVmunhU8cv@moose.rmq.cloudamqp.com/kujcbqju\",\"http_api_uri\":\"https://kujcbqju:xr-HKmBcq5Lv87CCrYlQ6NaVmunhU8cv@moose.rmq.cloudamqp.com/api/\"}}],\"newrelic\":[{\"name\":\"NewRelic\",\"label\":\"newrelic\",\"tags\":[\"Monitoring\"],\"plan\":\"standard\",\"credentials\":{\"licenseKey\":\"a8a96a124d1b58d708a2c4c07c6cff8938e2e2f4\"}}],\"mongolab\":[{\"name\":\"MongoDB\",\"label\":\"mongolab\",\"tags\":[\"DataStore\",\"document\",\"mongodb\"],\"plan\":\"sandbox\",\"credentials\":{\"uri\":\"mongodb://CloudFoundry_31lvrquo_j44bi0vu_3gjp7i4s:6RAtFVBfQUCe_DV7LAq5uCffOXaEXdly@ds047315.mongolab.com:47315/CloudFoundry_31lvrquo_j44bi0vu\"}}]}},\"application_env_json\":{\"VCAP_APPLICATION\":{\"limits\":{\"mem\":512,\"disk\":1024,\"fds\":16384},\"application_id\":\"87bdc475-83c4-4df9-92d1-40ff9bf82249\",\"application_version\":\"52891578-5906-4846-9231-afe7048f29bf\",\"application_name\":\"vinservice\",\"application_uris\":[\"vinservice.cfapps.io\"],\"version\":\"52891578-5906-4846-9231-afe7048f29bf\",\"name\":\"vinservice\",\"space_name\":\"development\",\"space_id\":\"d33d438c-860a-46d3-ab33-4c2efac841be\",\"uris\":[\"vinservice.cfapps.io\"],\"users\":null}}}";

    if (StringUtils.isNotBlank(boundServicesJson)) {
        //amqpURI =  JsonPath.read(boundServicesJson, "$..cloudamqp[0].credentials.uri",String.class);
        JSONArray jsonArray = JsonPath.read(boundServicesJson, "$..cloudamqp[0].credentials.uri");
        amqpURI = jsonArray.get(0).toString();
    } else {
        amqpURI = "amqp://localhost";
    }

    System.out.println("Sending messages to " + amqpURI);
    //System.exit(0);

    try {
        factory = new ConnectionFactory();
        factory.setUri(amqpURI);
        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.queueDeclare("fnol", true, false, false, null);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (TimeoutException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (KeyManagementException e1) {
        e1.printStackTrace();
    } catch (NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    } catch (URISyntaxException e1) {
        e1.printStackTrace();
    }

    for (Car car : carList) {
        message = new Gson().toJson(car);

        try {
            channel.basicPublish("amq.direct", "fnolKey",
                    new AMQP.BasicProperties.Builder().contentType("text/plain").deliveryMode(2).build(),
                    //MessageProperties.PERSISTENT_TEXT_PLAIN,
                    message.getBytes());
            System.out.println("message " + message + " was published");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    try {
        channel.close();
        connection.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TimeoutException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.audaexplore.claimservices.listeners.FnolMessageListener.java

public FnolMessageListener() {
    // init messaging    
    ConnectionFactory factory = null;
    String amqpURI = "amqp://localhost";
    Connection connection = null;
    String BOUND_SERVICES_ENV_VARIABLE_NAME = "VCAP_SERVICES";
    Map<String, String> env = System.getenv();
    String boundServicesJson = env.get(BOUND_SERVICES_ENV_VARIABLE_NAME);
    // String//from  w w  w. jav a2  s  .  c om
    // boundServicesJson="{\"staging_env_json\":{},\"running_env_json\":{},\"system_env_json\":{\"VCAP_SERVICES\":{\"cloudamqp\":[{\"name\":\"MQRabbit\",\"label\":\"cloudamqp\",\"tags\":[\"Web-based\",\"UserProvisioning\",\"MessagingandQueuing\",\"amqp\",\"Backup\",\"SingleSign-On\",\"NewProduct\",\"rabbitmq\",\"CertifiedApplications\",\"Android\",\"DeveloperTools\",\"DevelopmentandTestTools\",\"Buyable\",\"Messaging\",\"Importable\",\"ITManagement\"],\"plan\":\"lemur\",\"credentials\":{\"uri\":\"amqp://kujcbqju:xr-HKmBcq5Lv87CCrYlQ6NaVmunhU8cv@moose.rmq.cloudamqp.com/kujcbqju\",\"http_api_uri\":\"https://kujcbqju:xr-HKmBcq5Lv87CCrYlQ6NaVmunhU8cv@moose.rmq.cloudamqp.com/api/\"}}],\"newrelic\":[{\"name\":\"NewRelic\",\"label\":\"newrelic\",\"tags\":[\"Monitoring\"],\"plan\":\"standard\",\"credentials\":{\"licenseKey\":\"a8a96a124d1b58d708a2c4c07c6cff8938e2e2f4\"}}],\"mongolab\":[{\"name\":\"MongoDB\",\"label\":\"mongolab\",\"tags\":[\"DataStore\",\"document\",\"mongodb\"],\"plan\":\"sandbox\",\"credentials\":{\"uri\":\"mongodb://CloudFoundry_31lvrquo_j44bi0vu_3gjp7i4s:6RAtFVBfQUCe_DV7LAq5uCffOXaEXdly@ds047315.mongolab.com:47315/CloudFoundry_31lvrquo_j44bi0vu\"}}]}},\"application_env_json\":{\"VCAP_APPLICATION\":{\"limits\":{\"mem\":512,\"disk\":1024,\"fds\":16384},\"application_id\":\"87bdc475-83c4-4df9-92d1-40ff9bf82249\",\"application_version\":\"52891578-5906-4846-9231-afe7048f29bf\",\"application_name\":\"vinservice\",\"application_uris\":[\"vinservice.cfapps.io\"],\"version\":\"52891578-5906-4846-9231-afe7048f29bf\",\"name\":\"vinservice\",\"space_name\":\"development\",\"space_id\":\"d33d438c-860a-46d3-ab33-4c2efac841be\",\"uris\":[\"vinservice.cfapps.io\"],\"users\":null}}}";

    if (StringUtils.isNotBlank(boundServicesJson)) {
        // amqpURI = JsonPath.read(boundServicesJson,
        // "$..cloudamqp[0].credentials.uri",String.class);
        JSONArray jsonArray = JsonPath.read(boundServicesJson, "$..cloudamqp[0].credentials.uri");
        amqpURI = jsonArray.get(0).toString();
    } else {
        amqpURI = "amqp://localhost";
    }

    System.out.println(amqpURI);

    try {
        factory = new ConnectionFactory();
        factory.setUri(amqpURI);
        connection = factory.newConnection();
        channel = connection.createChannel();
        channel.queueDeclare("fnol", true, false, false, null);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (TimeoutException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (KeyManagementException e1) {
        e1.printStackTrace();
    } catch (NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    } catch (URISyntaxException e1) {
        e1.printStackTrace();
    }
}

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

public RequestConsumer() throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(DEFAULT_HOST);/*from www.j a v a  2 s.  c  o m*/
    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);//w  ww .  j a  v  a  2s  .c  o m
    }
    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;
}