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:org.wso2.siddhi.debs2017.input.DebsBenchmarkSystem.java

License:Open Source License

private RabbitQueue createQueueWithName(String name, ExecutorService executorService) throws Exception {
    Channel channel = createConnection(executorService).createChannel();
    channel.basicQos(getPrefetchCount());
    channel.queueDeclare(name, false, false, true, null);
    return new RabbitQueue(channel, name);
}

From source file:org.wso2.siddhi.debs2017.input.rabbitmq.RabbitMQSampleDataPublisher.java

License:Open Source License

public static void start(String[] argv) {

    try {//w  w  w.  j av  a 2  s  .  c o  m
        if (argv.length > 0) {
            TASK_QUEUE_NAME = argv[0];
        }
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
        String data = "";
        BufferedReader reader = new BufferedReader(new FileReader("frmattedData_63.nt"));//molding_machine_100M.nt rdfSample.txt //Machine_59 //frmattedData.txt
        // read file line by line
        String line;
        Scanner scanner;
        int count = 0;
        while ((line = reader.readLine()) != null) {
            scanner = new Scanner(line);
            while (scanner.hasNext()) {
                String dataInLine = scanner.next();
                if (dataInLine.contains("----")) {
                    if (data.length() > 100) {
                        for (int i = 0; i < 5; i++) {
                            count++;
                            System.out.println(count);
                            String data1 = data.replace("Machine_59", "Machine_" + i).replace("_59_",
                                    "_" + i + "_");
                            channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN,
                                    data1.getBytes());
                        }
                    }
                    data = "";
                } else {
                    data += " " + dataInLine;
                    if (dataInLine.contains(".") && !dataInLine.contains("<")) {
                        data += "\n";
                    }
                }
            }
        }

        channel.close();
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.wso2.siddhi.debs2017.input.rabbitmq.RabbitMQSamplePublisher.java

License:Open Source License

public static void main(String[] argv) throws java.io.IOException, TimeoutException {

    if (argv.length > 0) {
        TASK_QUEUE_NAME = argv[0];/*from  www .ja va  2  s  . c o  m*/
    }
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
    String data = "";
    try {
        BufferedReader reader = new BufferedReader(new FileReader("frmattedData_63.nt"));//molding_machine_100M.nt rdfSample.txt //Machine_59 //frmattedData.txt
        // read file line by line
        String line;
        Scanner scanner;
        int count = 0;
        while ((line = reader.readLine()) != null) {
            scanner = new Scanner(line);
            while (scanner.hasNext()) {
                String dataInLine = scanner.next();
                if (dataInLine.contains("----")) {
                    if (data.length() > 100) {
                        for (int i = 0; i < 5; i++) {
                            count++;
                            System.out.println(count);
                            // System.out.println(data);
                            //Thread.sleep(5000);
                            String data1 = data.replace("Machine_59", "Machine_" + i).replace("_59_",
                                    "_" + i + "_");
                            channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN,
                                    data1.getBytes());
                        }
                    }
                    data = "";
                } else {
                    data += " " + dataInLine;
                    if (dataInLine.contains(".") && !dataInLine.contains("<")) {
                        data += "\n";
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    channel.close();
    connection.close();
}

From source file:pl.nask.hsn2.bus.rabbitmq.RbtUtils.java

License:Open Source License

/**
 * Creates single queue./*from   w ww.ja v  a 2  s  .co m*/
 * 
 * @param channel Channel to create a queue.
 * @param queueName Name of the queue to be created.
 * @throws BusException Any problem will rise the exception.
 */
public static void createQueue(Channel channel, String servicesExchangeName, String queueName)
        throws BusException {
    try {
        channel.queueDeclare(queueName, false, false, false, null);
        channel.queueBind(queueName, servicesExchangeName, queueName);
    } catch (IOException e) {
        throw new BusException("Can not create queue: " + queueName, e);
    }
}

From source file:pl.nask.hsn2.bus.rabbitmq.RbtUtils.java

License:Open Source License

/**
 * Creates single queue.//from  www  . j  a  v a 2s  .c o m
 * 
 * @param connection Connection to Rabbit MQ server.
 * @param queueName Name of the queue to be created.
 * @throws BusException Any problem will rise the exception.
 */
public static void createQueue(Connection connection, String servicesExchangeName, String queueName)
        throws BusException {
    Channel channel = null;
    try {
        channel = createChannel(connection);
        channel.queueDeclare(queueName, false, false, false, null);
        channel.queueBind(queueName, servicesExchangeName, queueName);
    } catch (IOException e) {
        throw new BusException("Can not create queue: " + queueName, e);
    } finally {
        closeChannel(channel);
    }
}

From source file:play.modules.rabbitmq.RabbitMQPlugin.java

License:Apache License

/**
 * Creates the channel./* w  w  w.  j  av  a2s.  c o m*/
 * 
 * @param queue
 *            the queue
 * @return the channel
 * @throws Exception
 *             the exception
 */
public Channel createChannel(String queue, String routingKey) throws Exception {
    // Counter that keeps track of number of retries
    int attempts = 0;

    // Get Plugin
    RabbitMQPlugin plugin = Play.application().plugin(RabbitMQPlugin.class);

    // Create Channel
    Channel channel = this.createChannel();

    // Basic Qos
    if (RabbitMQPlugin.isBasicQos()) {
        int prefetchCount = 1;
        channel.basicQos(prefetchCount);
    }

    // Start Daemon
    while (true) {
        // Add to the number of retries
        attempts++;

        // Log Debug
        Logger.debug("Retry " + attempts);

        // Get Next Delivery Message
        try {
            // http://www.rabbitmq.com/api-guide.html
            // channel.exchangeDeclare(exchangeName, "direct", true);
            // String queueName = channel.queueDeclare().getQueue();
            // channel.queueBind(queueName, exchangeName, routingKey);

            channel.exchangeDeclare(queue, plugin.getExchangeType(), true);
            channel.queueDeclare(queue, plugin.isDurable(), false, false, null);
            channel.queueBind(queue, queue, routingKey);

            // Log Debug
            Logger.info("RabbitMQ Task Channel Available: " + channel);

            // Return Channel
            return channel;

        } catch (Throwable t) {
            // Log Debug
            Logger.error("Error establishing a connection to RabbitMQ, will keep retrying - Exception: %s",
                    ExceptionUtil.getStackTrace(t));

            // Sleep a little while before retrying
            try {
                Thread.sleep(1000 * 10);
            } catch (InterruptedException ex) {
            }
        }
    }
}

From source file:play.modules.rabbitmq.RabbitMQPlugin.java

License:Apache License

/**
    * Creates the channel for a subscriber to consume messages from an exchange
    * (Exchange is created.  Subscribers queue and binding by routing key are created)
    * //from   www  .j  a  va2  s  .  c o  m
    * @param exchangeName
    *            the exchange name
    * @param queue
    *            the queue
    * @param routingKey
    *            the routing key
    * @return the channel
    * @throws Exception
    *             the exception
    */
public Channel createSubscribersChannel(String exchangeName, String queue, String routingKey) throws Exception {
    // Counter that keeps track of number of retries
    int attempts = 0;

    // Get Plugin
    RabbitMQPlugin plugin = Play.application().plugin(RabbitMQPlugin.class);

    // Create Channel
    Channel channel = this.createChannel();

    // Basic Qos
    if (RabbitMQPlugin.isBasicQos()) {
        int prefetchCount = 1;
        channel.basicQos(prefetchCount);
    }

    // Start Daemon
    while (true) {
        // Add to the number of retries
        attempts++;

        // Log Debug
        Logger.debug("Retry " + attempts);

        // Get Next Delivery Message
        try {
            // http://www.rabbitmq.com/api-guide.html
            channel.exchangeDeclare(exchangeName, plugin.getExchangeType(), true);
            channel.queueDeclare(queue, plugin.isDurable(), false, false, null);
            channel.queueBind(queue, exchangeName, routingKey);

            // Log Debug
            Logger.info("RabbitMQ Task Channel Available: " + channel);

            // Return Channel
            return channel;

        } catch (Throwable t) {
            // Log Debug
            Logger.error("Error establishing a connection to RabbitMQ, will keep retrying - Exception: %s",
                    ExceptionUtil.getStackTrace(t));

            // Sleep a little while before retrying
            try {
                Thread.sleep(1000 * 10);
            } catch (InterruptedException ex) {
            }
        }
    }
}

From source file:rabbitmq.Producer.java

public void run() {
    try {/* w  w w. ja  v  a2 s.  c  o m*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

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

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        for (int i = 0; i < num; i++) {
            Mensaje persona = new Mensaje("RabbitMQ_Java", "Persona" + i, "ID" + i);
            Gson gson = new Gson();
            String message = gson.toJson(persona);

            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            System.out.println(" [" + id + "] Sent '" + message + "'");
        }

        channel.close();
        connection.close();
    } catch (IOException ex) {
        Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:rabbitmqapp.RabbitMQApp.java

/**
 * @param args the command line arguments
 *///  w  w w  .ja  v  a 2 s .  c om
public static void main(String[] args) throws Exception {
    ProductOrder order = new ProductOrder("Thando Mlauzi", 34.50);
    ConnectionFactory factory = new ConnectionFactory();
    String uri = "amqp://ytsoedex:Qu2LCiBJ5x9fhRUyLYkMhJqsURJ9dkSP@chicken.rmq.cloudamqp.com/ytsoedex";
    factory.setUri(uri);

    //Recommended settings
    factory.setRequestedHeartbeat(30);
    factory.setConnectionTimeout(30000);

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

    channel.queueDeclare(QUENAME, false, false, false, null);
    String message = "Hello World!";
    channel.basicPublish("", QUENAME, null, RabbitUtility.convertToByteArray(order));
    System.out.println(" [x] Sent '" + order + "'");

    channel.close();
    connection.close();

}

From source file:rabbitmqapp.Recv.java

public static void main(String... args) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    String uri = "amqp://ytsoedex:Qu2LCiBJ5x9fhRUyLYkMhJqsURJ9dkSP@chicken.rmq.cloudamqp.com/ytsoedex";
    factory.setUri(uri);/*  w w  w .  j a va  2  s  . c  o m*/

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

    channel.queueDeclare(QUENAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            System.out.println(String.format("The consumer tag is %s", consumerTag));
            ProductOrder message = (ProductOrder) RabbitUtility.getObject(body);

            //String message = new String(body, "UTF-8");
            System.out.println(" [x] Received name'" + message.getName() + "'");
            System.out.println(" [x] Received price'" + message.getPrice() + "'");
        }
    };
    channel.basicConsume(QUENAME, true, consumer);
}