Fire Quartz手动通过Spring控制器

时间:2015-06-12 09:26:23

标签: java spring spring-mvc quartz-scheduler

我使用以下配置类将spring框架与Quartz集成,它工作正常,但是一旦应用程序启动,作业就会动态触发,因为我使用@Configuration注释,我想通过控制器和ui手动触发作业。 如何通过Spring控制器手动触发石英?

Quartz配置类

[SomeTypeID]

}

Quartz job

@Configuration
public class QuartzConfig {

@Autowired
private PlatformTransactionManager transactionManager;

@Autowired
private ApplicationContext applicationContext;

@Bean
public SchedulerFactoryBean quartzScheduler() {
    SchedulerFactoryBean quartzScheduler = new SchedulerFactoryBean();

    quartzScheduler.setTransactionManager(transactionManager);
    quartzScheduler.setOverwriteExistingJobs(true);

    AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
    jobFactory.setApplicationContext(applicationContext);
    quartzScheduler.setJobFactory(jobFactory);

    Trigger[] triggers = {
            processMyJobTrigger().getObject()
    };

    quartzScheduler.setTriggers(triggers);

    return quartzScheduler;
}

@Bean
public JobDetailFactoryBean processMyJob() {
    JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
    jobDetailFactory.setJobClass(HelloJob.class);
    jobDetailFactory.setDurability(true);
    return jobDetailFactory;
}

@Bean
public CronTriggerFactoryBean processMyJobTrigger() {
    CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
    cronTriggerFactoryBean.setJobDetail(processMyJob().getObject());
    cronTriggerFactoryBean.setCronExpression("0 0/1 * * * ?");
    return cronTriggerFactoryBean;
}

}

1 个答案:

答案 0 :(得分:0)

您需要从控制器访问SchedulerFactoryBean,然后选择scheduler并触发您喜欢的工作。

Scheduler scheduler = (Scheduler)   getApplicationContext().getBean("schedulerFactoryBean");

scheduler.triggerJob(JobKey jobKey)