您好,登錄后才能下訂單哦!
Spring中添加計(jì)時(shí)器的時(shí)候根據(jù)業(yè)務(wù)需求可能會需要?jiǎng)討B(tài)處理觸發(fā)時(shí)間;
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.Trigger; import org.springframework.scheduling.TriggerContext; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.support.CronTrigger; import org.springframework.stereotype.Component; import java.util.Date; @Component @EnableScheduling public class SchedulerTest implements SchedulingConfigurer { private final static Logger logger = LoggerFactory.getLogger(SchedulerTest.class); public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { // 注冊計(jì)時(shí)任務(wù)到Scheduling接口 scheduledTaskRegistrar.addTriggerTask(new Runnable() { public void run() { logger.info("task job is running..."); } }, new Trigger() { public Date nextExecutionTime(TriggerContext triggerContext) { CronTrigger cronTrigger = new CronTrigger("時(shí)間表達(dá)式"); Date nextExec = cronTrigger.nextExecutionTime(triggerContext); return nextExec; } }); } }
PS:多個(gè)計(jì)時(shí)任務(wù)添加的時(shí)候只用都注冊到Spring中即可
總結(jié)
以上所述是小編給大家介紹的Spring動態(tài)配置計(jì)時(shí)器觸發(fā)時(shí)間的實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。