设为首页 加入收藏

TOP

Quartz使用监听器插入定时任务执行日志(二)
2023-07-25 21:36:28 】 浏览:50
Tags:Quartz 时任务
} //保存执行记录 QuartzRunningLog runningLog = logThreadLocal.get(); runningLog.setJobName(jobName); runningLog.setCronExpression(cronExpression); runningLog.setEndTime(new Date()); runningLog.setStatus("SUCESS"); runningLog.setTaskId(Long.valueOf(jobKey.getName())); runningLog.setTaskName(taskName); logService.save(runningLog); logThreadLocal.remove(); } }

quartzconfig配置类



import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.spi.JobFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class QuartzConfig {

	@Autowired
	private ApplicationContext applicationContext;

	/**
	 * Create the job factory bean
	 *
	 * @return Job factory bean
	 */
	@Bean
	public JobFactory jobFactory() {
		ApplicationContextHolder jobFactory = new ApplicationContextHolder();
		jobFactory.setApplicationContext(applicationContext);
		return jobFactory;
	}

	/**
	 * Create the Scheduler Factory bean
	 *
	 * @return scheduler factory object
	 */
	@Bean
	public SchedulerFactoryBean schedulerFactory() {
		SchedulerFactoryBean factory = new SchedulerFactoryBean();
		factory.setAutoStartup(true);
		factory.setSchedulerName("Scheduler");
		factory.setOverwriteExistingJobs(true);
		factory.setJobFactory(jobFactory());
		return factory;
	}

	/**
	 * Create the Scheduler bean
	 *
	 * @param logService
	 * @return Scheduler
	 * @throws SchedulerException
	 */
	@Bean
	public Scheduler scheduler(@Autowired QuartzRunningLogService logService) throws SchedulerException {
        //在这里注入日志服务类且激活监听器,如果直接在监听器类里面使用@Autowired会出现注入为null
		schedulerFactory().getScheduler().getListenerManager().addJobListener(new QuartzJobListener(logService));
		return schedulerFactory().getScheduler();
	}

}

容器工具类


import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextHolder extends SpringBeanJobFactory
		implements ApplicationContextAware {
	private static ApplicationContext context;
	private transient AutowireCapableBeanFactory beanFactory;

	@Override
	public void setApplicationContext(final ApplicationContext context) {
		beanFactory = context.getAutowireCapableBeanFactory();
		ApplicationContextHolder.context = context;
	}

	@Override
	protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
		final Object job = super.createJobInstance(bundle);
		beanFactory.autowireBean(job);
		return job;
	}

	public static ApplicationContext getContext() {
		return context;
	}
}

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Spring FactoryBean接口 下一篇动态代理与责任链模式

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目