How should i design the application if i want to read something from a file and then update the servlet with the new content. I can create a separate java class that reads from file and also checks its date modified in a infinite while loop. When the date modified is changed, it has to update the servlet with the new content. How should that part be handled?
Any ideas or any other approaches/suggestions would be helpful.
Your requirements aren't clear. For example, the whole concept of "updating a servlet" makes no sense. A servlet executes momentarily when a request is made, and serves a response. How could it be "updated"?
I think, your solution is nice. But in some way can we make some event handler, which could modify an attribute representing the change in the servlet context.
It is just a wild though, as it would give us following benefit..
1- We would not have to do the file processing to identify the change and would reuqire less time to complete the servlet processing(due to less no of steps performed to finalize the contents to display in view).
2- The change and the data would be available in RAM rather than in hard disk. This would improve the performance a lot.
Joined: Nov 13, 2008
Thank you all for your replies so far. I agree that requirements were not clear. Let me elborate a little bit:-
1. I want the application to get notified when content in that file changes
2. I want basically, that the application has the updated content without me having to redeploy the container. For example, if my file initially contained "1" in the file, and when a request is made to a servlet in that application, it sends '1' as reponse. Now the content in the file is changed to "2". Subsequent requests to that servlet or any part of the web app should have the updated content. So as long as the application is running, every request made to web-app should have the latest content from the file.
I think Salil is thinking along the right lines. We should be storing the content as a ServletContext attribute, so that it available to all components of web-app. And that attribute should be reset by the Java class that is running the infinte loop and reading from the file, when it gets updated.
Now how will java class will add something to the servlet context object? Via a listener? ServletContextAttributeListener? Who will implement that listener? each servlet in the application? And the listener should be put in the deployment descriptor file as well, right?
Basically if you want to be notified when something in a database changes, you have to put a trigger into the database which reacts to that change by (somehow) sending a message to your servlet. This is usually impractical, and in most cases people do the next-best thing, which is to poll the database every minute, or every hour, and when the poller notices a change, it notifies your servlet.
Does any one has any idea if we could achieve this using Quartz jobs?
Jyoti - Quartz is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components or EJBs. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering.
Quartz is freely usable, licensed under the Apache 2.0 license. You can find further details on the following link
Author and all-around good cowpoke
Joined: Mar 22, 2000
I want the application to get notified when content in that file changes
You could have the servlet implement the java.util.Observer interface and the utility which watches the file last modified date can include an instance of java.util.Observable - a classic pattern.