private void checkAndRemoveStuckItems(String agent,
ReplicationQueueProvider queueProvider) throws ReplicationQueueException {
ReplicationQueue defaultQueue = queueProvider.getDefaultQueue(agent);
// get first item in the queue with its status
ReplicationQueueItem firstItem = defaultQueue.getHead();
if (firstItem != null) {
ReplicationQueueItemState status = defaultQueue.getStatus(firstItem);
// if item is still in the queue after a max no. of attempts, move it to the error queue
int attempts = status.getAttempts();
Calendar entered = status.getEntered();
log.info("item {} entered {} was attempted {} times", new Object[]{firstItem, entered, attempts});
if (attempts > attemptsThreshold || (entered != null && Calendar.getInstance().getTimeInMillis() - entered.getTimeInMillis() > timeThreshold)) {
if (ERROR.equals(stuckQueueHandling)) {
log.warn("item {} moved to the error queue", firstItem);
ReplicationQueue errorQueue = queueProvider.getQueue(agent, ERROR_QUEUE_NAME);
if (!errorQueue.add(firstItem)) {
log.error("failed to move item {} the queue {}", firstItem, errorQueue);
throw new ReplicationQueueException("could not move an item to the error queue");
}
}
log.warn("item {} dropped from the default queue", firstItem);
defaultQueue.remove(firstItem.getId());
}
}
}