0

experts.

I need to reload a file when it updated automatically in Liberty. To make it more clear, I want to make a path like "dropins" in liberty, it can automatically detect the change of files or we can scan this folder manually. I need to load the files in this folder when they changed.

I've no idea how to achieve this.... Could anyone here know about it?

Thx!

Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
Jiddu.K
  • 69
  • 9

1 Answers1

2

If you are not averse to writing a Liberty feature (not hard, but requires a little background reading), then you can register a listener for changes to specific files by implementing the com.ibm.wsspi.kernel.filemonitor.FileMonitor interface as a Declarative Service. Once registered as a DS, the Liberty file monitor will invoke your implementation's methods. It invokes onBaseline(Collection<File> baseline) on startup, and onChange(Collection<File> createdFiles, Collection<File> modifiedFiles, Collection<File> deletedFiles) when a change of some sort has occurred.

One implementation might look like this:

@Component(immediate="true", property={"monitor.directories=/path/to/myMonitoredDir"})
public class MyFileMonitor implements FileMonitor {
    @Override
    public void onBaseline(Collection<File> baseline) {
        System.out.println("Initial file state:");
        for (File f : baseline) {
            System.out.println(f.getName());
        }
    }

    @Override
    public void onChange(Collection<File> createdFiles, Collection<File> modifiedFiles, Collection<File> deletedFiles) {

        System.out.println("Newly added files:");
        for (File f : createdFiles) {
            System.out.println(f.getName());
        }

        System.out.println("Newly deleted files:");
        for (File f : deletedFiles) {
            System.out.println(f.getName());
        }

        System.out.println("Modified files:");
        for (File f : modifiedFiles) {
            System.out.println(f.getName());
        }
    }
}

Hope this helps,

Andy

Andy McCright
  • 1,273
  • 6
  • 8
  • Thanks so much, Andy! It's really helpful and I'm trying on that! Thanks for your solution and details, it's very clear! – Jiddu.K Sep 26 '17 at 15:50