Closed simon-gunacker closed 5 years ago
start()
works exactly like it does in the better-files File.Monitor
it extends. It doesn't block. It runs the watch thread on the global execution context you've provided implicitly. The issue you're seeing is that your program is finishing immediately after the watcher starts running in the background.
Yes. Non-blocking is a good thing for a file monitor ;-). But how can I run my program then? Let's just assume that it should not do anything else than printing the events ...
The Scala global
execution context sets threads to be daemon threads. The JVM will be shut down when all non-daemon threads have exited, so as soon as your main thread finishes, your watcher will be terminated. You either need some other non-daemon thread running doing your processing, or you need to use an execution context with non-daemon threads, for example:
implicit val ec: ExecutionContext =
ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(8))
Hard to suggest specifics about thread pools without knowing the kind of work you're doing, but the bottom line is you need some regular non-daemon threads running so the JVM doesn't exit.
I am trying to run the watcher in Scala. But even though I am calling
watcher.start()
(which i expect to run forever), the program finishes immediately. What am I doing wrong?