Skip to content

Martini Invoking Services via a Directory Watcher Trigger

The Directory Watcher trigger allows applications to respond to file changes within a specified directory on the server's local file system. This capability is essential for scenarios requiring the initiation of further processing upon the addition, modification, or deletion of files in a directory.

File System Compatibility

The performance of the Directory Watcher trigger is contingent on the underlying file system's ability to notify changes. It functions effectively with local file systems on Windows, macOS, and Linux. However, remote or shared files systems are NOT supported by the Directory Watcher trigger. File system must be a locally attached disk to the server.

Configuration

General Properties

General trigger configuration applies, including properties such as trigger name, description, among others, as elaborated in the general trigger documentation.

Directory Watcher-Specific Configuration

Property Default Description
Directory (required) The directory to monitor for changes.
Recursive false Indicates whether changes in subdirectories should also trigger notifications.
Events (required) Specifies the types of file system events to monitor: 'Addition', 'Modification', 'Deletion'.

Service Parameters

The Directory Watcher trigger provides specific parameters for managing file system event notifications.

Name Type Description
fileAction io.toro.martini.event.FileEvent.Action Identifies the action that activated the trigger: ADDED, MODIFIED, or DELETED.
action java.lang.String The string representation of fileAction.
file java.nio.file.Path The file that caused the trigger to activate.
filename java.lang.String The absolute path of the file.
inputStream java.io.InputStream An InputStream to the file, automatically closed after service execution.
reader java.io.Reader A Reader for the file, automatically closed after service execution.
multipartFile org.springframework.web.multipart.MultipartFile A multipart file reference.
bytes byte[] File data in bytes, made available when matched with a service parameter name.
content java.lang.String File content as a string, made available when matched with a service parameter name.

Best Practices

  • Secure the monitored directory to prevent unauthorized access or alterations.
  • Extensively test the Directory Watcher trigger in the deployment environment to ensure its functionality meets expectations.

Troubleshooting

  • Issue: The trigger does not activate on file changes.
  • Check: Verify the directory path is correct and accessible with the appropriate permissions.
  • Check: In situations where the Directory Watcher trigger's performance is suboptimal, consider implementing a scheduler for manual file system inspections.

Examples

Service Example

Example of a service configuration that sends the file, which triggered the Directory Watcher, to a remote FTP server. This includes steps for retrieving FTP server credentials, fetching the remote file representation using the filename, and writing the bytes data to the remote file.

Groovy Script as Service

Example Groovy script that prints all available variables in the context when a file change is detected. This script can be used to understand what information is available for processing when a file event occurs.

1
2
3
['properties', 'parameters', 'martiniPackage', 'fileAction', 'action', 'file', 'filename', 'inputStream', 'reader', 'multipartFile', '$trackerId', '$tracker'].each {
    println "$it\t : " + this[it]
}

Method as Service

An example method for sending file content via email, demonstrating how Martini can be used to automate email notifications based on file events. This method is particularly useful for small files; for larger files, accessing the data via inputStream or directly from the file is recommended to avoid memory issues.

1
2
3
4
5
class Reporter {
    def sendFileAsEmailBody(String content) {
        'smtps://myEmail:password@server/Daily Report'.send([to: 'daily-reports@work.com'], content);
    }
}

Handling Large Files Warning

For processing large files, it is recommended to use inputStream or direct file access rather than relying on content or bytes to avoid potential memory overload.

Additional Notes

Ensure compatibility with your file system when using the Directory Watcher. For more complex scenarios, consider leveraging extension modules available in Martini.

For further examples and scripts, the Martini examples package provides a wealth of resources to explore.