【问题标题】:Execute methods inside CRON JOBS在 CRON JOBS 中执行方法
【发布时间】:2019-05-07 11:58:15
【问题描述】:

我的问题是关于在 Spring Boot 中执行 Cron Job 中的方法。我在下面有一个名为 Task 的类

@Entity
@Table(name = "task", schema = "public")
public class Task {

 @Id
 @GeneratedValue
 private Long id;

 @NotEmpty
 private String date;

 @NotEmpty
 private String startTime;

 @NotEmpty
 private String stopTime;

 @NotEmpty
 @Column(length=1000)
 private String description;

 @ManyToOne
 @JoinColumn(name="USER_EMAIL")
 private User user;

 public Long getId() {
    return id;
 }
 public void setId(Long id) {
    this.id = id;
 }
 public String getDate() {
    return date;
 }
 public void setDate(String date) {
    this.date = date;
 }
 public String getStartTime() {
    return startTime;
 }
 public void setStartTime(String startTime) {
    this.startTime = startTime;
 }
 public String getStopTime() {
    return stopTime;
 }
 public void setStopTime(String stopTime) {
    this.stopTime = stopTime;
 }
 public String getDescription() {
    return description;
 }
 public void setDescription(String description) {
    this.description = description;
 }
 public User getUser() {
    return user;
  }
 public void setUser(User user) {
    this.user = user;
 }

 public Task(String date, String startTime, String stopTime, String description, User user) {
    this.date = date;
    this.startTime = startTime;
    this.stopTime = stopTime;
    this.description = description;
    this.user = user;
 }

 public Task(String date, String startTime, String stopTime, String description) {
    this.date = date;
    this.startTime = startTime;
    this.stopTime = stopTime;
    this.description = description;
 }

 public Task() {
 }

}

任务有一个stopTime,我想在超过截止日期时删除该任务。时间将从 Cron 作业 方法检查,如下所示

@Scheduled(cron = "0 * * * * ?")
public void scheduleTaskWithCronExpression() {
    logger.info("Cron Task :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()));
}

比在 TaskRepository 中我创建了一个查询来获取所有任务的截止时间

public interface TaskRepository extends JpaRepository<Task, Long> {
@Modifying
@Query("select stopTime from Task ")
ZonedDateTime showEndTimeTasks(ZonedDateTime stopTime);
}

这里是删除方法

@GetMapping("deleteTask")
public void deleteTask(@RequestParam long id, HttpServletResponse response) throws Exception {
    taskService.deleteTask(id);
    response.sendRedirect("/profile");
}

有了这一切,我怎样才能自动删除所有已超过截止日期的任务? 提前致谢!

【问题讨论】:

    标签: spring postgresql hibernate spring-boot cron


    【解决方案1】:

    为什么不使用这样的东西:

    @Scheduled(cron = "0 * * * * ?")
    public void scheduleTaskWithCronExpression() {
        DateTime currentT = dateTimeFormatter.format(LocalDateTime.now()));
        List<Task> tasks2beDeleted = taskService.expiredTasks(DateTime currentT); 
        taskService.deleteAll(tasks2beDeleted);
    }
    

    几点说明:

    • 您的存储库看起来很奇怪,它应该返回过期的任务而不是时间。
    • 您可以改为使用 db 的 now() 方法实现 expiredTasks
    • 我不知道怎么做,但如果没问题,你可以在你的数据库中实现一个过程
    • 好像你做了一个使用repository的服务层,你也可以实现一个使用两个repository方法的服务方法,比如taskService.deleteAllExpired();这似乎好多了。

    【讨论】:

      猜你喜欢
      • 2014-04-06
      • 2016-06-17
      • 2017-10-17
      • 1970-01-01
      • 2018-08-03
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 2011-08-15
      相关资源
      最近更新 更多