Package org.oddjob.io

Class FileWatchService

java.lang.Object
org.oddjob.io.FileWatchService
All Implemented Interfaces:
Outbound<Path>, Service, FileWatch, Stoppable

public class FileWatchService extends Object implements FileWatch, Service, Outbound<Path>
Author:
rob
See Also:

Description

Provide a service for subscribers to watch a file system for Files existing, being created or being modified.

If the file is created during subscription the consumer may receive a notification for the same file twice. Once the subscription has succeeded a consumer should receive every creation and modification happening to the file.

If this service is stopped no notification is sent to consumers. Consumers must use the state of this service to know that it has stopped.

Consumers will receive creation and modification events on a different thread to the initial event if the file exists.

Implementation Note: This facility is still a work in progress. Requiring this service in a configuration is messy. In future releases this service should be hidden from users.

Example

Trigger when two files arrive.

<oddjob>

    <job>

        <sequential>

            <jobs>

                <mkdir dir="${some.dir}/etc" name="Create Test Dir"/>

                <file-watch id="file-watch" kinds="ENTRY_CREATE"/>

                <events:when id="both-files" name="When Both Files" xmlns:events="oddjob:events">

                    <jobs>

                        <events:list eventOperator="ALL">

                            <of>

                                <events:watch name="Watch File 1">

                                    <eventSource>

                                        <events:file>

                                            <fileWatch>

                                                <value value="${file-watch}"/>

                                            </fileWatch>

                                            <file>

                                                <value value="${some.dir}/file1.txt"/>

                                            </file>

                                        </events:file>

                                    </eventSource>

                                </events:watch>

                                <events:watch name="Watch File 2">

                                    <eventSource>

                                        <events:file>

                                            <fileWatch>

                                                <value value="${file-watch}"/>

                                            </fileWatch>

                                            <file>

                                                <value value="${some.dir}/file2.txt"/>

                                            </file>

                                        </events:file>

                                    </eventSource>

                                </events:watch>

                            </of>

                        </events:list>

                        <echo id="task">

                            ${both-files.trigger.ofs}

                        </echo>

                    </jobs>

                </events:when>

                <folder>

                    <jobs>

                        <copy id="createFile1" name="Create File 1" to="${some.dir}/file1.txt">

                            <input>

                                <buffer>

                                    Test1

                                </buffer>

                            </input>

                        </copy>

                        <copy id="createFile2" name="Create File 2" to="${some.dir}/file2.txt">

                            <input>

                                <buffer>

                                    Test2

                                </buffer>

                            </input>

                        </copy>

                        <delete name="Delete All Test Files">

                            <files>

                                <files files="${some.dir}/*"/>

                            </files>

                        </delete>

                    </jobs>

                </folder>

            </jobs>

        </sequential>

    </job>

</oddjob>
  • Constructor Details

    • FileWatchService

      public FileWatchService()
  • Method Details

    • start

      public void start()
      Description copied from interface: Service
      Start the service.
      Specified by:
      start in interface Service
    • stop

      public void stop()
      Description copied from interface: Stoppable
      Stop executing. This method should not return until the Stoppable has actually stopped.
      Specified by:
      stop in interface Stoppable
    • subscribe

      public Restore subscribe(Path path, Consumer<? super InstantEvent<Path>> consumer)
      Description copied from interface: FileWatch
      Subscribe to notifications for the existence or modifications in the file.

      If the file exists already the consumer will be notified before this method returns. If the file is created during the call to this method then the consumer may receive a notification for the same file twice. A consumer could use the modified date to remove duplicated but then may miss file modifications that happen close together. The documentation for implementations should adequately explain such behaviours.

      Specified by:
      subscribe in interface FileWatch
      Parameters:
      path - The path of the file to watch. Must not be null.
      consumer -
      Returns:
      Something that will close resources.
    • setFilter

      public void setFilter(String filter)
    • getFilter

      public String getFilter()
    • getNumberOfConsumers

      public int getNumberOfConsumers()
    • getName

      public String getName()
    • setName

      public void setName(String name)
    • getKinds

      public String getKinds()
    • setKinds

      public void setKinds(String kinds)
    • getPaths

      public List<Path> getPaths()
    • setPaths

      public void setPaths(List<Path> paths)
    • getTo

      public Consumer<? super Path> getTo()
    • setTo

      public void setTo(Consumer<? super Path> to)
      Description copied from interface: Outbound
      Set the out bound destination.
      Specified by:
      setTo in interface Outbound<Path>
      Parameters:
      to -
    • toString

      public String toString()
      Overrides:
      toString in class Object