Use ScheduledExecutorService to implement delayed tasks — delayed video release

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);
Enter fullscreen mode

Exit fullscreen mode

Executors.newScheduledThreadPool(50); Manufacturing facility sample is used right here.

manufacturing facility 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 enhancing flexibility.

@PostMapping("/ops/scheduled/publish")
    public ResponseResult scheduledPublish(@RequestBody ScheduleVideoDto dto) {
        Listing<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.measurement();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 revealed");
        }
        lengthy delay = pushTime.getTime() - System.currentTimeMillis();
        vids.forEach(vid->{
            videoService.updatePushTime(vid,pushTime);
            service.schedule(() -> videoService.publish(vid), delay, TimeUnit.MILLISECONDS);
        });
        return ResponseResult.of();
    }
Enter fullscreen mode

Exit fullscreen mode

Go 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
Enter fullscreen mode

Exit fullscreen mode

Calling ScheduledExecutorService

public ScheduledFuture<?> schedule(Runnable command,
                                   lengthy delay, TimeUnit unit);
Enter fullscreen mode

Exit fullscreen mode

api technique

It could actually understand the perform of publishing video at a set time

Add a Comment

Your email address will not be published. Required fields are marked *