簡體   English   中英

"java-如何檢查調度程序是否工作"

[英]java- How to check whether scheduler is working

我正在使用 java SingleThreadScheduledExecutor 在 Web 應用程序中運行計划任務

我遇到的問題是 -如何確定調度程序是否仍在運行並且沒有崩潰?<\/strong>

有沒有更好的方法而不是讓另一個調度程序來檢查這個特定的調度程序

答案取決於您的調度程序的實際功能。 例如,您可以生成文件或更新db中的字段,或者可以檢查並計算時間間隔(從現在到最后更新)。 在您的情況下,如果文件創建或數據庫更新的時間間隔超過半小時,則意味着作業確實停止了。 但是請注意,預定的工作將像愛情一樣永遠持續下去。

實際上有一種方法可以檢查

public class TaskSchedulerService{
    private final ThreadPoolTaskScheduler taskScheduler; //initialize it here or in constructor 
    private Map<String,ScheduledFuture<?>> scheduleMap = new ConcurrentHashMap<>();

    public TaskSchedulerServiceImpl() {
        this.schedulerName = schedulerName;
        taskScheduler.initialize();
    }

    public boolean isScheduled(String taskId) {
        final ScheduledFuture<?> exits = scheduledTasks.get(taskId);
        return exits != null && exits.isDone();
    }

    public ScheduledFuture<?> schedule(String taskId, Runnable task, Date date) {

        ScheduledFuture<?> scheduled = scheduleMap.get(taskId);
        if (scheduled==null ) {
            ScheduledFuture<?> future = taskScheduler.schedule(task, date);
            scheduleMap.put(taskId, future);
            return future;
       
        } else {
         // log it is already scheduled
            return scheduled;
       }
    }

我知道為時已晚,但我希望其他人能從中受益

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM