NutzCN Logo
问答 如何在web中弄一个常驻服务
发布于 3072天前 作者 LuoYY 1776 次浏览 复制 上一个帖子 下一个帖子
标签:

我想在系统中弄一个一直运行的服务,也就是开个线程一个跑某个东西,我想着在MainSetup中加了如下语句

 new Thread( ()->{ lfs.dosomething(); } ).start();

然后 lfs 是个接口的引用,尝试在MainSetup写注入,报错了,然后看到nutzbook上的是这么写Dao的,我也仿照写了一个,也出错了

Dao dao = ioc.get(Dao.class);
LogFileServices lfs = ioc.get(LogFileServices.class);
5 回复

实现类标注了@IocBean没? 具体报什么错误

来自炫酷的 NutzCN

@wendal
实现类标注了@IocBean

@IocBean
public class LogFileServicesImpl implements LogFileServices
七月 26, 2016 12:36:19 下午 org.apache.catalina.core.StandardContext filterStart
严重: Exception starting filter da
org.nutz.mvc.LoadingException: org.nutz.ioc.IocException: [logFileServices] # For object [logFileServices] - type:[interface pub.mysci.da.services.LogFileServices]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at org.nutz.lang.born.ConstructorBorning.born(ConstructorBorning.java:17)
	at org.nutz.lang.born.BornContext.doBorn(BornContext.java:60)
	at org.nutz.lang.Mirror.born(Mirror.java:990)
	at org.nutz.lang.Lang.wrapThrow(Lang.java:169)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:134)
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
	at org.nutz.mvc.NutFilter._init(NutFilter.java:87)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:65)
	at pub.mysci.da.mvc.DaNutFilter.init(DaNutFilter.java:22)
	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.<init>(ApplicationFilterConfig.java:105)
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4689)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5329)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
	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: org.nutz.ioc.IocException: [logFileServices] # For object [logFileServices] - type:[interface pub.mysci.da.services.LogFileServices]
	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 pub.mysci.da.MainSetup.init(MainSetup.java:26)
	at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:276)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:120)
	... 16 more
Caused by: org.nutz.ioc.ObjectLoadException: Object 'logFileServices' without define!
	at org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:166)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:181)
	... 21 more

七月 26, 2016 12:36:19 下午 org.apache.catalina.core.StandardContext startInternal
严重: One or more Filters failed to start. Full details will be found in the appropriate container log file
七月 26, 2016 12:36:19 下午 org.apache.catalina.core.StandardContext startInternal
严重: Context [/logDataAnalysis] startup failed due to previous errors
七月 26, 2016 12:36:19 下午 org.apache.catalina.core.ApplicationContext log
信息: Cleaning up Shiro Environment
七月 26, 2016 12:36:19 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesJdbc
警告: The web application [logDataAnalysis] registered the JDBC driver [com.alibaba.druid.proxy.DruidDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
七月 26, 2016 12:36:19 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesJdbc
警告: The web application [logDataAnalysis] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
七月 26, 2016 12:36:19 下午 org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
警告: The web application [logDataAnalysis] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 

改成这样

@IocBean(name="logFileServices")

来自炫酷的 NutzCN

@wendal 谢啦,问题解决了,另外问下有没有比较优雅的方式实现一个常驻服务,我现在用的是个循环,忙时直接循环,闲时就休眠一段时间再次询问。用定时任务的话,会出现如果第一次任务还没完成配置文件还没更新的时候第二次已经到时间启动了,读取到的配置相同就会和第一次任务做同样的事,造成浪费

@luoyy 那是不是做个静态变量就可以用计划任务了?

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