Search code examples
reloadwebsphere-libertyauto-updateopen-liberty

How to automatically reload a changing file in liberty


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!


Solution

  • 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