}
@Override
public void run() {
Entity entity = null;
EntityBuffer entityBuffer = null;
NewTypeNotification notification = null;
while (running || !taskQueue.isEmpty()) {
try {
// Check if a NewTypeNotification was triggered
if ((notification = newTypeNotification.getAndSet(null)) != null) {
LOG.fine("NewTypeNotification detected, flushing...");
if (entityBuffer != null) entityBuffer.flush();
entityBuffer = bufferFactory.buildForType(notification.getType());
notification.getLatch().countDown();
}
// Poll the queue
if ((entity = taskQueue.poll(WorkerPool.POLL_INTERVAL, TimeUnit.MILLISECONDS)) != null) {
entityBuffer.add(entity);
}
} catch (InterruptedException e) {
LOG.fine("InterruptedException triggered, leaving...");
}
}
if (entityBuffer != null) entityBuffer.flush();
LOG.fine(String.format("%s shutdown", getName()));
}