File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

background process

 
john s murray
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Pie
Posts: 1053
24
Android C++ Java Linux PHP Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Bill
 
john s murray
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags. Thanks.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
john s murray
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic