I'm venturing into a design problem that's new to me and I could use some ideas and feedback. Hopefully this will be like "Rubber Duck Debugging" and just walking through the issue here will help me figure this out.
Here's the business problem: Several manufacturing machines are producing parts. If all machines are running, a web page will display a timer showing how long all machines have been productive. If any machine goes idle the timer stops and remains at zero until all machines are productive again. The servlet must call this dao every 2 or 3 minutes to check the status of the machines. There may be several pages from different computers displaying this timer.
I already have a dao that runs the query and returns true to the servlet if any machine is idle. That part is easy.
Here's what I'm struggling with:
1. How to store the value of the timer so the same value is available to all pages.
2. When the servlet updates the value (for instance, stops the timer) is there a way to update all pages?
I'm thinking maybe using servlet context is the solution for problem 1, but I've never used context for anything. All my apps up until now have used page scope or request scope, so context scope is a new minefield for me.
As for problem 2, I can't think of a solution except auto-refresh for the pages. That is a viable option since these pages will displayed on kiosks and won't display anything except the timer value.
Maybe I need 2 servlets for this. One to run the query and store the value in context, and another one that just gets the value from the context and returns it to the requesting page? If I do this, what are the risks of running a servlet that does the query, sets the timer value in context, and then just "sleeps" for 3 minutes before running again? I'm nervous about anything that runs in a loop. I'd rather have some other way of triggering the servlet instead of letting it run in a loop but I haven't got any idea what that might be.
Ideas? Alternatives? Suggested reading?
"There is no reason for any individual to have a computer in his home" ~ Ken Olson, Co-founder of DEC, 1977
Thanks for the quick reply! But now the Rubber Duck is talking back to me.
I could maybe create a timer class as a singleton, with methods for run(), reset(), and currentValue(). It would always have the timer value available, but can I put a reference to a class like this in the context? Maybe there is something like this already available; I'll check SourceForge for clues.
The duck is telling me the first solution would be simplier...
What do you think about the issue of having the servlet run the query every 2 or 3 minutes? Just put the servlet in a loop, or is there a better way to do that?
Jk Robbins wrote:He tells me that all I can store in context is a static value
Bear Bibeault wrote:Huh? You can store anything you want.
I guess what I was trying to say is that to store a continuously changing value, I need an object that returns that value, not a variable. But that answers my question about storing a reference to a timer object in the context.
Jk Robbins wrote:I could maybe create a timer class as a singleton
Bear Bibeault wrote:Resist the urge to over-engineer. Why bother with a singleton? Just create one instance and store it in the app context. Over-complicating things is a recipe for future disaster,
KISS is always good advice. The reason I visualize it as a singleton is so I don't end up with multiple timers running with different values. I suppose I can just have the servlet check to see if it already exists and if so, skip instantiating a new one?
Jk Robbins wrote:What do you think about the issue of having the servlet run the query every 2 or 3 minutes? Just put the servlet in a loop, or is there a better way to do that?
Bear Bibeault wrote:I think that way lies madness and death. Servlets must return a response. Putting a servlet into a loop is akin to jabbing yourself in the eyes with a screwdriver. Don't even go there.
If you need something running in the background, do it in its own thread,
That's what I was afraid of. Can you elaborate on the threading option? This is a little above my pay grade. Where would the thread originate? I'm trying to visualize how this thread will interact with the servlet to make it fire off every 3 minutes.