|
@@ -0,0 +1,71 @@
|
|
|
+package cn.com.qmth.examcloud.task.service.job;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.PageRequest;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.data.domain.Sort;
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.task.base.AbstractTask;
|
|
|
+import cn.com.qmth.examcloud.task.base.ScheduleJob;
|
|
|
+import cn.com.qmth.examcloud.task.base.TaskTracker;
|
|
|
+import cn.com.qmth.examcloud.task.dao.TaskTraceRepo;
|
|
|
+import cn.com.qmth.examcloud.task.dao.entity.TaskTraceEntity;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 任务跟踪日志清理
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @date 2018年11月7日
|
|
|
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
|
+ */
|
|
|
+@Component("taskTraceCleanTask")
|
|
|
+public class TaskTraceCleanTask extends AbstractTask {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ TaskTracker TaskTracker;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ TaskTraceRepo taskTraceRepo;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run(ScheduleJob scheduleJob) throws Exception {
|
|
|
+ Specification<TaskTraceEntity> specification = (root, query, cb) -> {
|
|
|
+ List<Predicate> predicates = new ArrayList<>();
|
|
|
+ // 清理过去7天前的数据
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(new Date());
|
|
|
+ c.add(Calendar.DATE, -7);
|
|
|
+ Date d = c.getTime();
|
|
|
+ Predicate p3 = cb.lessThan(root.get("creationTime"), d);
|
|
|
+ predicates.add(p3);
|
|
|
+
|
|
|
+ return cb.and(predicates.toArray(new Predicate[predicates.size()]));
|
|
|
+ };
|
|
|
+
|
|
|
+ while (true) {
|
|
|
+ Pageable pageable = new PageRequest(0, 50, Sort.Direction.ASC, "creationTime");
|
|
|
+ Page<TaskTraceEntity> list = taskTraceRepo.findAll(specification, pageable);
|
|
|
+ if (1 > list.getSize()) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ for (TaskTraceEntity cur : list) {
|
|
|
+ taskTraceRepo.delete(cur);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TaskTracker getTaskTracker() {
|
|
|
+ return TaskTracker;
|
|
|
+ }
|
|
|
+}
|