Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. /**
  2. * Created by Eranda on 4/11/15.
  3. */
  4.  
  5. import com.rabbitmq.client.ConnectionFactory;
  6. import com.rabbitmq.client.Connection;
  7. import com.rabbitmq.client.Channel;
  8. import com.rabbitmq.client.QueueingConsumer;
  9.  
  10. import java.io.IOException;
  11.  
  12. public class Consumer {
  13. private final static String QUEUE_NAME = "hello";
  14. private final static boolean DURABLE =false;
  15. private final static boolean EXCLUSIVE =false;
  16. private final static boolean AUTO_DELETE =false;
  17.  
  18. public static void main(String[] argv) throws Exception {
  19.  
  20. ConnectionFactory factory = new ConnectionFactory();
  21. factory.setHost("localhost");
  22. Connection connection = factory.newConnection();
  23. Channel channel = connection.createChannel();
  24.  
  25. try{
  26. //checking whether queue is already declared throws exception
  27. // if its not exist or already declared as exclusive
  28. channel.queueDeclarePassive(QUEUE_NAME);
  29. } catch (IOException e) {
  30. channel = connection.createChannel();
  31. channel.queueDeclare(QUEUE_NAME, DURABLE, EXCLUSIVE, AUTO_DELETE, null);
  32. }
  33.  
  34. System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
  35.  
  36. QueueingConsumer consumer = new QueueingConsumer(channel);
  37. channel.basicConsume(QUEUE_NAME, true, consumer);
  38.  
  39. while (true) {
  40. QueueingConsumer.Delivery delivery = consumer.nextDelivery();
  41. String message = new String(delivery.getBody());
  42. System.out.println(" [x] Received '" + message + "'");
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement