2015-06-11 103 views
1

我有一個web服務,它使某個工作的動態調度。這項工作是擴展了石英工作界面石英和彈簧:動態調度

public class StartJob implements Job { 
    private String jobId; 
    private DAO dao; 

    public void execute(JobExecutionContext context) throws JobExecutionException { 

     JobDataMap dataMap = context.getMergedJobDataMap(); 

     //some logic here 
    } 

    // getters and setters 
} 

我也暴露出其接收一個的jobId cron表達式和日程安排新StartJob,將有接收到的ID的API的Java類。這裏是我的Spring配置

@Configuration 
@EnableTransactionManagement 
@EnableJpaRepositories(basePackages = { "persistence.dao" }) 
@ImportResource({"spring-quartz-context.xml"}) 
public class BeanConfig { 
    //wired from the xml 
    @Autowired JobDetailFactoryBean jobDetailFactory; 
    @Autowired CronTriggerFactoryBean cronTriggerFactory; 

    @Bean 
    public SchedulerFactoryBean schedulerFactoryBean() { 
     SchedulerFactoryBean bean = new SchedulerFactoryBean(); 
     bean.setApplicationContextSchedulerContextKey("applicationContext"); 
     bean.setSchedulerName("MyScheduler"); 
     Map<String, Object> schedulerContextAsMap = new HashMap<String, Object>(); 
     bean.setSchedulerContextAsMap(schedulerContextAsMap); 
     // quartzproperties not reported 
     bean.setQuartzProperties(quartzProperties()); 

     return bean; 
    } 

    //other bean definitions 

} 

和彈簧石英-config.xml中

<bean name="complexJobDetail" 
    class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> 
    <property name="jobClass" value="jobs.StartJob" /> 
    <property name="durability" value="true" /> 
</bean> 

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> 
    <property name="jobDetail" ref="complexJobDetail" /> 
    <property name="cronExpression" value="0/5 * * ? * SAT-SUN" /> 
</bean> 

我想要做的是一樣的東西(僞)

JobDetail job = jobDetailFactory.getObject(); 
job.setName(aGeneratedUUID); 
CronTrigger trigger = cronTriggerFactory.getObject(); 
trigger.setCronExpression(aDynamicCronExpression); 

scheduler.schedule(job, trigger); 

但問題是我無法更改作業名稱,所以,只有第一份工作是預定。這第一項工作使用JobDetailFactoryBean的名稱(complexJobDetail)作爲名稱。

我在這裏失蹤了什麼?這個配置是否正確?作業和觸發器是從工廠檢索同一類的新實例還是同一個類實例?

回答

0

問題是我沒有調用方法afterPropertiesSet()。所以

jobDetailFactory.setName(generatedUUID); 
jobDetailFactory.afterPropertiesSet(); 
JobDetail job = jobDetailFactory.getObject(); 

工作,創建的工作有了新的名字。