Alex Rivera | Logout

groovy singleton pattern

Asked 2011-09-30T15:18:11.750
9

Q1. What is the best way to implement the singleton pattern using groovy? What are the other options available in groovy to support the singleton mechanism?

Any example that would be useful.

Q2. Does groovy support something like File changed listener?

Edit
Report

1 Answer

7

Regarding Q2: while groovy itself doesn't provide any way to be notified on file changes, Java 7, which can be used with groovy, does.

In particular, if you want to watch for file changes on a file foo in the current directory, you can do something like this:

import java.nio.file.*
FileSystems.default.getPath(".")  // dot for current directory
def watchKey = p.register(FileSystems.default.newWatchService(),
    StandardWatchEventKinds.ENTRY_MODIFY)
def events = watchKey.pollEvents()
events.findAll{it.context().fileName == 'foo'}.each { event ->
    println "foo was changed"
}
answered 2011-09-30T16:39:05.440

Your Answer