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.
Solved! Go to Solution.
Views
Replies
Total Likes
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
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 ?
It is outside of aem repo. It is not a aem resource
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.
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
Sure, Thank you. I will try and see
Hello @Anny0505
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.
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.
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.
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.
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.
Views
Likes
Replies
Views
Likes
Replies