Tips & Tricks - Monitor a Directory

This post demonstrates how to use Intrexx to monitor a directory on the server. For the system to function properly, all relevant users must have write access to the directory.

Create an Application

In the Applications module, create a new application using the "Empty Application" template. Name it "Monitor Directory. " Create a data group with the following data fields:

  • Title (Data Type "Short Text")

  • File (Data Type "File")

On the overview page, a view table is created that includes the "Title" and "File" data fields as columns.

Save the application.

Create a process

In the "Processes" module, create a new process titled "Monitor Directory. "

Create a generic event source. Name it "Paste into c:\temp". The directory you will be using below must exist on the server. On the second page of the Properties dialog, you can select the Java class that defines the event source:

            de.uplanet.lucy.server.workflow.eventsource.FileSystemWorkflowEventSource
        

The following properties are added to the lower section of the dialog:

  • watchCreate (boolean): true

  • watchedDirectory (String): <Name of the directory to be monitored>, e.g., c:\temp

Click "OK." Then copy the GUID of the generic event source, which you can find by pressing the F4 key when the generic event source is selected. To do this, the expert options must be enabled.

Next, create a generic event handler titled "Intercept Event from Source." For the Generic Event Handler as well, a Java class and properties are specified in the second window of the Properties dialog.

  • Class: de.uplanet.lucy.server.workflow.eventhandler.FileSystemWorkflowEventHandler

  • handleWatchDirectoryCreateEvent: true

  • eventSourceGuid: Enter the copied GUID of the generic event source here. This tells the event handler that it should respond only to create events from this source.

Now the newly added file needs to be saved in an Intrexx record. To do this, you need the file information, which can be retrieved using a Groovy action attached to the generic event handler.

Name the Groovy action "Write File Information to the Processing Context." Enter the following script here.

            def myFileName = g_event.getContext() 
def myDir = g_event.getWatchedDirectory() 
def myFile = new File("${myDir}/${myFileName}")
g_sharedState.myFileName = myFileName 
g_sharedState.myFile = myFile 

        

The first two lines read the filename and the monitored directory.

In the third line, a new Groovy File object is created, which is then written to the processing context—along with the file name.

In the final step, a data group action is created to create the new record. Name it "Create File Record." Select the "Add Record" action and choose the data group from the "Monitor Directory" application as the target data group.

During field mapping, the previously saved values must be retrieved from the processing context.

To do this, click " " > "Custom Value" and select the "System Value" option. Then click "Edit" under " ."

Select "Processing Context" as the type. Enter the variable representing the file name in the Groovy script action in the "Value" field—in our example, "myFileName."

Confirm the changes by clicking "OK" each time, and assign the value "myFileName" to the "Title" field. Using the same procedure, create the system value "myFile" and assign the value to the "File" field.

This assignment copies the file, so it remains in the directory. If you want the file to be deleted from the directory after it has been uploaded to the Intrexx data field, you can do so using an additional, subsequent Groovy action.

The process is now complete and can be saved. You can test it by placing a file in the monitored directory and checking to see if the file appears in the application.

Note

When creating processes that use a FileSystemWorkflowEventSource, it is important to understand when events are triggered. Especially when files are processed using programs at their storage location, you must take into account how those programs work. For example, temporary files are created and then deleted, or files are deleted and rewritten, etc.

Problem:

Windows no longer sends events when, for example, the server is restarted.

Solution:

Intrexx has no control over this. This is an operating system error.

To prevent any execution errors, you can create a helper process that automatically deactivates the process and then reactivates it.

Problem:

"Monitor Directory" and "Import PDF" do not work. The problem is that some imported files have a file size of 0 and cannot be opened.

Solution:

It is important that the files not be created in the monitored directory, but rather be created first and then moved or copied into the directory.

If this condition is not met, the process may only see an incomplete or empty file. It's also possible that file permissions in the system or the duration of the "external" copy process could be a factor. In the latter case, you can use delays (dispatchDelay).