drapostolos / rdp4j

Remote Directory Poller for Java
MIT License
46 stars 25 forks source link

Add basic API to set/get start/end state of a DirectoryPoller. #26

Closed drapostolos closed 1 year ago

drapostolos commented 2 years ago

This PR adds:

  1. methods to initialize a DirectoryPoller with a state from a previous running DirectoryPoller.
  2. methods to read the current state of the last poll cycle. For example this information can be persisted and used later in a new running instance of a DirectoryPoller in a separate JVM, by using methods in 1).

NOTE! PR #27 builds upon this PR and adds a simple file persisting mechanism.

Example for 1) You can initialize the DirectoryPoller with a previous state by adding the files known to a previous running instance of DirectoryPoller by calling these methods:

DirectoryPoller.newBuilder()
.addPolledDirectory(new MyPolledDirectory(), CachedFileElement.of(...), CachedFileElement.ofFile(...), ...)
.addPolledDirectory(new MyPolledDirectory(), Set<CachedFileElement>)
...
.start();

Alternatively you can initialize the DirectoryPoller with a previous state by implementing DirectoryPollerListener.beforeStart(BeforeStartEventevent). Example:

@Override
public void beforeStart(BeforeStartEvent event) {
    event.addPolledDirectory(new MyPolledDirectory(), CachedFileElement.of(...), CachedFileElement.ofFile(...), ...)
    event.addPolledDirectory(new MyPolledDirectory(), Set<CachedFileElement>)
}

Example for 2)) Implement the DirectoryPollerListener.afterStop(AfterStopEvent event) interface to get access to the state after last poll cycle. Example:

@Override
public void afterStop(AfterStopEvent event) {
    /*
     * cachedFileElements is a Map, where the keys are your PolledDirectories 
     * and the values are each directory's files in the last poll cycle.
     * This can be used to persist the data using your choice of source (file/database etc.). 
     */
    Map<PolledDirectory, Set<CachedFileElement>> cachedFileElements = event.getCachedFileElements();
    cachedFileElements.entrySet().forEach(entry -> /* persist your data here */);
}
drapostolos commented 2 years ago

Possibly solves/partly solves #2, #10, #23, #24

drapostolos commented 1 year ago

This has been released in version 0.3.0