Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Background Operation 2

Status
Not open for further replies.

SON1

Programmer
Nov 21, 2004
51
CY
I am building a webapp, and i would like to have a background class, which i would like it to be alive for the duration when the webapp is UP.It also has to start when the webapp starts)

any ideas how do i go and do that ?

 
One, probably simplest way, is to have a servlet, and in the init() method, spawn a class that extends Thread.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
dont i have to access this servlet to start ?
 
No, the init() method is called on webapp startup as long as you add something like this in your webapp's web.xml :

Code:
	<servlet>
		<servlet-name>MyServlet</servlet-name>
		<servlet-class>com.acme.MyServlet</servlet-class>                   
		<load-on-startup>1</load-on-startup>	
	</servlet>

have a read of the API specs - it will become clearer.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I whould have probably mentioned that i want the servlet to constantly access the database on the background in an infinite loop. Is this making any sense ?

public class BackgroundOperation extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

while (TRUE) {
query database
WAIT 15 seconds
}



}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
public void init()
{
Thread BO = new Thread(this);
}


}
 
Try something like :-
Code:
public class BackgroundServlet extends HttpServlet implements Runnable {
  private boolean isAlive = true;

  public void init() throws ServletException {
    new Thread(this).start();
  }

  public void destroy(){
    isAlive = false;
  }

  public void run(){
    while ( isAlive ){
      //query database
      try {
        Thread.sleep(15000);
      } catch ( InterruptedException ex) ){
      }
    }
  }
}

I don't have time to try this out for real, so it may need some tinkering.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
aaa nice :) this works like a charm !
 
Here comes the diffuclt bit. This servlet just after querying the database and before waiting 15 seconds, it needs to open a website address (irrelevant to the webapplication). Anyone know how do i go do this ? I was thinking maybe forwarding a THE url to another servlet or something, but then again i am not sure how to open an external url within a servlet
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top