It is'nt enough to override the run() method. You should create a Thread
object on you runnable servlet, using the MyThread(Runnable) constructor.
Then, when you're ready, you shall call the method MyThread.start(). Your
Thread, when started, will ask to the Runnable it's built on to execute the
run() method.
IMPORTANT: when you want to stop the servlet running, you should set the
MyThread object to null, using a code like this:

Thread MyThread;

/** start running */
public void start() {
    MyThread=new Thread(this);
    MyThread.start();
}

/** stops running */
public void stop() {
    MyThread=null;
}


[Andrea Stocco]

DREAMWARE S.r.l.
Multimedia Productions
----------------------------------------
Via Pascoli 19 - 19125 La Spezia - Italy
Tel. ++39 0187 500968   Fax  ++39 0187 516388

Via Caneva 56 - 33013 Gemona del Friuli, Udine - Italy
Tel. ++39 0432 971811   Fax  ++39 0432 971910

----- Original Message -----
From: Arun Jayaprakash <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, January 08, 2001 12:40 PM
Subject: Can servlets implements the Runnable interface


> Hello,
>
> I have a servlet that traverses a hashtable that
> contain user logins mapped to their session objects.
> Every hour, I want this servlet to run and clear those
> user logins whose session objects have not changed in
> the last half hour. I wrote the following code, but I
> config.getServletContext is returning a null object.
> Have I called the run() method correctly? If not, is
> there any other way to call this servlet every hour?
>
> ********* CODE BEGINS HERE ************
> import java.io.*;
> import java.lang.*;
> import java.util.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
>
> public class ClearSessions extends HttpServlet
> implements Runnable
> {
> private ServletContext context;
> private boolean upAndRunning;
>
> public void init(ServletConfig config)
> throws ServletException
> {
>    context = config.getServletContext();
>    upAndRunning = false;
> }
>
> public void doGet(HttpServletRequest request,
> HttpServletResponse response) throws IOException,
> ServletException
> {
>  if (!upAndRunning)
>  {
>   upAndRunning = true;
>   (new ClearSessions()).run();
>  }
> }
>
> public void run()
> {
> Hashtable users = (Hashtable)
> context.getAttribute"users");
>
> for (Enumeration e = users.keys();
> e.hasMoreElements();)
>  {
>  String login = (String)e.nextElement();
>  HttpSession session = (HttpSession)users.get(login);
>  Date date = new Date();
>
>  long gap = date.getTime() -
> session.getLastAccessedTime();
>
>  if (gap > (1000*1800)) //Half an hour
>   {
>   users.remove(login);
>
>   DBA dba = (DBA)session.getValue("dba");
>   dba.close();
>   dba = null;
>
>   session.invalidate();
>   }
>  }
>
>  try
>  {
>   Thread.sleep(30); // Sleep for one hour
>  }
>  catch(InterruptedException ie)
>  {
>  }
> }
> }
>
> ********* CODE ENDS HERE ************
>
> The DBA object is a database object that is
> instatiated everytime a user logs into the website. I
> have written a close() method that closes the
> connection object and sets the statement objects to
> null.
>
> Thanks and bye,
> Arun Jayaprakash.
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Photos - Share your holiday photos online!
> http://photos.yahoo.com/
>
>
___________________________________________________________________________
> 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
>

___________________________________________________________________________
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