File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Regarding autostart of a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Regarding autostart of a servlet" Watch "Regarding autostart of a servlet" New topic
Author

Regarding autostart of a servlet

Priya Sri
Ranch Hand

Joined: Mar 30, 2004
Posts: 84
Hi friends,

I want to call a java file on autostart when the server is up. This java file is having an endless while loop which will fetch values from a table and will insert in other two tables.

Inorder to make this java file to run as autostart, I have written a servlet and used <load-on-startup>1</load-on-startup> in web.xml file.

So, the servlet is getting called when the server is starting and it is inturn calling this java file and going to endless while loop and checking for entries in table and trying to store in other two tables.

But the problem is java file is going to endless loop and the server is still in startingstage only. the server is never getting started fully.

I want the servlet to be called once the server is fully up.
Can anyone help me how to achieve this???
I am using WSAD.

Thanks in advance
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
May I know your requirement, why you want to call endless loop in the beginning of application? :roll:
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9317
    
109

This java file is having an endless while loop which will fetch values from a table and will insert in other two tables.


Why are you doing this? Any specific requirements? If so let us know, so that someone might suggest a better approach to handling the same


[My Blog] [JavaRanch Journal]
Priya Sri
Ranch Hand

Joined: Mar 30, 2004
Posts: 84
rathi ji,

My requirement is

An end user will submit an application form and the application numbers are stored in a table(take as first table) and the user will be given response that your application is submitted. There ends user work.

At Backend, I need to take application numbers from a first table(if present) and needs to store in two more tables continuously(ex: for every 10 min or 20min.) will delete the processed application number from the first table.

Like taking pending application numbers in first table needs to be stored in other two tables. This should happen continuously. For this I have written endlessloop

java file
while (true)
{
get application numbers from fist table
store in other two tables
}
servlet
will call the above java file in init method
and I declared <load-on-startup> for the above servlet.

Thanks
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Priya, I would suggest that you should not run your while (true) loop in Servlet � as doing this mean control will remain with Servlet for infinite time and will never let Server to start-up further.

Better you have a thread created in a Servlet that run after specified intervals for a spesified time period and again sleep(to wake-up again at spesified interval).Let thread to process your business logic.

If it is possible then transfer business logic from Servlet to database object � you will have less hazards.


My blood is tested +ve for Java.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
The solution to this is very simple.As you have stated that its a endless loop..that I can see from while(true){}.So the control is not returning from that piece of code.So the context is not getting initialized properly.

Solution
========


Make a thread and put your endless loop in the run().set the thread as deamon by thread.setDeamon(true) and then start the thread happily.
In this case what will happen is ..jvm would start a new thread and return the control..that is important in this issue.

that is it .. ;-)
hope this helps.


Rahul Bhattacharjee
LinkedIn - Blog
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9317
    
109

Though the usage of thread might solve your issue, it is recommended not to start your own threads in a web application from Servlets or EJBs. Instead you can think of JMS, but i am not sure whether using JMS is feasible in your case.
Priya Sri
Ranch Hand

Joined: Mar 30, 2004
Posts: 84
Thanks Chetan.

I will try with threads...

But is there any solution without using threads??

Thanks
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Post your Servlet code and bit more details about your business requirment.
Ramgopal Reddy
Greenhorn

Joined: Jan 28, 2006
Posts: 22
put you code here if i can i'll modify it.


Ram's
Priya Sri
Ranch Hand

Joined: Mar 30, 2004
Posts: 84
Hi all,

This is the Exact code....

My servlet : init method
Except init method all other methods are empty in servlet

public void init() throws ServletException {
sd=new StoreAppData();
sd.ProcessPendingApp();
super.init();
}

JAVA File
This is the method called by servlet in java file.

public void ProcessPendingApp(){
while(true)
{
try{
int appCount=0;
ArrayList appRequestList = checkPendingApplications();
appCount = appRequestList.size(); //count of applications
if (appCount>0)
{
for (int i=0; i<appCount; i++)
{
String pending_app_id = appRequestList.get(i).toString(); //each application number
saveProcessXMLFieldData(pending_app_id); //Store other two tables in this method
}//End For loop
}//End if
Thread.sleep(30000);// sleep for 3 minutes
}//End try block
catch(Exception e)
{
System.out.println("Error Message is "+e.getMessage());
}//End Catch
} //End While
}//End method

Thanks for your help.
[ September 15, 2006: Message edited by: Priya Sri ]
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Jaikiran : Yes its true that in a web context we should try to avoid the use of threads.We should analyse as why we do this.
The reason is if we write the thread code in improper way then it might happen that after the web context in which you have deployed your application is brought down the thread continues to run.That is why I have asked to make the thread deamon..so that as soon as the main thread get killed or complets then this deamon should also be stopped.I have myself implements something like this is a live project and having such 4 thread in the applicatio.It running well from the last 2 years in production well..did not get any complian in that particular piece of code.


I wonder how is your code working.

you should be swamping new thread for this operation.

Runnable myRunable = new Runnable(){
run(){
while(true){
..my oreration..
}
}
}//end of runnable

init(){
Thread t = new Thread(myRunable);
t.setDeamon(true);
t.start();
}

this should work..
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Priya try with the below posted modified code. I have not compile the code, so you make get compilation error. But basically you can derive the logic.

Hope you problem will be solved after implementing this.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding autostart of a servlet
 
Similar Threads
load-on-startup problem ?
cannot load servlet name :
Not able to initialize the properties file
Following Old Tutorial Using Tomcat 6 and having problems with Invoker Servlet (Deprecated)
Calling a servlet on startup