1

I am trying to use Quartz 2.2.1 with spring boot. Im trying to declare a scheduled task that is supposed to write some datas into a file. My Job is defined like below :

public class JobTask implements Job {

@Autowired
JobController controller;

@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {

                try {
                    controller.doPrintData();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    }
}

And then :

public class StartJob {

  public static void main(final String[] args) {
    final SchedulerFactory factory = new StdSchedulerFactory();
    Scheduler scheduler;
    try {
        scheduler = factory.getScheduler();
        scheduler.start();
    } catch (SchedulerException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


    try {
      scheduler = factory.getScheduler();

      final JobDetailImpl jobDetail = new JobDetailImpl();
      jobDetail.setName("My job");
      jobDetail.setJobClass(JobTask.class);

      final SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl();
      simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 5000));
      //simpleTrigger.setStartTime(dateOf(12, 58, 00,06,05,2016));
      simpleTrigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
      simpleTrigger.setRepeatInterval(5000);
      simpleTrigger.setName("Trigger execution every 5 secondes");

      scheduler.start();
      scheduler.scheduleJob(jobDetail, simpleTrigger);

      System.in.read();
      if (scheduler != null) {
        scheduler.shutdown();
      }
    } catch (final SchedulerException e) {
      e.printStackTrace();
    } catch (final IOException e) {
      e.printStackTrace();
    }
  }
}

PS : I have tested my controller method 'doPrintData' and it works. But when I put it inside the execute method im facing the javaNullPointerException.

Daniel
  • 584
  • 3
  • 8
  • 20
  • 2
    All those calls to new means those beans are your responsibility, not Spring's. They are not under Spring's control, so you can't expect them to be injected. – duffymo May 06 '16 at 14:54
  • try { JobController controller = new JobController(); controller.doGetDataFromBank(); } catch (Exception e) { e.printStackTrace(); } I also tried this and delete the autowiring part, in vain – Daniel May 06 '16 at 15:08
  • Calling "new" means "not Spring's problem". You're still doing it wrong. – duffymo May 06 '16 at 15:10
  • So how can i make it work without calling new and autowired annotation ? – Daniel May 06 '16 at 15:14
  • 1
    Put that bean and all its dependencies under Spring's control. Can't be halfway about it. Either all new or all Spring. – duffymo May 06 '16 at 15:15

2 Answers2

4

Spring Boot manages it for you. Remove the quartz dependency and just create a Service for having scheduled executions:

@Service
public class JobScheduler{

    @Autowired
    JobController controller;

    //Executes each 5000 ms
    @Scheduled(fixedRate=5000)
    public void performJob() {
        controller.doPrintData();
    }
}

And enable the task scheduling for your application:

@SpringBootApplication
@EnableScheduling
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

See also:

Aritz
  • 30,971
  • 16
  • 136
  • 217
4

You need to use SpringBeanJobFactory to create Job with Spring's autowired beans.

class AutowiringSpringBeanJobFactory extends SpringBeanJobFactory implements ApplicationContextAware {
    private transient AutowireCapableBeanFactory beanFactory;

    public void setApplicationContext(final ApplicationContext context) {
        beanFactory = context.getAutowireCapableBeanFactory();
    }

    @Override
    public Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
       final Object job = super.createJobInstance(bundle);
       beanFactory.autowireBean(job);  //the magic is done here
       return job;
    }
}

And then when you do

    SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
    scheduler = schedFact.getScheduler();

    AutowiringSpringBeanJobFactory autowiringSpringBeanJobFactory = new AutowiringSpringBeanJobFactory();
    autowiringSpringBeanJobFactory.setApplicationContext(applicationContext);
    scheduler.setJobFactory(autowiringSpringBeanJobFactory);
max_dev
  • 554
  • 2
  • 6
  • 15