I feel the logic is wrong if you are trying to clear sessions every 30
minutes .
Try this:
1) Create a class which implements runnable
2) Write ur clearing sessions logic into the run method
3) The logic should run as infinite loop each loop stoping for 30 minutes
4)Start the thread u have created in the init() method of ur servlet
5) in destroy() method kill ur thead( i mean stop it)

Hope this will help u
Shanki
----- Original Message -----
From: Arun Jayaprakash <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, January 08, 2001 5:10 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)) file://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