Advanced Techniques - Monitor a directory
This post will show you how you can monitor a directory on the server with
Intrexx.
Create the application
Create a
new application
with the template
Empty application
in the
Applications module. Rename the application to
Directory Monitor. Then open the
properties of the
data group and switch
to the
Data fields tab
and add the following fields there:
- Title (Data type: Short text)
- File (Data type: File)
The edit page can be deleted, as it isn't needed. Now we'll add a
View table to the
overview page (All Entries), the table will list all of the records of
the data groups and display both fields.
Save the application.
Create the process
Create a
new process
with the title
Directory Monitor
in the
Processes module.
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 inserted 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 script 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. Enter
Add file record as its title.
Select the option
Add record.
Then select the data group from the
Directory Monitor
application as the target data group on the following page.
On the field assignment page the previously saved values must be
called out of the processing context again.
To do that, click on
User-defined value.
Select the option
System value here.
Then click on
Edit system value.
As the type, select
Processing context.
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.