title: Quartz Scheduler not triggering the job when configured via Spring 4
body: <p>Ive being from sometime trying to setup a little program that uses Spring and Quartz together to schedule a task. I followed some other similar answers with no luck.
  At the moment I think I have all configured correctly, I see no more exceptions but my job looks like its not kicking off.</p>
  
  <p>In the log.out that Spring generates, I see the following messages at the end:</p>
  
  <blockquote>
    <p>2015-06-04T15:46:57.928 DEBUG
    [org.springframework.core.env.PropertySourcesPropertyResolver]
    Searching for key 'spring.liveBeansView.mbeanDomain' in
    [systemProperties] 2015-06-04T15:46:57.929 DEBUG
    [org.springframework.core.env.PropertySourcesPropertyResolver]
    Searching for key 'spring.liveBeansView.mbeanDomain' in
    [systemEnvironment] 2015-06-04T15:46:57.929 DEBUG
    [org.springframework.core.env.PropertySourcesPropertyResolver] Could
    not find key 'spring.liveBeansView.mbeanDomain' in any property
    source. Returning [null]</p>
  </blockquote>
  
  <p>I will show you my codes...</p>
  
  <p>This is the class from which I start the scheduler:</p>
  
  <pre><code>public class JobRunner {
  
      public static void main(String[] args) throws SchedulerException {
          ApplicationContext applicationContext = new AnnotationConfigApplicationContext(WhatsTheTimeConfiguration.class);
          AutowiringSpringBeanJobFactory autowiringSpringBeanJobFactory = new AutowiringSpringBeanJobFactory();
          autowiringSpringBeanJobFactory.setApplicationContext(applicationContext);
  
          SpringBeanJobFactory springBeanJobFactory = new SpringBeanJobFactory();
  
          SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
          schedulerFactoryBean.setTriggers(trigger());
          schedulerFactoryBean.setJobFactory(springBeanJobFactory);
          schedulerFactoryBean.start();
      }
  
      private static SimpleTrigger trigger() {
          return newTrigger()
                  .withIdentity("whatsTheTimeJobTrigger", "jobsGroup1")
                  .startNow()
                  .withSchedule(simpleSchedule()
                          .withIntervalInSeconds(1)
                          .repeatForever())
                  .build();
      }
  
  }
  </code></pre>
  
  <p>I want to mention that If I use the method schedulerFactoryBean.getScheduler().start(), it throws me a null pointer exception on the scheduler, so thats why im calling start() on the factory.</p>
  
  <p>The class AutowiringSpringBeanJobFactory was copy pasted from another answer here in stackoverflow. I decided to do that since all other answers where I found something was only configuration done via xml and I don't want to use xml.</p>
  
  <pre><code>public final class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements
          ApplicationContextAware {
  
      private transient AutowireCapableBeanFactory beanFactory;
  
      @Override
      public void setApplicationContext(final ApplicationContext context) {
          beanFactory = context.getAutowireCapableBeanFactory();
      }
  
      @Override
      protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
          final Object job = super.createJobInstance(bundle);
          beanFactory.autowireBean(job);
          return job;
      }
  }
  </code></pre>
  
  <p>This is the class that represents the Job that I want to trigger:</p>
  
  <pre><code>@Component
  public class WhatsTheTimeManager extends QuartzJobBean {
  
      @Autowired
      private WhatsTheTime usecase;
      @Autowired
      private LocationRetriever locationDataProvider;
  
      public WhatsTheTimeManager() {
      }
  
      @Override
      protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
          usecase.tellMeWhatsTheTimeIn(locationDataProvider.allLocations());
      }
  
      public void setUsecase(WhatsTheTime usecase) {
          this.usecase = usecase;
      }
  
      public void setLocationDataProvider(LocationRetriever locationDataProvider) {
          this.locationDataProvider = locationDataProvider;
      }
  }
  </code></pre>
  
  <p>My Spring configuration is doing component scanning, its very simple:</p>
  
  <pre><code>@Configuration
  @ComponentScan(basePackages = "com.springpractice")
  public class WhatsTheTimeConfiguration {
  
  }
  </code></pre>
  
  <p>From this point everything I have are just some interfaces, components and a domain object, but I will paste them also, just in case I forgot something:</p>
  
  <pre><code>public interface LocationRetriever {
      List&lt;String&gt; allLocations();
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public interface TimeOutputRenderer {
      TimeReport renderReport(String timeInLocation, String location);
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public interface TimeRetriever {
      String timeFor(String location);
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@Component
  public class LocationRetrieverDataProvider implements LocationRetriever{
  
      public LocationRetrieverDataProvider() {
      }
  
      @Override
      public List&lt;String&gt; allLocations() {
          return asList("Europe/London", "Europe/Madrid", "Europe/Moscow", "Asia/Tokyo", "Australia/Melbourne", "America/New_York");
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@Component
  public class TimeOutputRendererDataProvider implements TimeOutputRenderer {
  
      public TimeOutputRendererDataProvider() {
      }
  
      @Override
      public TimeReport renderReport(String location, String time) {
          System.out.println(location + " time is " + time);
          return new TimeReport(location, time);
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>@Component
  public class TimeRetrieverDataProvider implements TimeRetriever {
  
      public TimeRetrieverDataProvider() {
      }
  
      @Override
      public String timeFor(String location) {
          SimpleDateFormat timeInLocation = new SimpleDateFormat("dd-M-yyyy hh:mm:ss a");
          timeInLocation.setTimeZone(TimeZone.getTimeZone(location));
          return timeInLocation.format(new Date());
      }
  }
  </code></pre>
  
  <p>Just one last detail, that maybe is of interest. 
  The versions I am using in my libraries are the following:</p>
  
  <p>quartz 2.2.1</p>
  
  <p>spring 4.1.6.RELEASE</p>
  
  <p>When I run the appliaction, I expect the times of those countries to be printed every second, but it doesn't happen.</p>
  
  <p>If you want to clone the code and try for yourself and see, you can find it at this git repo(Feel free to fork if you want): <a href="https://github.com/SFRJ/cleanarchitecture" rel="nofollow">https://github.com/SFRJ/cleanarchitecture</a></p>