Utilizing ScheduledExecutorService can implement timed duties (such because the perform of timed launch)
First outline native variables within the class
ScheduledExecutorService service = Executors.newScheduledThreadPool(50);
Executors.newScheduledThreadPool(50); Manufacturing facility sample is used right here.
manufacturing unit sample
It’s primarily to supply a transition interface for creating objects, in order to defend and isolate the precise course of of making objects and obtain the aim of bettering flexibility.
@PostMapping("/ops/scheduled/publish")
public ResponseResult scheduledPublish(@RequestBody ScheduleVideoDto dto) {
Record<Integer> vids = dto.getVids();
if (vids.isEmpty()){
return ResponseResult.of().withErrorMessage("Didn't publish video, please choose a video to publish");
}
Date pushTime = dto.getPushTime();
if (pushTime==null){
return ResponseResult.of().withErrorMessage("Didn't publish the video, please re-select the publishing time");
}
for (int i = 0; i< vids.dimension();i++){
int standing = videoService.getStatusById(vids.get(i));
if (standing==1) vids.take away(vids.get(i));
}
if (vids.isEmpty()){
return ResponseResult.of().withErrorMessage("Didn't publish video, the chosen movies are all printed");
}
lengthy delay = pushTime.getTime() - System.currentTimeMillis();
vids.forEach(vid->{
videoService.updatePushTime(vid,pushTime);
service.schedule(() -> videoService.publish(vid), delay, TimeUnit.MILLISECONDS);
});
return ResponseResult.of();
}
Move within the launch time PushTime within the dto handed within the interface
lengthy delay = pushTime.getTime() - System.currentTimeMillis();
#The launch time minus the present time is the delay time delay
Calling ScheduledExecutorService
public ScheduledFuture<?> schedule(Runnable command,
lengthy delay, TimeUnit unit);
api technique
It might understand the perform of publishing video at a set time