Two Laptop Bag*
The moose likes Servlets and the fly likes background process Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "background process" Watch "background process" New topic

background process

john s murray
Ranch Hand

Joined: Apr 26, 2009
Posts: 30
hi, i'm a bit confused with thread in servlets. i'm trying to run a background process with a while(true) loop and sleep 2mins each time. The idea is that the thread would retrieve a url (ie stock quotes). Each time a request comes thru on doGet, they would automatically have an updated quote. I also looked at the isPrime example. Is that the proper way of accomplishing this task or should i be taking a different approach?

example 1:
public class PrimeSearcher extends HttpServlet implements Runnable
// kick off in init
public void getQuotes()

example 2:
public class extends HttpServlet
// kick off the getQuotes thread in init
class getQuotes implements Runnable

Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 514
Hi, looks like this will create a separate quote fetching thread for every servlet instance, and each thread will store its own copy of the quotes, thus duplicating the same information. Instead, consider starting just one quote fetching thread from ServerContextListener contextInitialized, and share the quote data between servlet instances (using ServletContext attributes, database,...).

EDIT: Ignore. I should read my specs more carefully.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

I wouldn't try to implement runnable from a servlet.
The servlet lifecycle is controlled by the container an is already called from within a threaded context.

Another approach would be to create a plain old Java object to fetch your stock data.
If you want to go with the treaded continuous loop approach, instanciate that object from a context listener.
Have it update some properties in a context scoped bean.
Then when someone makes a request you can look up the stock data from your context scoped bean.

If you want to get away from threads altogether just check the system time with each request, and update your context scoped bean by fetching the stock data whenever the data is more than n minutes old.

If you do go with the threaded approach, make sure you read up on the daemon property of the Thread object. Otherwise, you could end up having problems stopping, and restarting your application and/or container.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12809
The separate Runnable plain old java object approach has many advantages:

1. you can design and test it entirely outside the servlet environment
2. you can write a management function servlet (PW protected of course) to let you monitor how well it is working - delays, errors, etc
3. the management function servlet could be used to pause/restart the process

john s murray
Ranch Hand

Joined: Apr 26, 2009
Posts: 30
thanks for help comments. However, I tried implement it both ways:

The 1st option works. The 2nd option doesn't. It returns java.lang.NoClassDefFoundError. Any idea as to why?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 61456

Please UseCodeTags. Thanks.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42371
The 2nd option doesn't. It returns java.lang.NoClassDefFoundError. Any idea as to why?

Did you deploy the NAME.BackGround.class file along with the servlet class file?

Ping & DNS - my free Android networking tools app
john s murray
Ranch Hand

Joined: Apr 26, 2009
Posts: 30
sorry, just made changes to clarify the opening and closing of the braces so it's more understandable. I also tried doing it like the following with no luck. getting the same error as #2.
John Schretz
Ranch Hand

Joined: Sep 10, 2008
Posts: 188

You can also use the timer class. Very simple implementation.
Run it from a context Listener

jQuery in Action, 2nd edition
subject: background process