2015-06-11 44 views
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);  
      } 

     } 

}

回答

0

你只需要在其中注入調度和工廠你一個bean已經有。例如:

@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 

} 

修改此類以便它適合您的要求。我在Web服務中使用這樣的類作爲服務,但它可以用於Web應用程序或其他任何應用程序中。

,重要的是這種服務是由彈簧產生,這意味着它在Spring配置的bean定義:

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