在我的 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"/>
The WatchServiceRunnableWrapper
很简单:
public class WatchServiceRunnableWrapper implements Runnable {
private WatchService WatchService;
public WatchServiceRunnableWrapper(WatchService watchService) {
this.watchService = watchService;
}
public void run() {
watchService.poll();
//
}
}
我还没有测试过它,但它或多或少应该可以正常工作和关闭。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)