io.omatic.event.service.EventMaintainanceService.java Source code

Java tutorial

Introduction

Here is the source code for io.omatic.event.service.EventMaintainanceService.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.omatic.event.service;

import java.util.Calendar;
import java.util.Date;

import io.omatic.event.model.Event;
import io.omatic.event.model.configuration.ApplicationConfiguration;
import io.omatic.event.repo.EventRepository;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class EventMaintainanceService {

    private static final Logger log = LoggerFactory.getLogger(EventMaintainanceService.class);

    @Autowired
    EventRepository eventRepository;

    @Autowired
    ApplicationConfiguration applicationConfiguration;

    /**
     * Maintenance method used to keep the number of {@link Event} records in the database
     * at a manageable level. 
     * 
     * Note that this method can be scheduled. To do so specify a value for the externalised 
     * property eventomatic.maintenance.cronSchedule. The value must be in crontab format. For more information see 
     * <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/support/CronSequenceGenerator.html">here</a>
     */
    @Transactional
    @Scheduled(cron = "${eventomatic.maintenance.cronSchedule}")
    public void maintainPoll() {

        // Firstly delete any events that are older than they should be, this will stop historic data clogging up the system. 0 disable
        if (applicationConfiguration.getMaintenance().getMaxAge() < 0) {
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_YEAR, applicationConfiguration.getMaintenance().getMaxAge().intValue());
            Date maxAge = cal.getTime();

            long countOldEvents = eventRepository.countByRecievedDateBefore(maxAge);
            log.info("Checking for mesages older than {} and found {}", maxAge, countOldEvents);

            if (countOldEvents > 0) {
                log.info("Deleting {} events from before {}", countOldEvents, maxAge);
                eventRepository.deleteByRecievedDateBefore(maxAge);
                log.info("Deletion complete");
            }
        } else {
            log.info("Age based housekeeping disabled");
        }
        // Secondly, if the max events has exceeded the specified limit then trim the database. 0 or -1 will disable. 
        if (applicationConfiguration.getMaintenance().getEventLimit() > 0) {
            long countTotalEvents = eventRepository.count();
            Long overrun = countTotalEvents - applicationConfiguration.getMaintenance().getEventLimit();
            log.info("Checking for event count of greather than {} and found {}",
                    applicationConfiguration.getMaintenance().getEventLimit(), countTotalEvents);
            if (overrun > 0) {
                Page<Event> events = eventRepository
                        .findAll(new PageRequest(0, overrun.intValue(), new Sort(Direction.ASC, "recievedDate")));
                eventRepository.deleteInBatch(events);
                log.info("Deleted {} of {} events.", overrun, countTotalEvents);
            }
        } else {
            log.info("Event limit based housekeeping disabled");
        }
    }
}