С помощью ScheduledExecutorService можно реализовать отложенные задачи (например, функцию отложенного выпуска)
Сначала определите локальные переменные в классе
ScheduledExecutorService service = Executors.newScheduledThreadPool(50);
Executors.newScheduledThreadPool(50); Здесь используется паттерн фабрики.
фабричный паттерн
В основном он предназначен для предоставления интерфейса перехода для создания объектов, чтобы оградить и изолировать конкретный процесс создания объектов и достичь цели повышения гибкости.
@PostMapping("/ops/scheduled/publish")
public ResponseResult scheduledPublish(@RequestBody ScheduleVideoDto dto) {
List<Integer> vids = dto.getVids();
if (vids.isEmpty()){
return ResponseResult.of().withErrorMessage("Failed to publish video, please select a video to publish");
}
Date pushTime = dto.getPushTime();
if (pushTime==null){
return ResponseResult.of().withErrorMessage("Failed to publish the video, please re-select the publishing time");
}
for (int i = 0; i< vids.size();i++){
int status = videoService.getStatusById(vids.get(i));
if (status==1) vids.remove(vids.get(i));
}
if (vids.isEmpty()){
return ResponseResult.of().withErrorMessage("Failed to publish video, the selected videos are all published");
}
long delay = pushTime.getTime() - System.currentTimeMillis();
vids.forEach(vid->{
videoService.updatePushTime(vid,pushTime);
service.schedule(() -> videoService.publish(vid), delay, TimeUnit.MILLISECONDS);
});
return ResponseResult.of();
}
Передача времени освобождения PushTime в dto, передаваемом в интерфейсе
long delay = pushTime.getTime() - System.currentTimeMillis();
#The release time minus the current time is the delay time delay
Вызов службы ScheduledExecutorService
public ScheduledFuture<?> schedule(Runnable command,
long delay, TimeUnit unit);
api метод
Он может реализовать функцию публикации видео в фиксированное время