title: Spring 3 \ Quartz 2. Integration problems
body: <p>I have problems with integration spring and quartz. I need to dynamically added <code>CronTriggerFactoryBean</code> to <code>SchedulerFactoryBean</code>.</p>
  
  <p><strong>XML Spring mapping:</strong></p>
  
  
  
  <p></p>
  
  <pre><code>&lt;bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" scope="prototype"&gt;
      &lt;property name="jobDetails"&gt;
          &lt;list&gt;
              &lt;ref bean="plannedVacationServiceJob" /&gt;
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="triggers"&gt;
          &lt;list&gt;
              &lt;ref bean="plannedVacationServiceCronTrigger" /&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="plannedVacationServiceJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"&gt;
      &lt;property name="jobClass" value="com.my.service.package.PlannerJob" /&gt;
      &lt;property name="durability" value="true" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="plannedVacationServiceCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" scope="prototype"&gt;
      &lt;property name="jobDetail" ref="plannedVacationServiceJob" /&gt;
      &lt;property name="cronExpression" value="*/15 * * * * ?" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p><strong>Java code:</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>@Service
  public class Planner implements Planning { 
  @Autowired
  @Qualifier("plannedVacationServiceCronTrigger")
  private CronTriggerFactoryBean plannedVacationServiceCronTrigger;
  
  @Autowired
  private SchedulerFactoryBean schedulerFactoryBean;
  
   @PostConstruct
   public void init() {
         schedulerFactoryBean.start();
   } 
  //some code 
  private void addTask(PlannerEntity entity) {
      try {
          String name = getIdentityName(entity);
          JobKey jobKey = new JobKey(name);
          String cronExpression = getCronExpression(entity);
          plannedVacationServiceCronTrigger.setCronExpression(cronExpression);
          JobDetail jobDetail =      JobBuilder.newJob(PlannerJob.class).withIdentity(jobKey).build();
          plannedVacationServiceCronTrigger.setJobDetail(jobDetail);
          plannedVacationServiceCronTrigger.setName(name);
          plannedVacationServiceCronTrigger.afterPropertiesSet();
          schedulerFactoryBean.getScheduler().scheduleJob(jobDetail, plannedVacationServiceCronTrigger.getObject());
          triggers.put(entity.getId(), trigger);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  }
  </code></pre>
  
  <p>In this case we have only one trigger, which added in xml mapping</p>
  
  <p>I trying remove reference to triggers from xml and creating CronTriggerFactoryBean from code:</p>
  
  <pre class="lang-java prettyprint-override"><code>private void addTask(PlannerEntity entity) {
      try {
          String name = getIdentityName(entity);
          JobKey jobKey = new JobKey(name);
          String cronExpression = getCronExpression(entity);
          CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
          trigger.setCronExpression(cronExpression);
          JobDetail jobDetail = JobBuilder.newJob(PlannerJob.class).withIdentity(jobKey).build();
          trigger.setJobDetail(jobDetail);
          trigger.setName(name);
          trigger.afterPropertiesSet();
          schedulerFactoryBean.getScheduler().scheduleJob(jobDetail, trigger.getObject());
          triggers.put(entity.getId(), trigger);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>But in this case we dont have any triggers:</p>
  
  <pre><code>2014-04-02 14:28:55,144 DEBUG [org.quartz.core.QuartzSchedulerThread(org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread)] - batch acquisition of 0 triggers
  2014-04-02 14:29:18,981 DEBUG [org.quartz.core.QuartzSchedulerThread(org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread)] - batch acquisition of 0 triggers
  2014-04-02 14:29:46,361 DEBUG [org.quartz.core.QuartzSchedulerThread(org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread)] - batch acquisition of 0 triggers
  2014-04-02 14:30:09,439 DEBUG [org.quartz.core.QuartzSchedulerThread(org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread)] - batch acquisition of 0 triggers
  </code></pre>