2
我使用下面的配置類來將Spring框架與Quartz集成,它工作正常,但是一旦應用程序啓動,作業就會動態觸發,因爲我使用@Configuration註釋,我想手動觸發作業由控制器和用戶界面,任何人都可以告訴我,我該怎麼做,請嗎?使用Spring控制器手動運行Quartz
這是石英配置類
@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;
}
}
石英工作
@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);
}
}
}