How to implement External folder monitoring from AEM | Community
Skip to main content
Anny0505
New Participant
Jun 19, 2023 4:00 AM UTC
Solved

How to implement External folder monitoring from AEM

Hello All,

 

I have a requirement to watch or monitor external folders for adding new text files and invoke servlet in AEM based on the file that is placed in the external folder.

 

Please provide suggestions on implementing this in AEM. 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Peter_Puzanovs
Peter_PuzanovsAccepted solution
New Participant
Jun 19, 2023 12:41 PM UTC

Hi Aruna,

 

You can write an OSGi service to monitor native folders. 

On Activate method mount callback's Apache Commons IO Library provides a neat FileAlterationListener interface.

 

on event such as onFileCreate(File file) {

 

}

 

You can create syntheticRequuest and use RequestDispatcher to call any Servlet you wish from within your java code.

 

Regards,

Peter

6 replies

Tanika02
Level 7
Jun 19, 2023 1:46 PM UTC

Hello @anny0505 

 

  1. Set up a File System Watcher: A File System Watcher is a mechanism to monitor a directory for file system events. In AEM, you can use the Apache Commons IO library to implement a File System Watcher. Add the Apache Commons IO dependency to your AEM project.

  2. Create a Servlet: Create a custom servlet in AEM that will be invoked when a new file is added to the external folder. The servlet will handle the logic to process the file.

  3. Implement the File System Watcher: In your AEM application, implement the File System Watcher to monitor the external folder. This can be done by creating a separate Java class that extends the org.apache.commons.io.monitor.FileAlterationListenerAdaptor class. Override the necessary methods such as onFileCreate to handle file creation events.

  4. Register the File System Watcher as an OSGi Service: Register the File System Watcher class as an OSGi service in AEM. This can be done by creating a service component that exposes the File System Watcher class as a service.

  5. Invoke the Servlet: In the onFileCreate method of the File System Watcher, invoke the custom servlet using the org.apache.sling.api.servlets.SlingSafeMethodsServlet or org.apache.sling.api.servlets.SlingAllMethodsServlet classes. You can use the HttpClient or URLConnection to make a request to the servlet.

Translate
Peter_Puzanovs
Peter_PuzanovsAccepted solution
New Participant
Jun 19, 2023 12:41 PM UTC

Hi Aruna,

 

You can write an OSGi service to monitor native folders. 

On Activate method mount callback's Apache Commons IO Library provides a neat FileAlterationListener interface.

 

on event such as onFileCreate(File file) {

 

}

 

You can create syntheticRequuest and use RequestDispatcher to call any Servlet you wish from within your java code.

 

Regards,

Peter

Translate
Anny0505
Anny0505Author
New Participant
Jun 19, 2023 1:46 PM UTC

Sure, Thank you. I will try and see

Translate
TarunKumar
New Participant
Jun 19, 2023 10:36 AM UTC

Hi @anny0505 ,

If you are maintaining an external database then you would be having a data source configuration which represent the connection details of your database and Java class which actually runs a query.
My assumption is that if you could write a query in your Java class to check for any change in the resource or file.

Translate
Nishant-Singh
Employee
Jun 19, 2023 5:52 AM UTC

Can you please provide more clarity on "external folders" its only name of the folder but exist in AEM DAM or its outside of the AEM DAM ?

Translate
Anny0505
Anny0505Author
New Participant
Jun 19, 2023 5:54 AM UTC

It is outside of aem repo. It is not a aem resource 

Translate