Quartz:ERROR threw an unhandled Exception

详细的错误信息如下:

 1 2016-06-28 17:18:13.366 [DefaultQuartzScheduler_Worker-1] ERROR org.quartz.core.JobRunShell:211 - Job group1.job1 threw an unhandled Exception: 
 2 java.lang.NullPointerException
 3     at com.starunion.java.service.timer.JobEndConference.execute(JobEndConference.java:45) ~[bin/:?]
 4     at org.quartz.core.JobRunShell.run(JobRunShell.java:202) [quartz-2.2.1.jar:?]
 5     at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.2.1.jar:?]
 6 2016-06-28 17:18:13.374 [DefaultQuartzScheduler_Worker-1] ERROR org.quartz.core.ErrorLogger:2425 - Job (group1.job1 threw an exception.
 7 org.quartz.SchedulerException: Job threw an unhandled exception.
 8     at org.quartz.core.JobRunShell.run(JobRunShell.java:213) [quartz-2.2.1.jar:?]
 9     at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.2.1.jar:?]
10 Caused by: java.lang.NullPointerException
11     at com.starunion.java.service.timer.JobEndConference.execute(JobEndConference.java:45) ~[bin/:?]
12     at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[quartz-2.2.1.jar:?]

说说我的解决过程:

一、原因很明显:调用了null对象。

根据日志信息,定位到我的Job对象类的指定行,下图的21行:

 1 @Component
 2 public class JobEndConference implements Job {
 3 
 4     @Autowired
 5     CallableFsExecCmdProc procExecTask;
 6 
 7     @Override
 8     public void execute(JobExecutionContext JEContext) throws JobExecutionException {
 9 
10         JobDataMap map = JEContext.getJobDetail().getJobDataMap();
11 
12         String meetName = (String) map.get("meetName");
13 
14         StringBuffer buff = new StringBuffer();
15         buff.append("bgapi conference ");
16         buff.append(meetName);
17         buff.append(" kick all");
18         // buff.append(ConstantUtil.FS_CMD_TAIL);
19         20         // SocketFsTcp4SendCMD.fsSendCommand(buff.toString());
21         procExecTask.setSendCmd(buff.toString());
22         Future<Integer> future = StarProxy.executor.submit(procExecTask);
23         try {
24             future.get(5000, TimeUnit.MILLISECONDS);
25         } catch (InterruptedException | ExecutionException | TimeoutException e) {
26             e.printStackTrace();
27         }
28 
29     }
30 
31 }

这个对象为空,也就意味着没有通过Spring注解正确的初始化。

确定这个注解的写法是正确的,其他所有的类都是这么写的。

二、那问题在哪里呢?

往上查,看看对这个类的调用情况。

1 ......
2 JobDetail jobDetail = newJob(JobEndConference.class).withIdentity("job1", "group1").setJobData(dm).build();
3 ......

原因出来了,newJob传入的对象参数并非Spring注解加载的那个对象。

三、解决办法:

方法1. 从Spring的ApplicationContext获取JobEndConference对象。

方法2. 取消JobEndConference的本身和参数的注解,用new初始化。

原文地址:https://www.cnblogs.com/yoyotl/p/5624268.html