【发布时间】: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