Expand my Community achievements bar.

SOLVED

How to implement External folder monitoring from AEM

Avatar

Community Advisor

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. 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

6 Replies

Avatar

Level 2

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 ?

Avatar

Community Advisor

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

Avatar

Level 6

Hi @Aruna_surukunta_ ,

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.

Avatar

Correct answer by
Community Advisor

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

Avatar

Community Advisor

Hello @Aruna_surukunta_ 

 

  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.