This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jyoti B.Shah
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13044
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
Hibernate Oracle Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Jyoti B.Shah
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20153
23
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
Hibernate Oracle Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13044
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic