[英]Spring Bean running in its own thread
在我的Web应用程序中,我尝试使用Java SDK7 WatchService创建目录轮询bean。 我想要实现的是在自己的线程中运行此bean,以便它不会阻止应用程序。 就像是:
<bean id="directoryPoller" class="org...MyDirectoryPoller" scope="thread"/>
我担心你必须用Spring手动创建这个线程:
<bean id="pollThread" class="java.lang.Thread" init-method="start" destroy-method="interrupt">
<constructor-arg ref="watchServiceRunnableWrapper"/>
</bean>
<bean id="watchServiceRunnableWrapper" class="WatchServiceRunnableWrapper">
<constructor-arg ref="watchService"/>
</bean>
<bean id="WatchService" class="java.nio.file.WatchService" destroy-method="close"/>
WatchServiceRunnableWrapper
很简单:
public class WatchServiceRunnableWrapper implements Runnable {
private WatchService WatchService;
public WatchServiceRunnableWrapper(WatchService watchService) {
this.watchService = watchService;
}
public void run() {
watchService.poll();
//
}
}
我还没有对它进行测试,但它或多或少应该正常工作和关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.