[英]NullPointerException while deploying Quartz in Spring Boot
I am trying to use Quartz 2.2.1 with spring boot. 我正在尝试将Quartz 2.2.1与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. PS:我已经测试了我的控制器方法“ doPrintData”,它可以工作。 But when I put it inside the execute method im facing the javaNullPointerException .
但是,当我将其放入面向javaNullPointerException的execute方法中时。
Spring Boot manages it for you. Spring Boot为您管理它。 Remove the quartz dependency and just create a
Service
for having scheduled executions: 删除石英依赖项,仅创建具有计划执行的
Service
:
@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: 也可以看看:
You need to use SpringBeanJobFactory to create Job with Spring's autowired beans. 您需要使用SpringBeanJobFactory来使用Spring的自动装配的bean创建Job。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.