NutzCN Logo
问答 定时任务报错:FAIL to create Ioc Bean name=[scheduler]
发布于 2717天前 作者 syd4440 3092 次浏览 复制 上一个帖子 下一个帖子
标签:

想在nutz中执行定时任务,
就参照nutzbook中的【为正式开发做准备】的【加入Quartz】一步步操作方法:
加入的jar包是
quartz-2.2.3
nutz-integration-quartz-1.r.59

接下来就是添加修改
conf/quartz.properties

MainModule,修改IocBy

MainSetup类, 添加代码

运行后报错如下:
2017-06-08 15:50:52.473 DEBUG [localhost-startStop-1] org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(388) : SELECT COUNT(*) FROM tb_user
2017-06-08 15:50:52.504 DEBUG [localhost-startStop-1] org.nutz.ioc.impl.NutIoc.get(151) : Get 'nutQuartzCronJobFactory'<class org.nutz.integration.quartz.NutQuartzCronJobFactory>
2017-06-08 15:50:52.504 DEBUG [localhost-startStop-1] org.nutz.ioc.impl.NutIoc.get(177) : >> Load definition name=nutQuartzCronJobFactory
2017-06-08 15:50:52.504 DEBUG [localhost-startStop-1] org.nutz.ioc.loader.map.MapLoader.load(67) : Loading define for name=nutQuartzCronJobFactory
2017-06-08 15:50:52.504 DEBUG [localhost-startStop-1] org.nutz.ioc.loader.combo.ComboIocLoader.load(169) : Found IocObject(nutQuartzCronJobFactory) in QuartzIocLoader@19864874
2017-06-08 15:50:52.504 DEBUG [localhost-startStop-1] org.nutz.ioc.impl.NutIoc.get(209) : >> Make...'nutQuartzCronJobFactory'<class org.nutz.integration.quartz.NutQuartzCronJobFactory>
2017-06-08 15:50:52.504 DEBUG [localhost-startStop-1] org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(70) : Load class org.nutz.integration.quartz.NutQuartzCronJobFactory without AOP
2017-06-08 15:50:52.504 DEBUG [localhost-startStop-1] org.nutz.ioc.impl.ScopeContext.save(64) : Save object 'nutQuartzCronJobFactory' to [app]
2017-06-08 15:50:52.504 DEBUG [localhost-startStop-1] org.nutz.ioc.impl.NutIoc.get(151) : Get 'scheduler'<>
2017-06-08 15:50:52.504 DEBUG [localhost-startStop-1] org.nutz.ioc.impl.NutIoc.get(177) : >> Load definition name=scheduler
2017-06-08 15:50:52.504 DEBUG [localhost-startStop-1] org.nutz.ioc.loader.map.MapLoader.load(67) : Loading define for name=scheduler
2017-06-08 15:50:52.504 DEBUG [localhost-startStop-1] org.nutz.ioc.loader.combo.ComboIocLoader.load(169) : Found IocObject(scheduler) in QuartzIocLoader@19864874
2017-06-08 15:50:52.504 DEBUG [localhost-startStop-1] org.nutz.ioc.impl.NutIoc.get(209) : >> Make...'scheduler'<>
2017-06-08 15:50:52.504 DEBUG [localhost-startStop-1] org.nutz.ioc.impl.ScopeContext.save(64) : Save object 'scheduler' to [app]
2017-06-08 15:50:52.520 DEBUG [localhost-startStop-1] org.nutz.ioc.impl.ScopeContext.remove(84) : Remove object 'scheduler' from [app]
2017-06-08 15:50:52.520 DEBUG [localhost-startStop-1] org.nutz.ioc.impl.ScopeContext.remove(84) : Remove object 'nutQuartzCronJobFactory' from [app]
2017-06-08 15:50:52.520 ERROR [localhost-startStop-1] org.nutz.mvc.impl.NutLoading.load(125) : Error happend during start serivce!
org.nutz.ioc.IocException: [nutQuartzCronJobFactory, scheduler] # FAIL to create Ioc Bean name=[scheduler]
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
at org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:31)
at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:146)
at com.mgsoft.onekey.MainSetup.init(MainSetup.java:39)
at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:277)
at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:121)
at org.nutz.mvc.ActionHandler.(ActionHandler.java:19)
at org.nutz.mvc.NutFilter._init(NutFilter.java:87)
at org.nutz.mvc.NutFilter.init(NutFilter.java:65)
at com.mgsoft.onekey.mvc.OneKeyNutFilter.init(OneKeyNutFilter.java:25)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260)
at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:105)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4700)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5340)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: org/slf4j/Logger
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at org.nutz.lang.Mirror.findMethod(Mirror.java:1133)
at org.nutz.lang.Mirror.findMethod(Mirror.java:1114)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:101)
... 27 more
Caused by: java.lang.ClassNotFoundException: org.slf4j.Logger
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
... 35 more

12 回复

缺slf4j-api.jar 和 slf4j-log4j12.jar

谢谢,想不到为了个定时任务,需要加入4个jar包

如果你只需要简单的定时任务,可以考虑Tasks类

来自炫酷的 NutzCN

想要做到是:每月1号0点执行一次任务。
刚才试了下,没找到tasks类的例子。
能不能举个简单的tasks类的例子?

看Tasks类的javadoc

来自炫酷的 NutzCN

不过,既然都配好quartz了,那就用吧😄

来自炫酷的 NutzCN

得,简单的功能需要加4个jar包,估计会被项目经理骂死,还是研究下tasks类

再问下,这个思路有没问题?
在MainSetup的init方法末尾加入
Tasks.scheduleAtFixedRate(new Runnable(){
public void run() {
//检查是否是1号,是就执行。。。
}
}, "2017-06-09 00:00:00", 1, TimeUnit.DAYS); //每天检查1次

恩, 没啥问题

出现了个新的问题:
在定时任务中有执行数据库操作,
在类MainSetup中 已使用
@Inject
protected Dao dao;
在 public void run() {
dao.execute(sql); //报错:Cannot refer to the non-final local variable dao defined in an enclosing scope
}
在这种情况下,定时任务中如何进行数据库操作?

谢谢,惭愧,文档也看过,但没看得这么仔细。
本来还准备用jdbc直接操作的。。。。

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