您可以創建一個監控在啓動時加載和代表一個異步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