Made isOpen() public to avoid clashes with AbstractEndPoint.

This commit is contained in:
Simone Bordet 2013-03-15 15:53:35 +01:00
parent e1a31f468e
commit b66ec3d57e
2 changed files with 38 additions and 41 deletions

View File

@ -26,16 +26,13 @@ import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.thread.Scheduler; import org.eclipse.jetty.util.thread.Scheduler;
/**
* An Abstract implementation of an Idle Timeout.
/* ------------------------------------------------------------ */ * <p/>
/** An Abstract implementation of an Idle Timeout.
*
* This implementation is optimised that timeout operations are not cancelled on * This implementation is optimised that timeout operations are not cancelled on
* every operation. Rather timeout are allowed to expire and a check is then made * every operation. Rather timeout are allowed to expire and a check is then made
* to see when the last operation took place. If the idle timeout has not expired, * to see when the last operation took place. If the idle timeout has not expired,
* the timeout is rescheduled for the earliest possible time a timeout could occur. * the timeout is rescheduled for the earliest possible time a timeout could occur.
*
*/ */
public abstract class IdleTimeout public abstract class IdleTimeout
{ {
@ -43,15 +40,15 @@ public abstract class IdleTimeout
private final Scheduler _scheduler; private final Scheduler _scheduler;
private final AtomicReference<Scheduler.Task> _timeout = new AtomicReference<>(); private final AtomicReference<Scheduler.Task> _timeout = new AtomicReference<>();
private volatile long _idleTimeout; private volatile long _idleTimeout;
private volatile long _idleTimestamp=System.currentTimeMillis(); private volatile long _idleTimestamp = System.currentTimeMillis();
private final Runnable _idleTask = new Runnable() private final Runnable _idleTask = new Runnable()
{ {
@Override @Override
public void run() public void run()
{ {
long idleLeft=checkIdleTimeout(); long idleLeft = checkIdleTimeout();
if (idleLeft>=0) if (idleLeft >= 0)
scheduleIdleTimeout(idleLeft > 0 ? idleLeft : getIdleTimeout()); scheduleIdleTimeout(idleLeft > 0 ? idleLeft : getIdleTimeout());
} }
}; };
@ -61,7 +58,7 @@ public abstract class IdleTimeout
*/ */
public IdleTimeout(Scheduler scheduler) public IdleTimeout(Scheduler scheduler)
{ {
_scheduler=scheduler; _scheduler = scheduler;
} }
public long getIdleTimestamp() public long getIdleTimestamp()
@ -76,14 +73,14 @@ public abstract class IdleTimeout
public void setIdleTimeout(long idleTimeout) public void setIdleTimeout(long idleTimeout)
{ {
long old=_idleTimeout; long old = _idleTimeout;
_idleTimeout = idleTimeout; _idleTimeout = idleTimeout;
// Do we have an old timeout // Do we have an old timeout
if (old>0) if (old > 0)
{ {
// if the old was less than or equal to the new timeout, then nothing more to do // if the old was less than or equal to the new timeout, then nothing more to do
if (old<=idleTimeout) if (old <= idleTimeout)
return; return;
// old timeout is too long, so cancel it. // old timeout is too long, so cancel it.
@ -93,22 +90,22 @@ public abstract class IdleTimeout
} }
// If we have a new timeout, then check and reschedule // If we have a new timeout, then check and reschedule
if (idleTimeout>0 && isOpen()) if (idleTimeout > 0 && isOpen())
_idleTask.run(); _idleTask.run();
} }
/** This method should be called when non-idle activity has taken place. /**
* This method should be called when non-idle activity has taken place.
*/ */
public void notIdle() public void notIdle()
{ {
_idleTimestamp=System.currentTimeMillis(); _idleTimestamp = System.currentTimeMillis();
} }
private void scheduleIdleTimeout(long delay) private void scheduleIdleTimeout(long delay)
{ {
Scheduler.Task newTimeout = null; Scheduler.Task newTimeout = null;
if (isOpen() && delay > 0 && _scheduler!=null) if (isOpen() && delay > 0 && _scheduler != null)
newTimeout = _scheduler.schedule(_idleTask, delay, TimeUnit.MILLISECONDS); newTimeout = _scheduler.schedule(_idleTask, delay, TimeUnit.MILLISECONDS);
Scheduler.Task oldTimeout = _timeout.getAndSet(newTimeout); Scheduler.Task oldTimeout = _timeout.getAndSet(newTimeout);
if (oldTimeout != null) if (oldTimeout != null)
@ -117,7 +114,7 @@ public abstract class IdleTimeout
public void onOpen() public void onOpen()
{ {
if (_idleTimeout>0) if (_idleTimeout > 0)
_idleTask.run(); _idleTask.run();
} }
@ -162,22 +159,23 @@ public abstract class IdleTimeout
} }
} }
return idleLeft>=0?idleLeft:0; return idleLeft >= 0 ? idleLeft : 0;
} }
return -1; return -1;
} }
/* ------------------------------------------------------------ */ /**
/** This abstract method is called when the idle timeout has expired. * This abstract method is called when the idle timeout has expired.
*
* @param timeout a TimeoutException * @param timeout a TimeoutException
*/ */
abstract protected void onIdleExpired(TimeoutException timeout); protected abstract void onIdleExpired(TimeoutException timeout);
/**
/* ------------------------------------------------------------ */ * This abstract method should be called to check if idle timeouts
/** This abstract method should be called to check if idle timeouts
* should still be checked. * should still be checked.
*
* @return True if the entity monitored should still be checked for idle timeouts * @return True if the entity monitored should still be checked for idle timeouts
*/ */
abstract protected boolean isOpen(); public abstract boolean isOpen();
} }

View File

@ -22,7 +22,6 @@ package org.eclipse.jetty.io;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import junit.framework.Assert; import junit.framework.Assert;
import org.eclipse.jetty.util.thread.TimerScheduler; import org.eclipse.jetty.util.thread.TimerScheduler;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@ -52,7 +51,7 @@ public class IdleTimeoutTest
} }
@Override @Override
protected boolean isOpen() public boolean isOpen()
{ {
return _open; return _open;
} }