Tips & Tricks - Monitor a directory

This post will show you how you can monitor a directory on the server with Intrexx. For this to function correctly, all relevant users need write access to the directory.

Create an application

Create a new application in the Applications module using the "Empty application" template. Give it the title "Monitor directory". Create a data group with the following data fields :

  • Title (data type "Short text")

  • File (data type "File")

A view table is created on the overview page in which the data fields "Title" and "File" are included as columns.

Save the application.

Create process

In the "Processes" module, create a new process with the title "Monitor directory".

Create a Generic event source. Give it the title "Add to c:/temp". The directory, which will be used in the following, must be on the server. On the second page of the properties window you can select the necessary Java class, which defines the event source:

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

The following properties should be added in the lower area of the window:

  • watchCreate (Boolean): true

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

Click on "OK". Then copy the GUID of the generic event source, this can be found by selecting the generic event source and pressing F4. To do that the expert options need to be enabled.

Now add a Generic event handler and give it the title "Intercept event from source". Again, on the second page of the properties window you need to select a Java class and add properties.

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

  • handleWatchDirectoryCreateEvent: true

  • eventSourceGuid: The copied GUID of the generic event source is entered here. This communicates to the event handler that it should only respond to create-events from this source.

Now the newly added files must be saved in an Intrexx data record. To do this the file information is needed, which can be read with a Groovy action that is attached to the generic event handler.

Give the Groovy script action the title "Write file information in processing context". Insert the following script.

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

In the first two lines the file name and the monitored directory will be picked out.

A new Groovy file will be created, which then – along with the file name – will be written in the processing context.

In the final step, a Data group action will be created, which will add the new record. Give it the name "Create file data record" . Select the "Add data set" action and the data group from the "Monitor directory" application as the target data group.

On the field assignment page the previously saved values must be called out of the processing context again.

To do this, click on "User-defined value" and select the "System value" option. Then click on "Edit system value".

Select "Processing context" as the type. In the "Value" field enter the variable, which stands for the file name in the Groovy action script – in our example "myFileName".

Confirm the changes with OK and map the value "myFileName" to the "Title" field. With the same method, create the system value "myFile" and map it to the "File" field.

With this mapping, the file is copied meaning it is not removed from the directory. If the file should be removed from the directory after it has been uploaded to the Intrexx file field, this can be achieved with another, subsequent Groovy action.

The process is now finished and can be saved. You can test it by adding a file to the monitored directory and checking whether the file is listed in the application.

Comment

When creating processes that use a FileSystemWorkflowEventSource, it is important to understand when events are triggered. Especially when files are processed at the place of storage using programs, it is essential to take the way they work into account. For example, files may be created temporarily and deleted again, or files may be deleted and rewritten, etc.

Problem:

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

Solution:

Intrexx has no influence on this. This is an error of the operating system.

To avoid possible execution errors, an auxiliary process can be created that automatically disables and then re-enables the process.

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 that they be created first, and only then moved or copied to the directory.

If this condition is not met, the process may see only an incomplete or empty file. It is also possible for the file rights in the system or the duration of the "external" copy process to play a role. In the latter case you can work with delays (dispatchDelay).