title: Realtime Executor Pool for Queue and Worker design
body: <p>Rightnow, I'm trying to implement a queue-workers based design, where we receive millions of messages in queue. And workers are limited, so I'm using the following code assign work to workers. I'm using ExecutorService for the same:</p>
  
  <pre><code>    ExecutorService pool = Executors.newFixedThreadPool(POOL_SIZE); 
  
      while(LISTEN_FOR_MESSAGE_FLAG == true){
        Message receivedMessage = sqsClient.receiveMessage(request);
  
        if(receivedMessage == null){
          Thread.sleep(5000); // sleep for 5 seconds
        }
        else {
          // lock the message for a certain amount of time (60 secs). 
          // Other workers can't receive a message, when it is locked.
          sqsClient.changeMessageVisibility(receivedMessage, 60); 
  
          pool.execute(new Task(receivedMessage); // process the message.
        }
      }
  </code></pre>
  
  <p>I'm currently using Amazon SQS for the queue. Above code has serious problem. It receives the messages every 5 seconds and lock them with a visibility timeout. This lock will be broken once the visibility timeout is gone. Which causes workers to hold messages which are NOT LOCKED anymore. Therefore, it has problems of duplicate processing. 
  Note: Amazon SQS provides a way to extend the visibility timeout. </p>
  
  <p>Please help, how can above code be written to handle this case. </p>