How to implement External folder monitoring from AEM | Community
Skip to main content
Anny0505
Community Advisor
Community Advisor
June 19, 2023
Solved

How to implement External folder monitoring from AEM

  • June 19, 2023
  • 4 replies
  • 2255 views

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.
Best answer by Peter_Puzanovs

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

4 replies

Nishant-Singh
Adobe Employee
Adobe Employee
June 19, 2023

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 ?

Anny0505
Community Advisor
Anny0505Community AdvisorAuthor
Community Advisor
June 19, 2023

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

TarunKumar
Community Advisor
Community Advisor
June 19, 2023

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.

Peter_Puzanovs
Community Advisor
Peter_PuzanovsCommunity AdvisorAccepted solution
Community Advisor
June 19, 2023

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

Anny0505
Community Advisor
Anny0505Community AdvisorAuthor
Community Advisor
June 19, 2023

Sure, Thank you. I will try and see

Tanika02
Level 7
June 19, 2023

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.