# Java file watchservice quit working

**URL:** <https://forum.inductiveautomation.com/t/java-file-watchservice-quit-working/39868>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [October 21, 2020, 2:15pm UTC](https://forum.inductiveautomation.com/t/java-file-watchservice-quit-working/39868 "2020-10-21T14:15:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [October 21, 2020, 2:15pm UTC](https://forum.inductiveautomation.com/t/java-file-watchservice-quit-working/39868/1 "2020-10-21T14:15:08Z")

</div>

I am using a slightly modified version from this post

> [@Gateway Script to monitor directory for new files](http://forum.inductiveautomation.com/t/gateway-script-to-monitor-directory-for-new-files/16124/5):
>
> Thanks JordanCClark and PerryAJ, that simple code is exactly what I needed. Works perfectly, at this stage I’m happy to poll through a timed script and keep it simple. Thanks for the advice regarding WatchService though - I might have another use for that elsewhere in my project. Cheers Dan

The watch service worked perfectly for a couple of months but now it doesn't see any changes to my directory. I have it setup to run on gateway start. I checked to make sure the directory files are reachable, by using `system.file.readFileAsString` from the gateway and works fine. If I start a watch service on my local machine in the script console it works as expected on a local test folder. Any ideas on what the problem could be? I've stopped and started the service, the gateway, and rebooted...no changes.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 21, 2020, 3:51pm UTC](https://forum.inductiveautomation.com/t/java-file-watchservice-quit-working/39868/2 "2020-10-21T15:51:38Z")

</div>

Have you checked a thread dump while it’s not responding?

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [October 21, 2020, 3:58pm UTC](https://forum.inductiveautomation.com/t/java-file-watchservice-quit-working/39868/3 "2020-10-21T15:58:48Z")

</div>

No I haven’t, I will look into doing that and see what turns up

@PGriffith what should I be looking for?

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [October 21, 2020, 5:14pm UTC](https://forum.inductiveautomation.com/t/java-file-watchservice-quit-working/39868/4 "2020-10-21T17:14:15Z")

</div>

Looks like it is this thread

```auto
Thread [script-invoke-async] id=617, (TIMED_WAITING for java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@a201f51)
  sun.misc.Unsafe.park(Native Method)
  java.util.concurrent.locks.LockSupport.parkNanos(Unknown Source)
  java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(Unknown Source)
  java.util.concurrent.LinkedBlockingDeque.pollFirst(Unknown Source)
  java.util.concurrent.LinkedBlockingDeque.poll(Unknown Source)
  sun.nio.fs.AbstractWatchService.poll(Unknown Source)
  sun.reflect.GeneratedMethodAccessor99.invoke(Unknown Source)
  sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  java.lang.reflect.Method.invoke(Unknown Source)
  org.python.core.PyReflectedFunction. __call__ (PyReflectedFunction.java:186)
  org.python.core.PyReflectedFunction. __call__ (PyReflectedFunction.java:204)
  org.python.core.PyObject. __call__ (PyObject.java:422)
  org.python.core.PyObject. __call__ (PyObject.java:426)
  org.python.core.PyMethod. __call__ (PyMethod.java:139)
  org.python.pycode._pyx24._doAsync$3(<module:shared.watchService>:29)
  org.python.pycode._pyx24.call_function(<module:shared.watchService>)
  org.python.core.PyTableCode.call(PyTableCode.java:165)
  org.python.core.PyBaseCode.call(PyBaseCode.java:301)
  org.python.core.PyBaseCode.call(PyBaseCode.java:194)
  org.python.core.PyFunction. __call__ (PyFunction.java:387)
  org.python.core.PyMethod.instancemethod ___call__ (PyMethod.java:220)
  org.python.core.PyMethod. __call__ (PyMethod.java:211)
  org.python.core.PyMethod. __call__ (PyMethod.java:201)
  org.python.core.PyMethod. __call__ (PyMethod.java:196)
  com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:649)
  com.inductiveautomation.ignition.gateway.script.GatewaySystemUtilities$1.run(GatewaySystemUtilities.java:69)
  java.lang.Thread.run(Unknown Source)

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 21, 2020, 5:54pm UTC](https://forum.inductiveautomation.com/t/java-file-watchservice-quit-working/39868/5 "2020-10-21T17:54:37Z")

</div>

> [@dkhayes117](#):
>
> sun.nio.fs.AbstractWatchService.poll(Unknown Source)

Might be a JDK bug; eg:  
[https://bugs.openjdk.java.net/browse/JDK-7158947](https://bugs.openjdk.java.net/browse/JDK-7158947)  
If it only affects the gateway, you can try updating the runtime on the gateway to the latest Java 8 release, but there's no guarantee it will be fixed.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [October 23, 2020, 2:17pm UTC](https://forum.inductiveautomation.com/t/java-file-watchservice-quit-working/39868/6 "2020-10-23T14:17:41Z")

</div>

So weird. I updated to latest version of Java 8. If I invoke the file watcher on the gateway machine in a designer/client it works. If I invoke it on the same machine but in the gateway, it doesn’t work. In case I didn’t mention it, I’m watching a shared drive I connected to Ignition via the Ignition.conf file.

**EDIT**  
I deleted my mapped drive in the config file `\\L:` and renamed it to `\\K:` then stopped and started the gateway and I can still read files from the L drive? How is it still connected?

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [October 23, 2020, 8:09pm UTC](https://forum.inductiveautomation.com/t/java-file-watchservice-quit-working/39868/7 "2020-10-23T20:09:57Z")

</div>

I cannot get this working so I’m going to try a different method. Anyone with any java skills that can help me out? So far I have this

```auto
from java.nio.file import FileSystems,Files
from java.io import File
from org.apache.commons.io.monitor import FileAlterationMonitor
from org.apache.commons.io.monitor import FileAlterationObserver

	
class FileWatcher():
	isRunning = False
	
	def __init__ (self):
		self.watcher = None
		self.run()
		
	def _doAsync(self):
		self.isRunning = True
		
		directory = File("MyDirectory").toPath()
		observer = FileAlterationObserver(directory)
		listener = FileListener()
		observer.addListener(listener)
		self.watcher = FileAlterationMonitor(1000, observer)
		self.watcher.start()
		
		while self.isRunning:
			???
			
	def run(self):
		if not self.isRunning:
			system.util.invokeAsynchronous(self._doAsync)
	
	def kill(self):
		self.isRunning = False
		self.watcher.stop()

```
