@Startup - FileWatcher - Probleme beim Hochfahren des Servers

beta20

Top Contributor
Hallo zusammen,

ich möchte gerne einen FileWatcher starten, sobald der Wildfly server startet.
Das funktioniert auch, dass der FileWatcher beim ServerStart startet nur leider habe ich nun das Problem, dass der Server nicht hochfährt, da der FileWatcher ja dann eine while() Schleife hat und der Server nun die ganze Zeit in dieser while() - Schleife verweilt.

Wie kann ich das umgehen?
while(true) -> das scheint das Problem zu sein...

Hier mal meine Methode / Klasse:

Java:
@Startup
@Singleton
public class FileWatcher {


    @PostConstruct
    public void init() {

        System.out.println("Init file Watcher ");

        try {

            doStartFileWatcher();
        } catch (Exception e) {

        }
    }
Starten tue ich dann:
Java:
public static void watchDirectoryPath(Path path) {
        // Sanity check - Check if path is a folder
        try {
            Boolean isFolder = (Boolean) Files.getAttribute(path,
                    "basic:isDirectory", NOFOLLOW_LINKS);
            if (!isFolder) {
                throw new IllegalArgumentException("Path: " + path + " is not a folder");
            }
        } catch (IOException ioe) {
            // Folder does not exists
            ioe.printStackTrace();
        }
      
        System.out.println("Watching path: " + path);
      
        // We obtain the file system of the Path
        FileSystem fs = path.getFileSystem ();
      
        // We create the new WatchService using the new try() block
        try(WatchService service = fs.newWatchService()) {
          
            // We register the path to the service
            // We watch for creation events
            path.register(service, ENTRY_CREATE);
          
            // Start the infinite polling loop
            WatchKey key = null;
            while(true) {
                key = service.take();
              
                // Dequeueing events
                Kind<?> kind = null;
                for(WatchEvent<?> watchEvent : key.pollEvents()) {
                    // Get the type of the event
                    kind = watchEvent.kind();
                    if (OVERFLOW == kind) {
                        continue; //loop
                    } else if (ENTRY_CREATE == kind) {
                        // A new Path was created
                        Path newPath = ((WatchEvent<Path>) watchEvent).context();
                        // Output
                        System.out.println("New path created: " + newPath);
                    }
                }
              
                if(!key.reset()) {
                    break; //loop
                }
            }
          
        } catch(IOException ioe) {
            ioe.printStackTrace();
        } catch(InterruptedException ie) {
            ie.printStackTrace();
        }
      
    }
 
Zuletzt bearbeitet von einem Moderator:
Habe leider keine Ahnung vom WildFly-Server, deshalb frage ich mal ganz naiv, warum du den WatchService nicht einfach in einem eigenen Thread laufen lässt. Geht das in dem Umfeld nicht?
 

Zurück
Oben