Run Quartz manually with Spring controller

3.5k views Asked by At

I use the following configuration class to integrate spring framework with Quartz,It works fine,but the job fires dynamically once the application has started because I use @Configuration annotation,I want to fire the job manually by controller and ui,Can anyone tell me how can I do that please?

this is quartz configuration class

@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;
}

}

Quartz job

@Service
@Transactional
public class HelloJob implements Job{

@Inject
TestrecordRepository testrecordRepository;
@Inject
ScoreRepository scoreRepository;


public void execute(JobExecutionContext context)
        throws JobExecutionException {
            System.out.println("Hello Quartz!");    
            List<Testrecord> records=testrecordRepository.findAll();
            for(Testrecord t:records){
                Testrecord testrecord = new Testrecord();
                testrecord.setValue_integer(t.getValue_integer());
                testrecord.setId(t.getId());
                RuleExecutor ruleExecutor = new RuleExecutor();
                Score score= ruleExecutor.processRules(testrecord);
                scoreRepository.save(score);    
            }

        }

}

1

There are 1 answers

0
dylaniato On

You just need a bean in which you inject the scheduler and the factories you already have. For example:

@Service
public class QuartzService {
    private Scheduler scheduler;
    private CronTriggerFactoryBean cronTriggerFactory;
    private JobDetailFactoryBean jobDetailFactory;

    public void createJob() {
        JobDetail job = jobDetailFactory.getObject();
        Trigger trigger = cronTriggerFactory.getObject();
        scheduler.scheduleJob(job, trigger);
    }

    // other methods: delete (unschedule), update (modify job), ...

    // getters and setters

}

Modify this class so that it suits your requirements. I use a class like this as a service in a web service, but it can be used in a web application or anything else.

it's important that this service is created by spring, meaning that it has to be defined in the spring configuration bean:

@Bean
public QuartzService quartzService() {
    QuartzService service = new QuartzService();
    service.setScheduler(quartzScheduler());
    service.setJobDetailFactoryBean(processMyJob());
    service.setCronTriggerFactoryBean(processMyJobTrigger());
    return service;
}