NutzCN Logo
精华 定期任务中,任务类中注入的dao跑起来的时候抛出空指针异常
发布于 3024天前 作者 LuoYY 2660 次浏览 复制 上一个帖子 下一个帖子
标签:

跟着nutzbook做了个定时任务
发现dao抛出空指针异常
```Java
@IocBean
public class GetZipFileInfoListJob implements Job {

private static final Log log = Logs.get();

@Inject protected Dao dao;

public void execute(JobExecutionContext context) throws JobExecutionException {
    System.out.println(dao.count(User.class));
}

}

判断下竟然为true

@IocBean
public class GetZipFileInfoListJob implements Job {

private static final Log log = Logs.get();

@Inject protected Dao dao;

public void execute(JobExecutionContext context) throws JobExecutionException {
    //System.out.println(dao.count(User.class));
    System.out.println(dao == null);
}

}
```

7 回复

这markdown怎么格式不对啊

那是因为你没使用nutz-integration-quartz, 里面有org.nutz.integration.quartz.NutQuartzJobFactory, 替换默认的JobFactory

Quartz默认的JobFactory只会单纯的new一个对象,不管注入的事.

解决方法:

  • 换nutz-integration-quartz
  • 或主动取一下
if (dao == null)
  dao = Mvcs.ctx().getDefaultIoc().get(Dao.class);

@wendal 我是按照 http://nutzbook.wendal.net/dev_prepare/add_quartz.html 上配置的,除此之外还要怎么操作才能用NutQuartzJobFactory,替换默认的JobFactory?

完全按那个操作了? 没有自己改东西了? 没有自己新建Scheduler??

贴quartz.properties和日志.

quartz.properties

org.quartz.scheduler.instanceName = NutzbookScheduler 
org.quartz.threadPool.threadCount = 3 
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
org.quartz.scheduler.skipUpdateCheck=true

console.log

2016-07-20 14:47:57,838 org.nutz.integration.quartz.NutQuartzCronJobFactory.init(NutQuartzCronJobFactory.java:45) DEBUG - job define name=pub.mysci.lyy.quartz.job.GetZipFileInfoListJob cron=0/10 * * * * ?
2016-07-20 14:47:57,849 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:140) INFO  - Nutz.Mvc[da] is up in 707ms
2016-07-20 14:47:57,850 org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:276) DEBUG - batch acquisition of 1 triggers
七月 20, 2016 2:47:57 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-apr-8080"]
七月 20, 2016 2:47:57 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-apr-8009"]
七月 20, 2016 2:47:57 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 2209 ms
2016-07-20 14:48:00,006 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'getZipFileInfoListJob'<class pub.mysci.lyy.quartz.job.GetZipFileInfoListJob>
2016-07-20 14:48:00,006 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:178) DEBUG - 	 >> Load definition name=getZipFileInfoListJob
2016-07-20 14:48:00,007 org.nutz.integration.quartz.NutQuartzJobFactory.newJob(NutQuartzJobFactory.java:30) WARN  - Not ioc bean? fallback to SimpleJobFactory
org.nutz.ioc.IocException: [getZipFileInfoListJob] # For object [getZipFileInfoListJob] - type:[class pub.mysci.lyy.quartz.job.GetZipFileInfoListJob]
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:217)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:235)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:146)
	at org.nutz.integration.quartz.NutQuartzJobFactory.newJob(NutQuartzJobFactory.java:27)
	at org.quartz.core.JobRunShell.initialize(JobRunShell.java:127)
	at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:375)
Caused by: org.nutz.ioc.ObjectLoadException: Object 'getZipFileInfoListJob' without define!
	at org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:166)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:181)
	... 5 more
2016-07-20 14:48:00,009 org.quartz.simpl.SimpleJobFactory.newJob(SimpleJobFactory.java:51) DEBUG - Producing instance of Job 'DEFAULT.pub.mysci.lyy.quartz.job.GetZipFileInfoListJob', class=pub.mysci.lyy.quartz.job.GetZipFileInfoListJob
2016-07-20 14:48:00,010 org.quartz.core.JobRunShell.run(JobRunShell.java:201) DEBUG - Calling execute on job DEFAULT.pub.mysci.lyy.quartz.job.GetZipFileInfoListJob
2016-07-20 14:48:00,010 org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:276) DEBUG - batch acquisition of 1 triggers
2016-07-20 14:48:00,011 org.quartz.core.JobRunShell.run(JobRunShell.java:211) ERROR - Job DEFAULT.pub.mysci.lyy.quartz.job.GetZipFileInfoListJob threw an unhandled Exception: 
java.lang.NullPointerException
	at pub.mysci.lyy.quartz.job.GetZipFileInfoListJob.execute(GetZipFileInfoListJob.java:26)
	at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
2016-07-20 14:48:00,011 org.quartz.core.ErrorLogger.schedulerError(QuartzScheduler.java:2425) ERROR - Job (DEFAULT.pub.mysci.lyy.quartz.job.GetZipFileInfoListJob threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.NullPointerException]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.NullPointerException
	at pub.mysci.lyy.quartz.job.GetZipFileInfoListJob.execute(GetZipFileInfoListJob.java:26)
	at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
	... 1 more

检查IocBy配置, 确保这个类所在的package在*anno覆盖的范围内

2016-07-20 14:48:00,007 org.nutz.integration.quartz.NutQuartzJobFactory.newJob(NutQuartzJobFactory.java:30) WARN  - Not ioc bean? fallback to SimpleJobFactory
org.nutz.ioc.IocException: [getZipFileInfoListJob] # For object [getZipFileInfoListJob] - type:[class pub.mysci.lyy.quartz.job.GetZipFileInfoListJob]
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:217)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:235)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:146)
	at org.nutz.integration.quartz.NutQuartzJobFactory.newJob(NutQuartzJobFactory.java:27)
	at org.quartz.core.JobRunShell.initialize(JobRunShell.java:127)
	at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:375)
Caused by: org.nutz.ioc.ObjectLoadException: Object 'getZipFileInfoListJob' without define!
	at org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:166)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:181)
	... 5 more

@wendal 非常感谢,在IocBy配置中找到了错误,args参数中的第四个参数导致job没在覆盖范围

添加回复
请先登陆
回到顶部