Tips & Tricks - Monitor 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 "Insert in 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 directory to be watched>, e.g. c:\temp

Click "OK". Then copy the GUID of the generic event source, this can be found by selecting the generic event source and pressing F4. The options for experts must be activated for this.

Then create a generic event handler with 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. This requires the file information, which can be read with a Groovy action that is attached to the generic event handler.

Give the Groovy action the title "Write file information to 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 last step, a data group action is created that creates the new data record. Give it the name "Create file data set". 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".

Select "Processing context" as the type. The variable that stands for the file name in the Groovy script action is entered in "Value" - in our example "myFileName".

Confirm each change with "OK" and assign the value "myFileName" to the "Title" field. Use the same procedure to create the system value "myFile" and assign the value to the "File" field.

With this mapping, the file is copied meaning it is not removed from the directory. If the file is to be deleted in the directory after being uploaded to the Intrexx data field, this can be done 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, you need 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 are not created in the monitored directory, but are first created and only then moved or copied to the directory.

If this requirement is not met, the process may only see an incomplete or empty file. File permissions in the system or the duration of the "external" copying process may also play a role. In the latter case you can work with delays (dispatchDelay).