SpringBoot基於資料庫實現定時任務過程解析
阿新 • • 發佈:2020-01-07
這篇文章主要介紹了SpringBoot基於資料庫實現定時任務過程解析,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
在我們平時開發的專案中,定時任務基本屬於必不可少的功能,那大家都是怎麼做的呢?但我知道的大多都是靜態定時任務實現。
基於註解來建立定時任務非常簡單,只需幾行程式碼便可完成。實現如下:
@Configuration @EnableScheduling public class SimpleScheduleTask { //10秒鐘執行一次 @Scheduled(cron = "0/10 * * * * ?") private void tasks() { System.out.println("【定時任務】 每10秒執行一次!"); } }
Cron表示式引數分別表示(從左到右):
秒(0~59) 如0/5表示每5秒
分(0~59)
時(0~23)
日(0~31) 月的某一天
月(0~11)
周幾( 可填1-7 或 SUN/MON/TUE/WED/THU/FRI/SAT)
就上面幾行程式碼,就能搞定一個定時任務。顯然,使用Scheduled 確實特別的方便,但有很大的缺點和侷限,就是當我們調整了執行計劃的時間時,需要重啟服務才能生效,這就有些不方便。為了達到實時生效的效果,可以通過資料庫來動態實現定時任務。
基於資料庫的動態定時任務實現
將定時任務配置在資料庫,啟動專案的時候,用mybatis讀取資料庫,例項化物件,並設定定時任務。如果需要新增,減少,修改定時任務,僅需要修改資料庫資料,並重啟專案即可,無需改程式碼。
@Lazy(value = false) @Component public class ScheduleTask implements SchedulingConfigurer { protected static Logger logger = LoggerFactory.getLogger(ScheduleTask.class); private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Autowired private ScheduleTaskMapper scheduleTaskMapper; @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { List<ScheduleTask> tasks = getAllScheduleTasks(); logger.info("【定時任務啟動】 啟動任務數:"+tasks.size()+"; time="+sdf.format(new Date())); //校驗資料 checkDataList(tasks); //通過校驗的資料執行定時任務 int count = 0; if(tasks.size()>0) { for (int i = 0; i < tasks.size(); i++) { try { taskRegistrar.addTriggerTask(getRunnable(tasks.get(i)),getTrigger(tasks.get(i))); count++; } catch (Exception e) { logger.error("task start error:" + tasks.get(i).getClassName() + ";" + tasks.get(i).getMethodName() + ";" + e.getMessage()); } } } logger.info("started task number="+count+"; time="+sdf.format(new Date())); }; /** * 獲取要執行的所有任務 * @return */ private List<ScheduleTask> getAllScheduleTasks() { ScheduleTaskExample example=new ScheduleTaskExample(); example.createCriteria().andIsDeleteEqualTo((byte) 0); return scheduleTaskMapper.selectByExample(example); } /** * 獲取Runnable * * @param task * @return */ private Runnable getRunnable(ScheduleTask task){ return new Runnable() { @Override public void run() { try { Object obj = SpringUtil.getBean(task.getClassName()); Method method = obj.getClass().getMethod(task.getMethodName(),null); method.invoke(obj); } catch (InvocationTargetException e) { logger.error("refect exception:"+task.getClassName()+";"+task.getMethodName()+";"+ e.getMessage()); } catch (Exception e) { logger.error(e.getMessage()); } } }; } /** * 獲取Trigger * * @param task * @return */ private Trigger getTrigger(ScheduleTask task){ return new Trigger() { @Override public Date nextExecutionTime(TriggerContext triggerContext) { //將Cron 0/1 * * * * ? CronTrigger trigger = new CronTrigger(task.getCron()); Date nextExec = trigger.nextExecutionTime(triggerContext); return nextExec; } }; } /** * 校驗資料 * * @param list * @return */ private List<ScheduleTask> checkDataList(List<ScheduleTask> list) { String msg=""; for(int i=0;i<list.size();i++){ if(!checkOneData(list.get(i)).equalsIgnoreCase("ok")){ msg+=list.get(i).getTaskName()+";"; list.remove(list.get(i)); i--; }; } if(!StringUtils.IsEmpty(msg)){ msg="未啟動的任務:"+msg; logger.error(msg); } return list; } /** * 按每一條校驗資料 * * @param task * @return */ private String checkOneData(ScheduleTask task){ String result="ok"; Class cal= null; try { cal = Class.forName(task.getClassName()); Object obj =SpringUtil.getBean(cal); Method method = obj.getClass().getMethod(task.getMethodName(),null); String cron=task.getCron(); if(StringUtils.isBlank(cron)){ result="no found the cron:"+task.getTaskName(); logger.error(result); } } catch (ClassNotFoundException e) { result="not found the class:"+task.getClassName()+ e.getMessage(); logger.error(result); } catch (NoSuchMethodException e) { result="not found the method:"+task.getClassName()+";"+task.getMethodName()+";"+ e.getMessage(); logger.error(result); } catch (Exception e) { logger.error(e.getMessage()); } return result; } }
資料庫配置
執行的結果
這樣我們可以通過直接修改資料庫,執行週期就會改變,並且不需要我們重啟應用,十分方便。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。