2011-01-27 77 views
4

我正在使用Spring石英调度程序,但我没有使用XML文件。我想以编程方式创建整个配置。如何以编程方式创建触发器对象?

我写了下面的代码。

package com.eaportal.service.impl; 

import java.text.ParseException; 
import java.util.Date; 
import java.util.HashMap; 
import java.util.Map; 

import org.quartz.JobDetail; 
import org.springframework.scheduling.SchedulingException; 
import org.springframework.scheduling.quartz.CronTriggerBean; 
import org.springframework.scheduling.quartz.JobDetailBean; 
import org.springframework.scheduling.quartz.SchedulerFactoryBean; 

import com.eaportal.service.intfc.AuctionWinnerService; 

public class NormalAuctionWinnerServiceImpl1 implements AuctionWinnerService { 

    @SuppressWarnings("deprecation") 
    public void declareWinner(int auctionId, Map<String, Object> parameterMap) { 
     System.out.println("INSIDE DECLARE WINNER METHOD."); 
     /** STEP 1 : INSTANTIATE TASK CLASS **/ 
     NormalAuctionWinnerTask1 runMeTask = new NormalAuctionWinnerTask1(); 
     System.out.println("FINISHED STEP 1"); 

     /** STEP 2 : INSTANTIATE JOB DETAIL CLASS AND SET ITS PROPERTIES **/ 
     Map<String,Object> jobDataAsMap = new HashMap<String,Object>(); 
     jobDataAsMap.put("runMeTask",runMeTask); 
     JobDetailBean jdb = new JobDetailBean(); 
     jdb.setJobClass(NormalAuctionWinnerTask1.class); 
     jdb.setJobDataAsMap(jobDataAsMap); 
     System.out.println("FINISHED STEP 2"); 

     /** STEP 3 : INSTANTIATE CRON TRIGGER AND SET ITS PROPERTIES **/ 
     CronTriggerBean ctb = new CronTriggerBean(); 
     Date d1 = new Date(); 
     Date d2 = new Date(); 
     d2.setMinutes(d1.getMinutes()+10); 
     ctb.setStartTime(d1); 
     ctb.setEndTime(d2); 
     ctb.setJobDetail(jdb); 

     try { 
      ctb.setCronExpression("59 * * * * ? *"); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

     /** STEP 4 : INSTANTIATE SCHEDULER FACTORY BEAN AND SET ITS PROPERTIES **/ 
     SchedulerFactoryBean sfb = new SchedulerFactoryBean(); 
     sfb.setJobDetails(new JobDetail[]{jdb}); 
     try { 
      sfb.start(); 
     } catch (SchedulingException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

代码正在工作,除了触发器没有触发因为我没有设置它。

这里的问题是在XML配置中我们有schedulerFactoryBean的'triggers'属性,我们用list来配置我们的触发器。

但我不能以编程方式设置相同的属性。 SchedulerFactoryBean中有一个setTriggers方法,它接受一个Trigger 的数组,但是如何创建它就是问题所在。

我对它的最后4小时仍然没有成功的迹象。

有人可以帮我吗?

感谢

+0

这看起来有点像你的其他问题:http://stackoverflow.com/questions/4794560/quartz-integration-with-spring/ – Ralph 2011-01-27 15:08:11

回答

-1

主要的问题应该是,你需要安排工作:

scheduler.scheduleJob(jobDetail, trigger); 

而且我也不知道是怎么回事春季石英硅豆类,但正常的石英硅作业和触发器必须有一个名字和一个小组!并且可能需要启动调度程序:scheduler.start();

我已经修改了你的代码位,它的作品没有弹簧,以及所有省去了不需要的东西,向人们展示它是如何工作的:

封装测试;

import java.text.ParseException; 
import java.util.Date; 

import org.quartz.CronTrigger; 
import org.quartz.Job; 
import org.quartz.JobDetail; 
import org.quartz.JobExecutionContext; 
import org.quartz.JobExecutionException; 
import org.quartz.Scheduler; 
import org.quartz.SchedulerException; 
import org.quartz.SchedulerFactory; 
import org.quartz.impl.StdSchedulerFactory; 


public class Demo { 

    public static class TestJob implements Job{ 
     @Override 
     public void execute(JobExecutionContext arg0) throws JobExecutionException { 
      System.out.println("execute");   
     } 
    } 

    public static void declareWinner() throws SchedulerException, ParseException { 

     JobDetail jobDetail = new JobDetail("job","group",Demo.TestJob.class); 

     CronTrigger trigger = new CronTrigger("trigger","group"); 

     trigger.setStartTime(new Date()); 
     trigger.setEndTime(new Date(new Date().getTime() + 10 * 60 * 1000)); 
     trigger.setCronExpression("* * * * * ? *");  

     /** STEP 4 : INSTANTIATE SCHEDULER FACTORY BEAN AND SET ITS PROPERTIES **/ 
     SchedulerFactory sfb = new StdSchedulerFactory(); 
     Scheduler scheduler = sfb.getScheduler();  

     scheduler.scheduleJob(jobDetail, trigger);  

     scheduler.start();     
    } 

    public static void main (String[] args) throws SchedulerException, Exception { 
     declareWinner(); 
     Thread.sleep(10000); 
    } 

} 
8

我能够用Spring Scheduling Framework成功地做到这一点。

我知道这是一个很老的帖子,但由于这个主题的内容非常稀少,所以把它放在这里应该是一个更好的主意。

第一篇文章的代码中的主要问题是afterPropertiesSet()尚未在JobDetail对象以及CronTrigger对象上被调用。在准备运行cron之前,afterProperties函数会对输入的值进行一些处理。

此外,我使用MethodInvokingJobDetailFactoryBean而不是常规的jobDetail对象,因为它给了我在给定类中由cron调用的函数更多的灵活性。

这里是我的代码:

package test.spring; 

import org.quartz.JobDetail; 
import org.quartz.Trigger; 
import org.springframework.context.support.GenericApplicationContext; 
import org.springframework.scheduling.SchedulingException; 
import org.springframework.scheduling.quartz.CronTriggerBean; 
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean; 
import org.springframework.scheduling.quartz.SchedulerFactoryBean; 

import java.text.ParseException; 

public class ProgrammaticCron { 

    public static void callWorkFlow() { 
     System.out.println("Abhishek Jain"); 
    } 

    public static void main (String[] args) { 
     try { 
      GenericApplicationContext applicationContext = new GenericApplicationContext(); 

      MethodInvokingJobDetailFactoryBean jdfb = new MethodInvokingJobDetailFactoryBean(); 
      jdfb.setTargetClass(ProgrammaticCron.class); 
      jdfb.setTargetMethod("callWorkFlow"); 
      jdfb.setName("Trial program"); 
      jdfb.afterPropertiesSet(); 
      JobDetail jd = (JobDetail)jdfb.getObject(); 

      CronTriggerBean ctb = new CronTriggerBean(); 
      ctb.setJobDetail(jd); 
      ctb.setName("Daily cron"); 
      ctb.setJobName(jd.getName()); 
      try { 
       ctb.setCronExpression("59 * * * * ? *"); 
      } catch (ParseException e) { 
       e.printStackTrace(); 
      } 

      ctb.afterPropertiesSet(); 

      SchedulerFactoryBean sfb = new SchedulerFactoryBean(); 
      sfb.setJobDetails(new JobDetail[]{(JobDetail)jdfb.getObject()}); 
      sfb.setTriggers(new Trigger[]{ctb}); 
      sfb.afterPropertiesSet(); 
      try { 
       sfb.start(); 
      } catch (SchedulingException e) { 
       e.printStackTrace(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

afterProperties()是非常重要的,它可以从afterProperties实施SchedulerFactoryBean这是如下理解:

//--------------------------------------------------------------------- 
// Implementation of InitializingBean interface 
//--------------------------------------------------------------------- 

public void afterPropertiesSet() throws Exception { 
    if (this.dataSource == null && this.nonTransactionalDataSource != null) { 
     this.dataSource = this.nonTransactionalDataSource; 
    } 

    if (this.applicationContext != null && this.resourceLoader == null) { 
     this.resourceLoader = this.applicationContext; 
    } 

    // Create SchedulerFactory instance. 
    SchedulerFactory schedulerFactory = (SchedulerFactory) 
      BeanUtils.instantiateClass(this.schedulerFactoryClass); 

    initSchedulerFactory(schedulerFactory); 

    if (this.resourceLoader != null) { 
     // Make given ResourceLoader available for SchedulerFactory configuration. 
     configTimeResourceLoaderHolder.set(this.resourceLoader); 
    } 
    if (this.taskExecutor != null) { 
     // Make given TaskExecutor available for SchedulerFactory configuration. 
     configTimeTaskExecutorHolder.set(this.taskExecutor); 
    } 
    if (this.dataSource != null) { 
     // Make given DataSource available for SchedulerFactory configuration. 
     configTimeDataSourceHolder.set(this.dataSource); 
    } 
    if (this.nonTransactionalDataSource != null) { 
     // Make given non-transactional DataSource available for SchedulerFactory configuration. 
     configTimeNonTransactionalDataSourceHolder.set(this.nonTransactionalDataSource); 
    } 


    // Get Scheduler instance from SchedulerFactory. 
    try { 
     this.scheduler = createScheduler(schedulerFactory, this.schedulerName); 
     populateSchedulerContext(); 

     if (!this.jobFactorySet && !(this.scheduler instanceof RemoteScheduler)) { 
      // Use AdaptableJobFactory as default for a local Scheduler, unless when 
      // explicitly given a null value through the "jobFactory" bean property. 
      this.jobFactory = new AdaptableJobFactory(); 
     } 
     if (this.jobFactory != null) { 
      if (this.jobFactory instanceof SchedulerContextAware) { 
       ((SchedulerContextAware) this.jobFactory).setSchedulerContext(this.scheduler.getContext()); 
      } 
      this.scheduler.setJobFactory(this.jobFactory); 
     } 
    } 

    finally { 
     if (this.resourceLoader != null) { 
      configTimeResourceLoaderHolder.remove(); 
     } 
     if (this.taskExecutor != null) { 
      configTimeTaskExecutorHolder.remove(); 
     } 
     if (this.dataSource != null) { 
      configTimeDataSourceHolder.remove(); 
     } 
     if (this.nonTransactionalDataSource != null) { 
      configTimeNonTransactionalDataSourceHolder.remove(); 
     } 
    } 

    registerListeners(); 
    registerJobsAndTriggers(); 
     } 

正如你可能会注意到,所有这些任务因为获取调度程序和使用触发器注册作业是作为此功能的一部分完成的。

相关问题