File Integration With Directory Service¶
In this section, we will learn how to create a file integration using the Ballerina Integrator. The integration will listen to events in a directory and will be triggered when an file related event occurs.
Step 1: Create a new integration project¶
- Click on the Ballerina Integrator icon on the sidebar.
- Click on the Create New Integration button.
- Enter the project name as
FileIntegration
. - Select Project Directory and click on the Select Location button.
- Click on the Create New Integration button to create the integration project.
Step 2: Create an Directory service¶
- In the design view, click on the Add Artifact button.
- Select Directory Service under the File Integration category.
- Enter the listener name as
directoryListener
. - Enter the path to the directory you want to monitor. For example,
"/home/user/Downloads"
. - Click on the Next button to create the directory service.
-
Keep the default listener in the Listener Configuration window and click on the Create button to create the directory service.
Step 3: Configure file event resources¶
- Navigate to the
directoryListener
service under the Entry Points section and click on the +Function button. - Select onCreate from Available Functions dropdown and click on the Save button.
- Click on the onCreate function to navigate to the function implementation designer view.
- Click on + and select Log Info from the node panel under Logging category.
- Add the log message as
"File created "+ event.name
in the Msg field. -
Click on the Save button to add the log action to the function.
-
Repeat the above steps to add the onDelete and onModify functions to the service.
- Add the log message as
"File deleted "+ event.name
in the Msg field for the onDelete function. - Add the log message as
"File modified "+ event.name
in the Msg field for the onModify function. -
The final service will look like this:
Step 4: Run the integration¶
- Click on the Run button in the top-right corner to run the integration.
- The integration will start listening to the events in the directory specified in step 2.
- Create a new file in the directory to trigger the onCreate event.
- Modify the file to trigger the onModify event.
- Delete the file to trigger the onDelete event.
-
The log messages will be displayed in the console.