class DeviceMessageCleanupJob implements Job {

    @Override
    public void execute(final JobExecutionContext context) {
        LOGGER.info("Quartz triggered cleanup of database - device message records.");
        final DateTime start = DateTime.now();

        try {
            final Date retention = this.calculateRetentionDate();
            final List<DeviceLogItem> oldDeviceMessages = this.transactionalDeviceLogItemService
                    .findDeviceLogItemsBeforeDate(retention, this.deviceMessagePageSize);
            if (!oldDeviceMessages.isEmpty()) {
                this.saveDeviceMessagesToCsvFile(oldDeviceMessages);

                LOGGER.info("Deleting device messages...");
                this.transactionalDeviceLogItemService.deleteDeviceLogItems(oldDeviceMessages);
            }
        } catch (final Exception e) {
            LOGGER.error("Exception during CSV file creation, compression or device message deletion.", e);
        }

        final DateTime end = DateTime.now();
        LOGGER.info("Start: {}, end: {}, duration: {} milliseconds.", start, end, end.getMillis() - start.getMillis());
    }

}