您可以创建一个监控在启动时加载和代表一个异步Bean
@Singleton
@Startup
public class Initialiser {
@EJB
private FileSystemMonitor fileSystemMonitor;
@PostConstruct
public void init() {
String fileSystemPath = ....;
fileSystemMonitor.poll(fileSystemPath);
}
}
单身然后异步Bean看起来像这样
@Stateless
public class FileSystemMonitor {
@Asynchronous
public void poll(String fileSystemPath) {
WatchService watcher = ....;
for (;;) {
WatchKey key = null;
try {
key = watcher.take();
for (WatchEvent<?> event: key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue; // If events are lost or discarded
}
WatchEvent<Path> watchEvent = (WatchEvent<Path>)event;
//Process files....
}
} catch (InterruptedException e) {
e.printStackTrace();
return;
} finally {
if (key != null) {
boolean valid = key.reset();
if (!valid) break; // If the key is no longer valid, the directory is inaccessible so exit the loop.
}
}
}
}
}
喜尼古拉斯,##首先的thx为你的时间。目前我们正在使用JBoss 7.1.1,但可能会切换到Glassfish 3.1。 JMX绝对是一种选择,但我想知道,ESB(如JBoss ESB)如何解决这个问题,他们是否也使用JMX? – Subcomandante