Tuesday, May 31, 2022
HomeWordPress DevelopmentUse ScheduledExecutorService to implement delayed duties — delayed video launch

Use ScheduledExecutorService to implement delayed duties — delayed video launch


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

Exit fullscreen mode

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
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 might understand the perform of publishing video at a set time

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments