Example usage for com.rabbitmq.client Channel queueDeclare

List of usage examples for com.rabbitmq.client Channel queueDeclare

Introduction

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

Prototype

Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
        Map<String, Object> arguments) throws IOException;

Source Link

Document

Declare a queue

Usage

From source file:com.abiquo.commons.amqp.impl.vsm.VSMConfiguration.java

License:Open Source License

@Override
public void declareQueues(Channel channel) throws IOException {
    channel.queueDeclare(EVENT_SYNK_QUEUE, Durable, NonExclusive, NonAutodelete, null);
    channel.queueBind(EVENT_SYNK_QUEUE, VSM_EXCHANGE, VSM_ROUTING_KEY);
}

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 {

    // /*  ww  w .  j  a  va2s.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);
    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 {

    // //  w  ww.j a  v  a2 s  .com
    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);//  ww  w  .j  a va2 s.  c  o  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();/*  w w  w  .j  a v  a  2s  .c  o  m*/
    connection.close();
}

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

private void publishFnol(List<Fnol> fnolList) {
    String amqpURI = null;/*w  w w  .jav a2s .  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;/* ww  w. j a va  2 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.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;
}

From source file:com.cisco.oss.foundation.message.RabbitMQMessageConsumer.java

License:Apache License

RabbitMQMessageConsumer(String consumerName) {
    try {/*from w  ww  .ja v  a2s.c  om*/
        this.consumerName = consumerName;
        Configuration subset = configuration.subset(consumerName);
        queueName = subset.getString("queue.name");

        String filter = subset.getString("queue.filter", "");
        boolean isAutoDelete = subset.getBoolean("queue.isAutoDelete", false);
        boolean isDurable = subset.getBoolean("queue.isDurable", true);
        boolean isSubscription = subset.getBoolean("queue.isSubscription", false);
        long expiration = subset.getLong("queue.expiration", 1800000);
        long maxLength = subset.getLong("queue.maxLength", -1);
        boolean deadLetterIsEnabled = subset.getBoolean("queue.deadLetterIsEnabled", true);
        String deadLetterExchangeName = subset.getString("queue.deadLetterExchangeName", DLQ);
        String subscribedTo = isSubscription ? subset.getString("queue.subscribedTo", "") : queueName;
        String exchangeType = subset.getString("queue.exchangeType", isSubscription ? "topic" : "direct");
        boolean isExclusive = subset.getBoolean("queue.isExclusive", false);
        try {
            RabbitMQMessagingFactory.INIT_LATCH.await();
        } catch (InterruptedException e) {
            LOGGER.error("error waiting for init to finish: " + e);
        }
        Channel channel = RabbitMQMessagingFactory.getChannel();
        channel.exchangeDeclare(subscribedTo, exchangeType, isDurable, false, false, null);

        Map<String, Object> args = new HashMap<String, Object>();

        if (maxLength > 0) {
            args.put("x-max-length", maxLength);
        }

        if (expiration > 0) {
            args.put("x-message-ttl", expiration);
        }

        if (deadLetterIsEnabled) {
            channel.exchangeDeclare(deadLetterExchangeName, exchangeType, isDurable, false, false, null);
            args.put("x-dead-letter-exchange", deadLetterExchangeName);
        }

        String queue = channel.queueDeclare(queueName, isDurable, isExclusive, isAutoDelete, args).getQueue();

        Map<String, String> filters = ConfigUtil.parseSimpleArrayAsMap(consumerName + ".queue.filters");
        if (filters != null && !filters.isEmpty()) {
            for (String routingKey : filters.values()) {
                channel.queueBind(queue, subscribedTo, routingKey);
            }
        } else {
            channel.queueBind(queue, subscribedTo, "#");
        }

        consumer = new QueueingConsumer(channel);
        //            channel.basicConsume(queueName, true, consumer);
        LOGGER.info("created rabbitmq consumer: {} on exchange: {}, queue-name: {}", consumerName, subscribedTo,
                queueName);
    } catch (IOException e) {
        throw new QueueException("Can't create consumer: " + e, e);
    }

}

From source file:com.cisco.oss.foundation.message.RabbitMQMessageConsumerWithoutTL.java

License:Apache License

RabbitMQMessageConsumerWithoutTL(String consumerName) {
    try {// w  w w  . j  av  a 2s . com
        this.consumerName = consumerName;
        Configuration subset = configuration.subset(consumerName);
        queueName = subset.getString("queue.name");

        String filter = subset.getString("queue.filter", "");
        boolean isAutoDelete = subset.getBoolean("queue.isAutoDelete", false);
        boolean isDurable = subset.getBoolean("queue.isDurable", true);
        boolean isSubscription = subset.getBoolean("queue.isSubscription", false);
        long expiration = subset.getLong("queue.expiration", 1800000);
        long maxLength = subset.getLong("queue.maxLength", -1);
        boolean deadLetterIsEnabled = subset.getBoolean("queue.deadLetterIsEnabled", true);
        String deadLetterExchangeName = subset.getString("queue.deadLetterExchangeName", DLQ);
        String subscribedTo = isSubscription ? subset.getString("queue.subscribedTo", "") : queueName;
        String exchangeType = subset.getString("queue.exchangeType", isSubscription ? "topic" : "direct");
        boolean isExclusive = subset.getBoolean("queue.isExclusive", false);
        try {
            RabbitMQMessagingFactory.INIT_LATCH.await();
        } catch (InterruptedException e) {
            LOGGER.error("error waiting for init to finish: " + e);
        }
        Channel channel = RabbitMQMessagingFactory.createChannelWithoutTL();
        channel.exchangeDeclare(subscribedTo, exchangeType, isDurable, false, false, null);

        Map<String, Object> args = new HashMap<String, Object>();

        if (maxLength > 0) {
            args.put("x-max-length", maxLength);
        }

        if (expiration > 0) {
            args.put("x-message-ttl", expiration);
        }

        if (deadLetterIsEnabled) {
            channel.exchangeDeclare(deadLetterExchangeName, exchangeType, isDurable, false, false, null);
            args.put("x-dead-letter-exchange", deadLetterExchangeName);
        }

        String queue = channel.queueDeclare(queueName, isDurable, isExclusive, isAutoDelete, args).getQueue();

        Map<String, String> filters = ConfigUtil.parseSimpleArrayAsMap(consumerName + ".queue.filters");
        if (filters != null && !filters.isEmpty()) {
            for (String routingKey : filters.values()) {
                channel.queueBind(queue, subscribedTo, routingKey);
            }
        } else {
            channel.queueBind(queue, subscribedTo, "#");
        }

        consumer = new QueueingConsumer(channel);
        //            channel.basicConsume(queueName, true, consumer);
        LOGGER.info("created rabbitmq consumer: {} on exchange: {}, queue-name: {} channel number {}",
                consumerName, subscribedTo, queueName, channel.getChannelNumber());
    } catch (IOException e) {
        throw new QueueException("Can't create consumer: " + e, e);
    }

}