wood burning stoves 2.0*
The moose likes Servlets and the fly likes load on start up Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "load on start up" Watch "load on start up" New topic
Author

load on start up

visu Nekk
Ranch Hand

Joined: Sep 06, 2005
Posts: 122
Hi,

can we have multiple servlets running in infinite loop with load-on-startup?

Thanks,
Visu Nekk
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

can you explain more please? I don't understand.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by visu Nekk:
can we have multiple servlets running in infinite loop with load-on-startup?


I could not get the meaning of servlets running in infinite loop?


Rahul Bhattacharjee
LinkedIn - Blog
visu Nekk
Ranch Hand

Joined: Sep 06, 2005
Posts: 122
Hi,

I have 5 servlets each doing a specific task and this has to happen round the clock. Hence I placed the business logic in an infinite loop and in the web.xml, I put them load-on-startup to a positive integer and hot deployed the code. But what's happening is, only one servlet is being processed.

Thanks,
Visu Nekk
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
I think the following is happening in your case.

You first servlet loads up as you have set load-on-startup to be positive.The container runs the code in init method of that servlet and never returns from that method(as that is an infinite loop).That is the issue.

You can start off a individual thread for each process and you do not need five servlets to kick off five threads.It can be done using only one servlet ,or ContextListner is even better suited for this task.

Hope this helps,
visu Nekk
Ranch Hand

Joined: Sep 06, 2005
Posts: 122
Thanks Rahul,

But can we place an infinite loop in a ContextListener. It its possible, will I be able to access the index page of the web application.

Visu Nekk
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by visu Nekk:
But can we place an infinite loop in a ContextListener. It its possible, will I be able to access the index page of the web application.


Hi Visu,

I guess I am missing something or you are.

I have asked you to execute the infinite loop in a separate thread and use context listener to kick off the thread.
Sunil Vasudevan
Ranch Hand

Joined: Mar 05, 2007
Posts: 107
Originally posted by visu Nekk:
Hi,

I have 5 servlets each doing a specific task and this has to happen round the clock.


It seems what you need is a scheduler. Did you evaluate Quartz (http://www.opensymphony.com/quartz/). This will do your job neatly without you having to get into the intricacies of multithreading.


Sunil.V<br />SCJP2, SCWCD1.4, SCBCD1.3
visu Nekk
Ranch Hand

Joined: Sep 06, 2005
Posts: 122
Thanks Rahul and Sunil,

How about using a TimerTask in tandem with a servlet or ContextListener?

Visu
Ashok Mor
Ranch Hand

Joined: Jul 17, 2007
Posts: 44
Directly we can't use TimerTask, because it's an abstract class, so first of first you need to extend it in term to use it.

Once you extend it in any class then after you can use that class with Timer class. So to start that task you can use this Timer class with Servlet or ContextListener.


TimerTask is best to use when ever you want to detach you task from your normal routine.


Ashok Mor
Ness Tech.
[ July 18, 2007: Message edited by: Ashok Mor ]
visu Nekk
Ranch Hand

Joined: Sep 06, 2005
Posts: 122
Thanks Ashok,

From a servlet, if I invoke a class that extends TimerTask, does the servlet maintain any reference to that class once the class is invoked. Because I have a doubt that since servlet is multithreaded, and we are spawning a new thread from the servlet, the servlet will be holding a reference to that thread, whcih might affect the performance. Could you please clarify this?

Visu
Ashok Mor
Ranch Hand

Joined: Jul 17, 2007
Posts: 44
Sure Visu.



You must be knowing Servlet right ???


So for Servlet we can have an init(..) method � and this init() method will be called once ever in it�s entirety, So what you can do is, just create an instance of Timer class in this servlet and initiate it in init(..) method. So there there will be a single instance of Timer, and it want create an issue like performance.


Is this clear to you ???


Ashok Mor,
Ness Technology.
visu Nekk
Ranch Hand

Joined: Sep 06, 2005
Posts: 122
Thanks Ashok,

It worked out and is working fine.
Ashok Mor
Ranch Hand

Joined: Jul 17, 2007
Posts: 44
You are always welcome Vishu......
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: load on start up