我有一个注入豆子的工作。我使用此solution进行了尝试。
在此解决方案中,在配置类中的SchedulerFactoryBean
bean配置期间设置作业触发器。
但我想通过colling SchedulerService
方法在我的自定义scheduleTrackRetry
服务中安排它。
我尝试这样的事情(见下文),但工作没有在适当的时候启动。
@Service
public class SchedulerService {
@Autowired
SchedulerFactoryBean quartzScheduler;
@Autowired
JobDetailFactoryBean jobDetailFactoryBean;
@Autowired
CronTriggerFactoryBean cronTriggerFactoryBean;
public void scheduleTrackRetry() {
cronTriggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
quartzScheduler.setTriggers(cronTriggerFactoryBean.getObject());
}
那么,请告诉我如何能够实现理想的行为?
这是我的工作和会议课程:
@Component
public class TrackRetryJob implements Job {
private static final Logger LOGGER = LoggerFactory.getLogger(TrackRetryJob.class);
@Autowired
private TimeformBatchService timeformBatchService;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
LOGGER.info("Run retry for fetching greyhound tracks");
timeformBatchService.consumeTracks();
}
}
@Configuration
public class QuartzConfig {
@Autowired
private ApplicationContext applicationContext;
@Bean
public SchedulerFactoryBean quartzScheduler() {
SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();
// custom job factory of spring with DI support for @Autowired!
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
quartzScheduler.setJobFactory(jobFactory);
quartzScheduler.setTriggers(getTrigger().getObject());
return quartzScheduler;
}
@Bean
public JobDetailFactoryBean retryTrackFetch() {
JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
jobDetailFactory.setJobClass(TrackRetryJob.class);
jobDetailFactory.setGroup("group1");
return jobDetailFactory;
}
@Bean
public CronTriggerFactoryBean getTrigger() {
CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
cronTriggerFactoryBean.setJobDetail(retryTrackFetch().getObject());
cronTriggerFactoryBean.setCronExpression("0 53 * * * ?");
cronTriggerFactoryBean.setGroup("group1");
return cronTriggerFactoryBean;
}
}
public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {
private transient AutowireCapableBeanFactory beanFactory;
@Override
public void setApplicationContext(final ApplicationContext context) {
beanFactory = context.getAutowireCapableBeanFactory();
}
@Override
protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
final Object job = super.createJobInstance(bundle);
beanFactory.autowireBean(job);
return job;
}
}
答案 0 :(得分:1)
我找到了解决方案here
我无法将SchedulerFactoryBean
用作普通bean。当我尝试注入它时,spring注入Scheduler
bean。因此我的服务应如下所示:
@Service
public class SchedulerService {
@Autowired
Scheduler scheduler;
}