Do you want to reject with an Exception, or do you just want to ignore them.
A better approach might be to update a Map and ignore duplicate updates to a given key. This drops events you couldn't process but ensuring the latest update of a key is always taken.
This way you consume N new message per second and can gracefully drop duplicates.
e.g.
final ConcurrentMap<String, Message> map = new ConcurrentHashMap<>(); final BlockingQueue<String> queue = new ArrayBlockingQueue<>(10000); int updateCount = 0; long lastUpdate = 0; public void enqueue(String key, Message message) { map.put(key, message); queue.offer(key); } public Message dequeue() throws InterruptedException { long updateTime = System.currentTimeMillis(); long time = updateTime - lastUpdate; lastUpdate = updateTime; if (time > count) count = 0; else count -= time; if (count > 1000) Thread.sleep(1); count++; while(true) { String key = queue.take(); Message msg = map.remove(key); if (msg != null) return msg; // if the msg is null, we have already sent the latest update for that key } }
This will give you an update rate of 1000 per second and always give you the latest update for any key.