aspose file tools*
The moose likes Servlets and the fly likes ideas for a servlet design problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "ideas for a servlet design problem" Watch "ideas for a servlet design problem" New topic
Author

ideas for a servlet design problem

J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 917
    
  13

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?



"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

1. The application context (servletContext) is the perfect place to store values shared by the whole application.

2. There's no need to refresh the entire page just to update one value; use Ajax to periodically fetch information behind-the-scenes and update the display accordingly.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 917
    
  13

Thanks for the quick reply! But now the Rubber Duck is talking back to me.

He tells me that all I can store in context is a static value, say the time that the timer was started. Then I'll need a servlet that gets that value, compares it to the current time, and sends the delta back to the page so javascript can display it as a clock.

OR

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?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

Jk Robbins wrote:He tells me that all I can store in context is a static value

Huh? You can store anything you want.

I could maybe create a timer class as a singleton

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,

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?

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,
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 917
    
  13

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.

I'm off to do some reading on threading...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

Jk Robbins wrote:
Jk Robbins wrote:I suppose I can just have the servlet check to see if it already exists and if so, skip instantiating a new one?


Why have the servlet do anything at all? Establish one instance in app context in a context listener at app startup time and then you don't ever need to worry about whether it exists or not.

That's also a good place to start off a worker thread if need be.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 917
    
  13

Bear Bibeault wrote:
Jk Robbins wrote:I suppose I can just have the servlet check to see if it already exists and if so, skip instantiating a new one?


Why have the servlet do anything at all? Establish one instance in app context in a context listener at app startup time and then you don't ever need to worry about whether it exists or not.

That's also a good place to start off a worker thread if need be.


Okay, I've been reading about ContextListener, Timer, and TimerTask, and I think I have enough to get started. Thanks for the help.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ideas for a servlet design problem