簡體   English   中英

ScheduledExecutorService 只運行一次

[英]ScheduledExecutorService only runs once

我希望在啟動 Web 服務后運行一個進程,然后每 30 分鍾左右運行一次,(我現在正在以較小的延遲測試它,只是為了看看它是否有效),但我的進程從未運行過一次. 我究竟做錯了什么?

這是我的代碼:

@WebListener
public class SchedulerService implements ServletContextListener{

    @Autowired
    UpdateSubscriberService updateSubscriberService;

    ScheduledExecutorService scheduledExecService;

    public SchedulerService(){
        scheduledExecService = Executors.newSingleThreadScheduledExecutor();
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        scheduledExecService.shutdown();
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        scheduledExecService.scheduleWithFixedDelay(new Runnable(){
            @Override
            public void run() {
                Date date = new Date(System.currentTimeMillis());
                System.out.println("Running scheduled update check " + date.toString());
                updateSubscriberService.checkForUpdates();
            }
        }, 60, 30, TimeUnit.SECONDS);
    }
}

在一個類似的問題上看到我的這個更長的答案

try catch包裝run代碼

只是一個猜測:正在拋出異常。 如果ScheduledExecutorService遇到異常,則它會靜默停止,不再執行任何計划的工作。

run方法的代碼應該始終被 try-catch 包圍,以處理和吸收任何拋出的異常。

 @Override
 public void run() {
    try {  // Let no Exception reach the ScheduledExecutorService.
        Date date = new Date(System.currentTimeMillis());
        System.out.println("Running scheduled update check " + date.toString());
        updateSubscriberService.checkForUpdates();
    } catch ( Exception e ) {
        System.out.println( "ERROR - unexpected exception" );
    }
}

存根run方法

采取嬰兒的步驟。 從一個只run System.out.printlnrun方法開始。

以防萬一您處於代碼必須每隔幾秒運行一次的位置,即使最后一次運行尚未完成(如果管理不當,這可能非常危險),您可以在內部啟動您的流程計時器內的不同線程。 這是示例代碼。

ScheduledExecutorService scheduledExecService = newSingleThreadScheduledExecutor();
scheduledExecService.scheduleWithFixedDelay(new Runnable()
{
    @Override
    public void run()
    {
        // This should be in a try-catch because any error here
        // will stop the recurrence
        try
        {
            // The timer will only repeat if the last run is finished. So
            // we put each new process in a different thread than the timer
            // itself, so the last timer call "finishes" as soon as the process
            // leaves the timer's thread.
            Thread t = new Thread(new Runnable()
            {
                public void run()
                {
                    try
                    {
                        android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
                        MyProcessThatShouldRunEverySoManySecondsNoMatterWhat();
                    }
                    catch (Exception erTimerThread)
                    {
                        Log.e("Error", erTimerThread.toString());
                    }
                }
            });
            t.setPriority(2);
            t.start();
        }
        catch (Exception erTimer)
        {
            Log.e("Error", erTimer.toString());
        }
    }
}, 0, 60, java.util.concurrent.TimeUnit.SECONDS);

暫無
暫無

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

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