以下是定时器初始化的代码,使用的是ServletContextListener:
public class B2pCheckListener implements ServletContextListener { Timer timer = new Timer(); private static final Logger log = Logger.getLogger(B2pCheckListener.class); public void contextDestroyed(ServletContextEvent arg0) { System.out.println("系统定时任务初始化结束...."); timer.cancel(); } public void contextInitialized(ServletContextEvent arg0) { System.out.println("系统定时任务初始化开始...."); /*调用任务,延迟30秒,每15分钟执行一次*/ long delay = 30 * 1000; long intevalPeriod = 900 * 1000; timer.scheduleAtFixedRate(new B2pMsgCheck(), delay, intevalPeriod); Class c = getClass(); ClassLoader loader = c.getClassLoader(); } }
以下是定时任务的代码:
public class B2pMsgCheck extends TimerTask { private static final Logger log = Logger.getLogger(B2pMsgCheck.class); @Override public void run() { ……………… } }
@JFinal 请查看。
问题补充:我把定时任务去掉后,查看启动日志如下:
项目A:
Config的ClassLoader是sun.misc.Launcher$AppClassLoader@5e9e34
Starting JFinal 1.9
Starting scanner at interval of 5 seconds.
Starting web server on port: 80
2015-09-18 15:21:10
[INFO]-[Thread: main]-[org.eclipse.jetty.server.Server.doStart()]: jetty-8.1.8.v20121106
2015-09-18 15:21:10
[INFO]-[Thread: main]-[org.eclipse.jetty.webapp.StandardDescriptorProcessor.visitServlet()]: NO JSP Support for /bocp, did not find org.apache.jasper.servlet.JspServlet
2015-09-18 15:21:11
[INFO]-[Thread: main]-[org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized()]: started o.e.j.w.WebAppContext{/bocp,file:/D:/Eclipse/workspace_j2ee/bocp/src/webapp/}
configPlugin的ClassLoader是WebAppClassLoader=15964901@f39ae5
2015-09-18 15:21:11
[INFO]-[Thread: main]-[com.mchange.v2.log.MLog.<clinit>()]: MLog clients using log4j logging.
2015-09-18 15:21:11
[INFO]-[Thread: main]-[com.mchange.v2.c3p0.C3P0Registry.banner()]: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
2015-09-18 15:21:12
[INFO]-[Thread: main]-[org.eclipse.jetty.server.AbstractConnector.doStart()]: Started SelectChannelConnector@0.0.0.0:80
Starting Complete. Welcome To The JFinal World :)
2015-09-18 15:21:14
Change的ClassLoader是WebAppClassLoader=15964901@f39ae5
问题项目:
BocpTestConfig.class的ClassLoader是sun.misc.Launcher$AppClassLoader@10014f0
Starting JFinal 1.9
Starting scanner at interval of 5 seconds.
Starting web server on port: 80
2015-09-18 15:24:12
[INFO]-[Thread: main]-[org.eclipse.jetty.server.Server.doStart()]: jetty-8.1.8.v20121106
2015-09-18 15:24:12
[INFO]-[Thread: main]-[org.eclipse.jetty.webapp.StandardDescriptorProcessor.visitServlet()]: NO JSP Support for /bocpTest, did not find org.apache.jasper.servlet.JspServlet
2015-09-18 15:24:12
[INFO]-[Thread: main]-[org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized()]: started o.e.j.w.WebAppContext{/bocpTest,file:/D:/Eclipse/workspace_j2ee/bocpTest/webapp/}
configPlugin的ClassLoader是sun.misc.Launcher$AppClassLoader@10014f0
2015-09-18 15:24:13
[INFO]-[Thread: main]-[com.mchange.v2.log.MLog.<clinit>()]: MLog clients using log4j logging.
2015-09-18 15:24:13
[INFO]-[Thread: main]-[com.mchange.v2.c3p0.C3P0Registry.banner()]: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
2015-09-18 15:24:14
[INFO]-[Thread: main]-[org.eclipse.jetty.server.AbstractConnector.doStart()]: Started SelectChannelConnector@0.0.0.0:80
Starting Complete. Welcome To The JFinal World :)
2015-09-18 15:24:19
Change的ClassLoader是sun.misc.Launcher$AppClassLoader@10014f0
问题项目之所以定时任务无法正常获取Model对象,是因为它一直都是从WebAppClassLoader中寻找,但是JFinal的相关对象都在AppClassLoader,因此会报错,但是对于JFinal自身相关的运作是没有影响的,因为是在同一个ClassLoader。而另一个正常运行的项目,它的定时任务和JFinal相关对象都在WebAppClassLoader,所以都运行正常。
已经在上两个贴子中给出了解决方法:
1: http://www.oschina.net/question/1793377_2132461#AnchorAnswer1013995
2: http://www.oschina.net/question/1793377_251436
注意这句话: 我现在断定你的那个定时线程起动的时机过早,将这个启动放在YourJFinalConfig.afterJFinalStart()即可解决问题。
如果还是不知道如何解决,请看下面的载图:
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。