Skip to content

Commit

Permalink
Scheduled Task example (#19)
Browse files Browse the repository at this point in the history
* Schedule un-activate patients not updated in the last year.
  • Loading branch information
alfonsserra authored Dec 10, 2018
1 parent 6c9284e commit fba3987
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/java/com/systelab/seed/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.context.annotation.Bean;

@EnableCircuitBreaker
@EnableHystrixDashboard
@EnableFeignClients
@EnableCaching
@EnableScheduling
@SpringBootApplication
public class App {

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/systelab/seed/model/ModelBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public abstract class ModelBase {
@JsonIgnore
protected Timestamp updateTime;

protected Boolean active = Boolean.TRUE;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import com.systelab.seed.model.patient.Patient;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Optional;
import java.util.UUID;

Expand All @@ -13,4 +17,9 @@ public interface PatientRepository extends JpaRepository<Patient, UUID> {

Optional<Patient> findById(@Param("id") UUID id);

@Modifying
@Transactional
@Query("update Patient p set p.active = FALSE where p.updateTime < ?1")
int setActiveForUpdatedBefore(Date somedate);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.systelab.seed.service;

import com.systelab.seed.repository.PatientRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Calendar;

@Service
public class PatientMaintenanceService {

private Logger logger = LoggerFactory.getLogger(PatientMaintenanceService.class);

@Autowired
private PatientRepository patientRepository;

@Scheduled(cron = "${patient.maintenance.cron.expression}")
public void schedulePurgeOlderRecordsTask() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -1);
this.patientRepository.setActiveForUpdatedBefore(cal.getTime());
logger.info("Patients DB purged!");
}
}
8 changes: 7 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,10 @@ bucket4j:
- bandwidths:
- capacity: 10000
time: 1
unit: minutes
unit: minutes

patient:
maintenance:
cron:
# Check https://www.baeldung.com/cron-expressions
expression: "0/20 * * * * *"

0 comments on commit fba3987

Please sign in to comment.