private void cleanUpObsoleteEvents() {
if ( this.cleanupPeriod > 0 ) {
this.logger.debug("Cleaning up distributed events, removing all entries older than {} minutes.", this.cleanupPeriod);
ResourceResolver resolver = null;
try {
resolver = this.resourceResolverFactory.getAdministrativeResourceResolver(null);
final ResourceUtil.BatchResourceRemover brr = ResourceUtil.getBatchResourceRemover(50);
final Resource baseResource = resolver.getResource(this.ownRootPath);
// sanity check - should never be null
if ( baseResource != null ) {
final Calendar oldDate = Calendar.getInstance();
oldDate.add(Calendar.MINUTE, -1 * this.cleanupPeriod);
// check years
final int oldYear = oldDate.get(Calendar.YEAR);
final Iterator<Resource> yearIter = baseResource.listChildren();
while ( yearIter.hasNext() ) {
final Resource yearResource = yearIter.next();
final int year = Integer.valueOf(yearResource.getName());
if ( year < oldYear ) {
brr.delete(yearResource);
} else if ( year == oldYear ) {
// same year - check months
final int oldMonth = oldDate.get(Calendar.MONTH) + 1;
final Iterator<Resource> monthIter = yearResource.listChildren();
while ( monthIter.hasNext() ) {
final Resource monthResource = monthIter.next();
final int month = Integer.valueOf(monthResource.getName());
if ( month < oldMonth ) {
brr.delete(monthResource);
} else if ( month == oldMonth ) {
// same month - check days
final int oldDay = oldDate.get(Calendar.DAY_OF_MONTH);
final Iterator<Resource> dayIter = monthResource.listChildren();
while ( dayIter.hasNext() ) {
final Resource dayResource = dayIter.next();
final int day = Integer.valueOf(dayResource.getName());
if ( day < oldDay ) {
brr.delete(dayResource);
} else if ( day == oldDay ) {
// same day - check hours
final int oldHour = oldDate.get(Calendar.HOUR_OF_DAY);
final Iterator<Resource> hourIter = dayResource.listChildren();
while ( hourIter.hasNext() ) {
final Resource hourResource = hourIter.next();
final int hour = Integer.valueOf(hourResource.getName());
if ( hour < oldHour ) {
brr.delete(hourResource);
} else if ( hour == oldHour ) {
// same hour - check minutes
final int oldMinute = oldDate.get(Calendar.MINUTE);
final Iterator<Resource> minuteIter = hourResource.listChildren();
while ( minuteIter.hasNext() ) {
final Resource minuteResource = minuteIter.next();
final int minute = Integer.valueOf(minuteResource.getName());
if ( minute < oldMinute ) {
brr.delete(minuteResource);
}
}
}
}
}
}
}
}
}
}
}
// final commit for outstanding resources
resolver.commit();
} catch (final PersistenceException pe) {
// in the case of an error, we just log this as a warning
this.logger.warn("Exception during job resource tree cleanup.", pe);
} catch (final LoginException ignore) {
this.ignoreException(ignore);
} finally {
if ( resolver != null ) {
resolver.close();
}
}
}
}