Java 更新上次修改日期的最佳解决方案-
我有一个Spring控制器,用于向父项添加不同类型的元素,当我更新或添加新项时,我需要更新其父项的上次修改日期。我的问题是,我们多次调用此更新上次修改日期服务,我希望避免这种情况。 有没有更好或更优雅的方法来解决这个问题,然后是TimerTimer、TimerTask或 ScheduledFuture,ScheduledThreadPoolExecutorJava 更新上次修改日期的最佳解决方案-,java,spring-mvc,scheduled-tasks,timertask,Java,Spring Mvc,Scheduled Tasks,Timertask,我有一个Spring控制器,用于向父项添加不同类型的元素,当我更新或添加新项时,我需要更新其父项的上次修改日期。我的问题是,我们多次调用此更新上次修改日期服务,我希望避免这种情况。 有没有更好或更优雅的方法来解决这个问题,然后是TimerTimer、TimerTask或 ScheduledFuture,ScheduledThreadPoolExecutor Timer updateLastModifiedDateTimer = timers.get(parentkey); if (upd
Timer updateLastModifiedDateTimer = timers.get(parentkey);
if (updateLastModifiedDateTimer != null) {
updateLastModifiedDateTimer.cancel();
}
TimerTask updateLastModifiedDateTask = new TimerTask() {
public void run() {
getPresentationDAO().updateLastModifiedDate(owner, parentkey, System.currentTimeMillis());
}
};
updateLastModifiedDateTimer = new Timer(true);
updateLastModifiedDateTimer.schedule(updateLastModifiedDateTask, updateDelay);
timers.put(parentkey, updateLastModifiedDateTimer);
我用ScheduledFuture&ScheduledThreadPoolExecutor实现了类似的功能。我不明白为什么要在表示层而不是在服务层这样做。我更不明白你为什么用定时器来做这件事。为什么不直接调用服务中的parent.setLastModifiedDataSystem.currentTimeMillis,将子项添加到父项?为什么在此处使用计时器?它是否用于满足某些特定目的?它位于服务层,我每次都需要对数据库进行更新,我希望有一个延迟,看看是否在此间隔内得到新的更新,然后取消以前的任务,只调用一次数据库更新。