• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding autostart of a servlet

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May I know your requirement, why you want to call endless loop in the beginning of application? :roll:
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Priya Sri
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chetan.

I will try with threads...

But is there any solution without using threads??

Thanks
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post your Servlet code and bit more details about your business requirment.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
put you code here if i can i'll modify it.
 
Priya Sri
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


reply
    Bookmark Topic Watch Topic
  • New Topic