title: Reading from RabbitMQ concurrently
body: <p>Basically I am trying to read from RabbitMQ.
  I need to do it concurrently.Hence I am using ThreadPoolExecutor.
  I have written the below classes for the same.I am using intelliJ IDEA.</p>
  
  <p>1) RabbitMQConsumer.java</p>
  
  <pre><code>package consume.rabbitmq;
  
  
  import com.rabbitmq.client.Connection;
  import com.rabbitmq.client.Channel;
  import com.rabbitmq.client.*;
  
  import java.io.IOException;
  import java.util.concurrent.TimeoutException;
  
  public class RabbitMQConsumer implements Runnable {
  
  private String name;
  
  public RabbitMQConsumer(String name)
  {
      this.name = name;
  }
  
  public String getName() {
      return name;
  }
  
  public void run() {
  
      ConnectionFactory factory = new ConnectionFactory();
      factory.setUsername("admin");
      factory.setPassword("password");
      factory.setVirtualHost("abc-pqr.dessqa.com");
      factory.setHost("abc-pqr.dessqa.com");
      factory.setPort(5672);
      Connection conn = null;
      Channel channel = null;
      String exchangeName = "amq.direct";
      String queueName = "cust-670";
      boolean durable = true;
      boolean noAck = false;
      QueueingConsumer consumer = new QueueingConsumer(channel);
      boolean runInfinite = true;
  
      try {
          conn = factory.newConnection();
          channel = conn.createChannel();
          channel.exchangeDeclare(exchangeName, "direct", durable);
          channel.queueDeclare(queueName, durable,false,false,null);
          channel.basicConsume(queueName, noAck, consumer);
          Long duration = (long) (Math.random() * 10);
          System.out.println("Consuming the queue during : " + name);
          TimeUnit.SECONDS.sleep(duration);
  
          while (runInfinite) {
              QueueingConsumer.Delivery delivery;
              try {
                  delivery = consumer.nextDelivery();
              } catch (InterruptedException ie) {
                  continue;
              }
              System.out.println("Message received"
                      + new String(delivery.getBody()));
              try {
                  channel.basicAck(delivery.getEnvelope().getDeliveryTag(),     false);
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          channel.close();
          conn.close();
  
      } catch (IOException e) {
          e.printStackTrace();
      } catch (TimeoutException e) {
          e.printStackTrace();
      }
  
   }
  
  }
  </code></pre>
  
  <p>2) BasicThreadPoolExecutor</p>
  
  <pre><code>package consume.rabbitmq;
  
  import java.util.concurrent.Executors;
  import java.util.concurrent.ThreadPoolExecutor;
  import java.util.concurrent.TimeUnit;
  
  
  public class BasicThreadPoolExecutor {
  
  public static void main(String[] args){
  
      ThreadPoolExecutor executor = (ThreadPoolExecutor)   Executors.newCachedThreadPool();
      for (int i = 0; i &lt;= 5; i++)
      {
          RabbitMQConsumer rabbitMQConsumer = new  RabbitMQConsumer("rabbitMQConsumer " + i);
          System.out.println("A new rabbitMQConsumer has been added : " +  rabbitMQConsumer.getName());
          executor.execute(rabbitMQConsumer);
      }
      executor.shutdown();
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The code works fine and I am able to read from Rabbitmq.
  But I am not sure whether my approach is correct.Can we optimize it furthur?</p>
  
  <p>Regards</p>