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:SendMessage.java

public static void main(String[] argv) throws Exception {

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

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    String message = "Message 004!";

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

    channel.close();/*from  w  w  w. ja va 2s. c o m*/
    connection.close();
}

From source file:Rece.java

public static void main(String[] argv) throws Exception {
    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(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        @Override//from  w w  w  . jav  a  2s.  c o  m
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
        }
    };
    channel.basicConsume(QUEUE_NAME, true, consumer);
}

From source file:RecieveMessage.java

public static void main(String[] argv) throws Exception {

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

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    Consumer consumer = new DefaultConsumer(channel) {
        //@Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
        }//  w  w w.java  2s  .  c om
    };
    channel.basicConsume(QUEUE_NAME, true, consumer);
}

From source file:ThreadWorkers.java

public void run() {
    System.out.println("MyThread running");

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = null;
    try {/*  w  w w .jav a 2s. com*/
        connection = factory.newConnection();
    } catch (IOException ex) {
        Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex);
    }
    Channel channel = null;
    try {
        channel = connection.createChannel();
    } catch (IOException ex) {
        Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
    } catch (IOException ex) {
        Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    try {
        channel.basicQos(1);
    } catch (IOException ex) {
        Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex);
    }

    QueueingConsumer consumer = new QueueingConsumer(channel);
    try {
        channel.basicConsume(TASK_QUEUE_NAME, false, consumer);
    } catch (IOException ex) {
        Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex);
    }

    while (true) {
        QueueingConsumer.Delivery delivery = null;
        try {
            delivery = consumer.nextDelivery();
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ShutdownSignalException ex) {
            Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ConsumerCancelledException ex) {
            Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex);
        }
        String message = new String(delivery.getBody());

        System.out.println(" [x] Received '" + message + "'");
        try {

            doWork(message);
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(" [x] Done");

        try {
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        } catch (IOException ex) {
            Logger.getLogger(ThreadWorkers.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:ThreadWorkers.java

private static String doWork(String task) throws InterruptedException, IOException {
    //for (char ch: task.toCharArray()) {
    //  if (ch == '.') Thread.sleep(1000);
    //}/*from w w  w .ja  v a  2  s .  c o  m*/
    String result = "empty result";
    long threadId = Thread.currentThread().getId();
    System.out.println("in thread>>>>" + threadId);

    if (task.equalsIgnoreCase(""))
        return result;
    if (task == null)
        return result;
    //if(task.charAt(0)=='f' || task.charAt(0)=='1') return result;

    if (task.charAt(0) == 'f')
        result = "factorial of " + task.substring(1) + ">>" + factor(task.substring(1)).toString();
    else if (task.charAt(0) == 'p') {

        if (isPrime(task.substring(1).toString()))
            result = task.substring(1).toString() + " is prime";
        else
            result = task.substring(1).toString() + " is not prime";
    } else {
        result = "exponent " + task.substring(1) + ">>" + exp(task.substring(1)).toString();
    }
    // sending result
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(TASK_QUEUE_REPLY_NAME, true, false, false, null);
    //String[] msg= {"10","second",".........."};
    //String[] msg= {"1","second","."};
    //String message ="rvr";// getMessage(msg);

    channel.basicPublish("", TASK_QUEUE_REPLY_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, result.getBytes());
    System.out.println(" [x] Sent reply to client:: '" + result + "'");

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

    return result;
}

From source file:ReplyUtil.java

public void getReply() throws IOException, InterruptedException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

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

    channel.basicQos(1);//  w  ww.ja v a  2s .c o  m

    QueueingConsumer consumer2 = new QueueingConsumer(channel);
    channel.basicConsume(TASK_QUEUE_REPLY_NAME, false, consumer2);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer2.nextDelivery();
        String message = new String(delivery.getBody());

        System.out.println(" [x] Receiving reply from worker::'" + message + "'");
        //doWork(message); 
        //System.out.println(" [x] Done" );

        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

    }

}

From source file:RabbitMQConnector.java

License:Open Source License

@Override
boolean sendMsg(String msg) {
    try {//  www. ja va 2 s  .  c om
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        AMQP.Queue.DeclareOk myQueue = channel.queueDeclare(queueName, true, false, false, null);
        String message = msg;
        channel.basicPublish("", queueName, null, message.getBytes());
        channel.close();
        connection.close();
    } catch (Exception ex) {
        return false;
    }
    return true;
}

From source file:SenderUtil.java

public void sendMessage(String msg) throws IOException {

    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[] msg= {"10","second",".........."};
    //String[] msg= {"1","second","."};
    String message = msg;//getMessage(msg);

    channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
    System.out.println(" [x] Sent '" + message + "'");

    channel.close();/*from   www . j  a v  a  2  s. c om*/
    connection.close();

}

From source file:blocker.Blocker.java

/**
 * @param argv/*from   ww  w . jav  a 2 s  .c  o  m*/
 */
public static void main(String[] argv) throws Exception {
    seconds = Integer.parseInt(argv[7]);
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(argv[0]);
    factory.setUsername(argv[2]);
    factory.setPassword(argv[3]);
    factory.setVirtualHost(argv[1]);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(argv[4], "direct", true);
    String queueName = channel.queueDeclare(argv[5], true, false, false, null).getQueue();

    //                          exchange  key
    channel.queueBind(queueName, argv[4], argv[6]);

    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    Consumer consumer = new DefaultConsumer(channel) {
        @Override
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            JSONParser parser = new JSONParser();

            Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(Thread th, Throwable ex) {
                    System.out.println("Uncaught exception: " + ex);
                }
            };

            try {
                Object obj = parser.parse(message);
                JSONObject jobj = (JSONObject) obj;
                String IP = (String) jobj.get("clientip");
                Thread t = new Thread(new BlockerThread(IP));
                t.setUncaughtExceptionHandler(h);
                t.start();
            } catch (ParseException ex) {
                Logger.getLogger(Blocker.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    };
    channel.basicConsume(argv[5], true, consumer);
}

From source file:br.uff.labtempo.omcp.common.utils.RabbitUtil.java

License:Apache License

public static AMQP.Queue.DeclareOk declareDurableQueue(Channel channel, String queueName) throws IOException {
    Map<String, Object> args = new HashMap<>();
    args.put("x-message-ttl", 60000);
    return channel.queueDeclare(queueName, true, false, false, args);
}