User: jules_gosnell
  Date: 02/02/13 17:03:17

  Modified:    jetty/src/main/org/jboss/jetty/util
                        AbstractTimeOutManager.java
                        NaiveTimeOutManager.java
  Log:
  a bit more work on timers etc..
  comment out Mortbay Logger MBean - it's causing trouble
  
  Revision  Changes    Path
  1.4       +1 -4      
contrib/jetty/src/main/org/jboss/jetty/util/AbstractTimeOutManager.java
  
  Index: AbstractTimeOutManager.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/contrib/jetty/src/main/org/jboss/jetty/util/AbstractTimeOutManager.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- AbstractTimeOutManager.java       1 Feb 2002 19:12:03 -0000       1.3
  +++ AbstractTimeOutManager.java       14 Feb 2002 01:03:16 -0000      1.4
  @@ -5,7 +5,7 @@
    * See terms of license at gnu.org.
    */
   
  -// $Id: AbstractTimeOutManager.java,v 1.3 2002/02/01 19:12:03 jules_gosnell Exp $
  +// $Id: AbstractTimeOutManager.java,v 1.4 2002/02/14 01:03:16 jules_gosnell Exp $
   
   //------------------------------------------------------------------------------
   
  @@ -19,9 +19,6 @@
     public void register(Object object, long now, long maxInactiveInterval);
     public void reregister(Object object, long now, long maxInactiveInterval);
     public void deregister(Object object);
  -
  -  public void start();
  -  public void stop();
   
     public interface
       TimeOutHelper
  
  
  
  1.7       +72 -57    
contrib/jetty/src/main/org/jboss/jetty/util/NaiveTimeOutManager.java
  
  Index: NaiveTimeOutManager.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/contrib/jetty/src/main/org/jboss/jetty/util/NaiveTimeOutManager.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- NaiveTimeOutManager.java  13 Feb 2002 01:03:06 -0000      1.6
  +++ NaiveTimeOutManager.java  14 Feb 2002 01:03:17 -0000      1.7
  @@ -5,7 +5,7 @@
    * See terms of license at gnu.org.
    */
   
  -// $Id: NaiveTimeOutManager.java,v 1.6 2002/02/13 01:03:06 jules_gosnell Exp $
  +// $Id: NaiveTimeOutManager.java,v 1.7 2002/02/14 01:03:17 jules_gosnell Exp $
   
   //------------------------------------------------------------------------------
   
  @@ -16,6 +16,13 @@
   import java.util.Iterator;
   import java.util.LinkedList;
   import java.util.List;
  +import org.jboss.logging.Logger;
  +
  +//------------------------------------------------------------------------------
  +
  +// This needn't be so Naive. If it tried to keep a handle on which was
  +// the next entry to timeout it could simply sleep until that time,
  +// instead of a set period. This might save some processing time...
   
   //------------------------------------------------------------------------------
   
  @@ -24,9 +31,12 @@
     NaiveTimeOutManager
     implements AbstractTimeOutManager
   {
  -  final List            _entries=new LinkedList();
  -  final long            _interval;
  -  final TimeOutHelper   _helper;
  +  protected final Logger        _log    =Logger.getLogger(getClass().getName());
  +  protected final List          _entries=new LinkedList();
  +  protected final long          _interval;
  +  protected final TimeOutHelper _helper;
  +  protected Sweeper             _sweeper;
  +
   
     public
       NaiveTimeOutManager(long interval, TimeOutHelper helper)
  @@ -61,6 +71,29 @@
       }
     }
   
  +  class Sweeper
  +    extends Thread
  +  {
  +    volatile boolean _running=false;
  +
  +    public void
  +      run()
  +    {
  +      _log.info("background thread started: "+NaiveTimeOutManager.this);
  +      while (_running)
  +      {
  +     try
  +     {
  +       sleep(_interval);
  +       sweep();
  +     }
  +     catch (InterruptedException e)
  +     {}
  +      }
  +      _log.info("background thread ended: "+NaiveTimeOutManager.this);
  +    }
  +  }
  +
     public synchronized void
       register(Object object, long timeRegistered, long maxInactiveInterval)
       {
  @@ -70,31 +103,46 @@
         Entry entry=new Entry(object, timeRegistered, maxInactiveInterval);
   
         synchronized (_entries) {
  +     // start sweeping if this is the first to go into the list...
  +     if (_entries.size()==0)
  +     {
  +       _sweeper=new Sweeper(); // can we reuse this thread ? - TODO
  +       _sweeper._running=true;
  +       _sweeper.start();
  +     }
  +
        _entries.add(entry);
         }
       }
   
     public synchronized void
  -    reregister(Object object, long timeRegistered, long maxInactiveInterval)
  -    {
  -      // could be optimised - but this is simpler for the moment...
  -      deregister(object);
  -      register(object, timeRegistered, maxInactiveInterval);
  -    }
  -
  -  public synchronized void
       deregister(Object object)
       {
         Entry p=find(object);
   
         synchronized (_entries) {
        _entries.remove(p);
  +
  +     // stop sweeping if this is the last to come out of the list...
  +     if (_entries.size()==0)
  +     {
  +       _sweeper._running=false;
  +       _sweeper=null;        // cut it loose to finish by itself - dangerous ? - 
TODO
  +     }
         }
       }
   
  +  public synchronized void
  +    reregister(Object object, long timeRegistered, long maxInactiveInterval)
  +    {
  +      // could be optimised - but this is simpler for the moment...
  +      deregister(object);
  +      register(object, timeRegistered, maxInactiveInterval);
  +    }
  +
     // utils...
   
  -  Entry
  +  protected Entry
       find(Object object)
       {
         Iterator i=_entries.iterator();
  @@ -107,52 +155,21 @@
         return null;
       }
   
  -  volatile boolean _loop=true;
  -
  -  protected int _started=0;
  -
  -  public synchronized void
  -    start()
  -    {
  -      if (_started<1)
  -      {
  -     new Thread() {
  -         public void
  -           run()
  -         {
  -           while (_loop)
  -           {
  -             try
  -             {
  -               sleep(_interval);
  -               sweep();
  -             }
  -             catch (InterruptedException e)
  -             {}
  -           }
  -         }
  -       }.start();
  -      }
  -      _started++;
  -    }
  -
  -  public synchronized void
  -    stop()
  -    {
  -      _started--;
  -      if (_started<1)
  -     _loop=false;
  -    }
  -
  -  void
  +  protected void
       sweep()
       {
         List copy;
         synchronized (_entries) {copy=new LinkedList(_entries);};
   
  -      long now=System.currentTimeMillis();
  +      // what happens if we take our copy, then one of the entries
  +      // deregisters from the original list. We may then reregister()
  +      // it with the original list if decide to alter it's timeout
  +      // period... The entry will remain in the list, although it is
  +      // assumed that it was removed. Because the list has an entry it
  +      // will carry on waking up and running for ever - even if cut
  +      // loose.
   
  -      //System.out.println("Sweeping...");
  +      long now=System.currentTimeMillis();
   
         Iterator i=copy.iterator();
         while (i.hasNext())
  @@ -166,11 +183,9 @@
          }
          catch (Exception e)
          {
  -         System.err.println("Problem notifying: "+p._object);
  -         e.printStackTrace();        // TODO
  +         _log.error("problem notifying: "+p._object, e);
          }
        }
         }
  -      //System.out.println("Sweeping...done");
  -   }
  +    }
   }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to