SchedulerFactoryBean+AdaptableJobFactory+QuartzJobBean
package schedule.quartz5;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
//Spring与Quartz集成使用的是SchedulerFactoryBean这个类
@Configuration
public class QuartzConfig {
// @Autowired
// private TaskSchedulerFactory taskSchedulerFactory;
// @Bean
// public SchedulerFactoryBean schedulerFactoryBean() {
// SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
// schedulerFactoryBean.setJobFactory(taskSchedulerFactory);
// return schedulerFactoryBean;
// }
// 注入scheduler到spring,在quartzManege会用到
@Bean(name = "scheduler")
public Scheduler scheduler(TaskSchedulerFactory quartzJobFactory) throws Exception {
SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
factoryBean.setJobFactory(quartzJobFactory);
factoryBean.afterPropertiesSet();
Scheduler scheduler = factoryBean.getScheduler();
scheduler.start();
return scheduler;
}
}
package schedule.quartz5;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;
@Component("taskSchedulerFactory")
public class TaskSchedulerFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception
{
// 首先,调用父类的方法创建好Quartz所需的Job实例
Object jobInstance = super.createJobInstance(bundle);
// 然后,使用BeanFactory为创建好的Job实例进行属性自动装配并将其纳入到Spring容器的管理之中,属于Spring的技术范畴.
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
package schedule.quartz5;
import javax.annotation.Resource;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.quartz.impl.triggers.SimpleTriggerImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
@Component
public class ProcessJob extends QuartzJobBean {
@Resource
private SayService sayService;
private Logger logger = LoggerFactory.getLogger(ProcessJob.class);
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
try {
sayService.sayHello();
if(context.getTrigger() instanceof CronTriggerImpl){
CronTriggerImpl xxx = ((CronTriggerImpl)context.getTrigger());
logger.info("动态定时调度测试 job group:"+xxx.getJobGroup()+", job name:"+xxx.getJobName() +", trigger group:"+xxx.getGroup()+", trigger name:"+xxx.getName()+", cronExpression "+xxx.getCronExpression());
}else if(context.getTrigger() instanceof SimpleTriggerImpl){
SimpleTriggerImpl xxx = ((SimpleTriggerImpl)context.getTrigger());
logger.info("动态定时调度测试 job group:"+xxx.getJobGroup()+", job name:"+xxx.getJobName() +", trigger group:"+xxx.getGroup()+", trigger name:"+xxx.getName()+", cronExpression "+xxx);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
java.lang.NoSuchMethodError
org.springframework.util.ReflectionUtils.accessibleConstructor(Ljava/lang/Class;[Ljava/lang/Class;)Ljava/lang/reflect/Constructor;
一般是引入了两个重复的spring Jar包,如既引入了spring.framework 又引入了spring.core,去掉一个就好
用tree方式查看
博客地址:https://blog.csdn.net/xiang__liu,https://www.cnblogs.com/xiang--liu/