spring集合quartz获取所有定时任务的下次时间

原创
2019/03/31 22:41
阅读数 606
//需要继承 ApplicationContextAware
public class TaskDetail implements ApplicationContextAware{
	
	private Logger log = Logger.getLogger(TaskDetail.class);
	private static ApplicationContext context = null;
	
	public synchronized String taskMethod() {
        try {
            log.info("===获取所有定时任务信息开始===");
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Scheduler scheduler =(Scheduler) context.getBean("quartzScheduler");
        	//loop all group
        	for (String groupName : scheduler.getJobGroupNames()) {

        	//loop all jobs by groupname
        	for (String jobName : scheduler.getJobNames(groupName)) {

        	      //get job's trigger
        	  Trigger[] triggers = scheduler.getTriggersOfJob(jobName,groupName);
        	  Date nextFireTime = triggers[0].getNextFireTime();

        	  log.info("===[任务名称]:" + jobName + "[下次执行时间]:" + sdf.format(nextFireTime));
			  
			 /**STATE_BLOCKED 4 阻塞
                 STATE_COMPLETE 2 完成
                 TATE_ERROR 3 错误
                 STATE_NONE -1 不存在
                 STATE_NORMAL 0 正常
                STATE_PAUSED 1 暂停**/
              scheduler.getTriggerState(triggers[0].getName(), groupName);
        	}
        	}
        	  log.info("===获取所有定时任务信息结束===");
        } catch (Exception e) {
            log.error("获取定时任务信息出错", e);
        }
		return null;
	}

	@Override
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
      		this.context=arg0;
	}
	
}
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部