Skip to content

Instantly share code, notes, and snippets.

@ganeshs
Created February 12, 2020 17:28
Show Gist options
  • Save ganeshs/d25b11535aa0550a169e8ccd0911d108 to your computer and use it in GitHub Desktop.
Save ganeshs/d25b11535aa0550a169e8ccd0911d108 to your computer and use it in GitHub Desktop.
Bulk handler
public static class BulkHandler implements Handler, Runnable {
private BlockingQueue<Record> queue = new LinkedBlockingQueue<>(1000);
public BulkHandler() {
new Thread(this).start();
}
@Override
public Status process(Record record) {
queue.offer(record);
return Status.SUCCESS;
}
public void run() {
while (true) {
if (queue.isEmpty()) {
Thread.sleep(100);
continue;
}
List<Record> records = new ArrayList<>();
queue.drainTo(records, 100);
process(records);
}
}
public void process(List<Record> records) {
try {
// Handle bulk records
} catch (Exception e) {
// Push it to retry or dlq
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment