0

I have job with a bean injected to it. I've acheaved using this solution.

In this solution the job trigger is setted during configuration of SchedulerFactoryBean bean in the config class.

But I want to schedule it in my custom SchedulerService service by colling scheduleTrackRetry method.

I try something like this(see below) but the job is not fired up at the appropriate time.

@Service
public class SchedulerService {

  @Autowired
  SchedulerFactoryBean quartzScheduler;

  @Autowired
  JobDetailFactoryBean jobDetailFactoryBean;

  @Autowired
  CronTriggerFactoryBean cronTriggerFactoryBean;

  public void scheduleTrackRetry() {
    cronTriggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
    quartzScheduler.setTriggers(cronTriggerFactoryBean.getObject());
}

So, please tell me how could I acheave the desired behaviour?

Here is my job and conf classes:

@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;
  }
}
Community
  • 1
  • 1
Hutsul
  • 1,535
  • 4
  • 31
  • 51

1 Answers1

0

I found the solution here

I could not use SchedulerFactoryBean as a normal bean. When I try to inject it, spring inject Scheduler bean. Therefore my service should look like this:

@Service
public class SchedulerService {

  @Autowired
  Scheduler scheduler;

}
Community
  • 1
  • 1
Hutsul
  • 1,535
  • 4
  • 31
  • 51