File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Design a servlet that gets values from a file and update itself as soon as file is modified Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Design a servlet that gets values from a file and update itself as soon as file is modified" Watch "Design a servlet that gets values from a file and update itself as soon as file is modified" New topic
Author

Design a servlet that gets values from a file and update itself as soon as file is modified

Jyoti B.Shah
Greenhorn

Joined: Nov 13, 2008
Posts: 17
Hi All,

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.

Thank you,
Jyoti


SCJP(95%)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

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"?

What are you really trying to accomplish?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12779
    
    5
Why not just have the servlet always query your helper class instance for the current value(s)?

That way you dont end up with instance variables in the servlet.

Bill
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hi Bill,

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.


Regards
Salil Verma
Jyoti B.Shah
Greenhorn

Joined: Nov 13, 2008
Posts: 17
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?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18563
    
    8

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.
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

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
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12779
    
    5
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.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Design a servlet that gets values from a file and update itself as soon as file is modified