我有這個(gè)問題:我正在安排每 10 分鐘運(yùn)行一次的任務(wù)。當(dāng)它運(yùn)行時(shí),我預(yù)計(jì)會收到兩封來自兩次檢查失敗的電子郵件。但是,當(dāng)我編輯我的項(xiàng)目時(shí),開發(fā)工具會獲取更改并重新啟動 spring 應(yīng)用程序。但問題是:我仍然收到來自正在運(yùn)行的舊應(yīng)用程序的電子郵件。我該怎么做才能正確終止應(yīng)用程序?恐怕這個(gè)問題也會影響到生產(chǎn)環(huán)境。代碼:@Componentpublic class CheckRunner implements Runnable { private static final Logger log = LoggerFactory.getLogger(CheckRunner.class); private static boolean runOnce = true; private final long DEFAULT_CHECKING_INITIAL = 1L; private final long DEFAULT_CHECKING_PERIOD = 10L; @Autowired public EmailServiceImpl emailService; /** * Construct and schedule a CheckRunner */ public CheckRunner() { if(runOnce) { runOnce = false; log.info("Starting check runner."); ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); exec.scheduleAtFixedRate(this, DEFAULT_CHECKING_INITIAL , DEFAULT_CHECKING_PERIOD, TimeUnit.MINUTES); } } /** * Run the checks once */ @Override public void run() { log.info("Checking thread running."); checks(); } /** * Do the checks */ private void checks() { StringBuilder sb = new StringBuilder(); //sb.append("<pre>"); WebServerAvailablityCheck.runAllChecks(sb, emailService); //sb.append("</pre>"); }}
1 回答

DIEA
TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超3個(gè)贊
如果您在 bean 上實(shí)現(xiàn)java.io.Closeable
并CheckRunner
從其中調(diào)用shutdown()
executor,那么 Spring 將Closeable.close()
在 bean 被銷毀時(shí)調(diào)用。這將導(dǎo)致您的執(zhí)行程序線程優(yōu)雅地關(guān)閉。
添加回答
舉報(bào)
0/150
提交
取消