WatcherService hangs server at shutdown
tompilompi Jun 12, 2014 5:21 AMSo im running JBoss AS 7.1.1.Final "brontes" in a maven3 project running eclipse kepler and java7.
I am trying to build a watcherService that is monitoring a folder for updates. I have a Singleton that at startup initializes and then runs an asynhronous metod in another statelessbean. So far everything works and the watcherservice is started and monitors the assigned directory fine.
The problem comes to when i try to shutdown the server it hangs and won't go down and after a while eclipse tells me that the server has stopped responding.
why is this? Serverlogs doesn't say anything it just wont shutdown, i have to force quit it everytime.
here is my startup singleton
@Startup @Singleton @DependsOn("ExcelPropertiesLoader") public class FileSystemMonitorInitializer { @EJB private ExcelPropertiesLoader excelPropertiesLoader; @EJB private FileSystemMonitor fileSystemMonitor; public static Logger log = Logger.getLogger(FileSystemMonitorInitializer.class.getName());; @PostConstruct public void init(){ fileSystemMonitor.newFolderWatcher(); fileSystemMonitor.setMonitoredDir(excelPropertiesLoader.getMonitoredDir()); fileSystemMonitor.startWatching(); } }
and here is the actual filemonitor/watcher service:
@Stateless public class FileSystemMonitor { public static Logger log = Logger.getLogger(FileSystemMonitor.class.getName()); private WatchService watcher; private WatchKey key; private Path dir; public void newFolderWatcher() { try { watcher = FileSystems.getDefault().newWatchService(); } catch (IOException e) { log.error("Failed to init watcher"); e.printStackTrace(); } } public void setMonitoredDir(String filePath) { dir = Paths.get(filePath); WatchEvent.Kind<?>[] events = { StandardWatchEventKinds.ENTRY_CREATE }; try { key = dir.register(watcher, events); } catch (IOException e) { log.error("Failed to register watcher to selected directory"); e.printStackTrace(); } } @Asynchronous public void startWatching() { while (true) { log.info("Waiting for watch event"); log.info("Path being watched: " + key.watchable()); try { key = watcher.take(); } catch (InterruptedException e) { log.error("Watcher got interrupted"); e.printStackTrace(); } if (key.isValid()) { for (WatchEvent<?> event : key.pollEvents()) { if (event.kind().equals(StandardWatchEventKinds.ENTRY_CREATE) && event.count() == 1) { log.info("Context: " + event.context().toString()); String extension = Helpers.getFileExtension(event.context().toString()); String fileName = key.watchable().toString() + "\\" + event.context().toString(); if (extension.equals("xls")) { log.info("Filename: " + fileName); } else if (extension.equals("xlsx")) { log.info("Filename: " + fileName); } else { log.info("Unknown file format no action will be taken"); } } boolean result = key.reset(); System.out.println(result); } } } } }
server log output at shutdown (i have enabled trace logging for arjuna and jboss.jpa
10:36:40,351 INFO [org.jboss.as.osgi] (MSC service thread 1-11) JBAS011942: Stopping OSGi Framework 10:36:40,360 INFO [org.jboss.as.logging] JBAS011503: Restored bootstrap log handlers 10:36:40,367 INFO [org.jboss.as.connector.subsystems.datasources] JBAS010409: Unbound data source [java:jboss/datasources/ExampleDS] 10:36:40,372 INFO [org.apache.catalina.core.StandardContext] Container org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/] has not been started 10:36:40,374 INFO [org.apache.coyote.http11.Http11Protocol] Pausing Coyote HTTP/1.1 on http-localhost-127.0.0.1-8080 10:36:40,375 INFO [org.apache.coyote.http11.Http11Protocol] Stopping Coyote HTTP/1.1 on http-localhost-127.0.0.1-8080 10:36:40,379 INFO [org.jboss.weld.deployer] JBAS016009: Stopping weld service for deployment statistic-ear.ear 10:36:40,384 INFO [org.jboss.as.jpa] JBAS011403: Stopping Persistence Unit Service 'statistic-ear.ear/statistic-ejb.jar#statistic' 10:36:40,388 INFO [org.jboss.as.server.deployment] JBAS015877: Stopped deployment statistic-web.war in 29ms 10:36:40,389 INFO [org.jboss.as.server.deployment] JBAS015877: Stopped deployment statistic-ejb.jar in 30ms 10:36:40,390 INFO [com.arjuna.ats.jbossatx] ARJUNA032018: Destroying TransactionManagerService 10:36:40,391 INFO [com.arjuna.ats.jbossatx] ARJUNA032014: Stopping transaction recovery manager 10:36:40,404 INFO [org.jboss.as.server.deployment] JBAS015877: Stopped deployment statistic-ear.ear in 50ms
after that server just "waits"... i have no idea what to do.
if i dont start the async method server shutsdown fine. is it that the server is waiting for the thred to finish? i have tried predestroy to interrupt the thread, does not work....