• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic