G S Sundar wrote:

> Hello All,
> I'm facing problem, while creating a new thread/timer inside the Servlet init()
> method. In init method i'm loading  text file(s), and updating them in my
> servlet context. When i update/change my text file(s) i need to restart my
> servlet runner to update the file contents in Servlet Context.
> I want to update the file contents without restarting the servlet runner, for
> that i tried with Thread/Timer class. For the specified interval it will check
> the file status and update the servlet context if there is any change in file.
> Otherwise it should return to the main thread.
> But once my thread/timer start() begins it's not at all coming back to the main
> thread.
> I think my thread is running inside the main thread(INIT), and there is no
> specific condition to exit/stop my thread, bcoz it should work like demon
> process. In this how can i pass the control back to the main thread.
>
> Due to this reason init methd is not getting completed, and i'm not able to run
> my servlet. expecting help in this regard.
>
> Thanx in advance..
>
> Regards
> G S Sundaram
> [...]

Hi :-)    I did a simple testing, just as a reference :
    - make a "timer" with a helper class implementing Runnable
    - in this "timer", change a Attribute of ServletContext periodcally
      (every 10 seconds)
    - in service(...) of myServlet, for every client' requesting, print
      this  Attribute of ServletContext, so we can see the changing.

********  the following is the code: ********

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;

public class servlet_iT extends HttpServlet{ //iT=insideThread

   static byte [] ctxLocker=new byte[0];
   static ServletContext ctx;

   public void init(ServletConfig config) throws ServletException {
      super.init(config);
      synchronized(servlet_iT.ctxLocker){
         ctx=this.getServletContext();
   }

   try{
      new Thread( servlet_iT_Helper.getOnlyOneInstance(), "insideThread" ).start();

   }catch(Exception e){ System.out.println("servlet_iT_Helper, e="+e); }

      System.out.println("in init...");
   }

   public void service(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException{

      synchronized(servlet_iT.ctxLocker){
         System.out.println( "name0="+servlet_iT.ctx.getAttribute("name0") );
   }

      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("<HTML><BODY>");
      out.println("ok");
      out.println("</BODY></HTML>");
      out.close();
      //return;
   }

}


class servlet_iT_Helper implements Runnable, Serializable{
   private static servlet_iT_Helper onlyOneInstance;
   private /*static*/ int counter=0;

   private servlet_iT_Helper(){}

   public static synchronized servlet_iT_Helper getOnlyOneInstance() {
      if (onlyOneInstance == null) {
         onlyOneInstance = new servlet_iT_Helper();
      }
      return onlyOneInstance;
   }

   public void run(){
   while(true){
         counter++;
         System.out.println("in run, counter="+counter);
         synchronized(servlet_iT.ctxLocker){
            servlet_iT.ctx.setAttribute( "name0", String.valueOf(counter) );
   }
         try{
            Thread.currentThread().sleep(10000);
      }catch(Exception e){ System.out.println("servlet_iT_Helper, e="+e); }
   }
   }

}




Bo
Feb.12, 2001

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to