Made isOpen() public to avoid clashes with AbstractEndPoint.
This commit is contained in:
parent
e1a31f468e
commit
b66ec3d57e
|
@ -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,10 +114,10 @@ public abstract class IdleTimeout
|
||||||
|
|
||||||
public void onOpen()
|
public void onOpen()
|
||||||
{
|
{
|
||||||
if (_idleTimeout>0)
|
if (_idleTimeout > 0)
|
||||||
_idleTask.run();
|
_idleTask.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onClose()
|
public void onClose()
|
||||||
{
|
{
|
||||||
Scheduler.Task oldTimeout = _timeout.getAndSet(null);
|
Scheduler.Task oldTimeout = _timeout.getAndSet(null);
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
@ -32,10 +31,10 @@ public class IdleTimeoutTest
|
||||||
{
|
{
|
||||||
volatile boolean _open;
|
volatile boolean _open;
|
||||||
volatile TimeoutException _expired;
|
volatile TimeoutException _expired;
|
||||||
|
|
||||||
TimerScheduler _timer;
|
TimerScheduler _timer;
|
||||||
IdleTimeout _timeout;
|
IdleTimeout _timeout;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception
|
public void setUp() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -50,9 +49,9 @@ public class IdleTimeoutTest
|
||||||
{
|
{
|
||||||
_expired=timeout;
|
_expired=timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean isOpen()
|
public boolean isOpen()
|
||||||
{
|
{
|
||||||
return _open;
|
return _open;
|
||||||
}
|
}
|
||||||
|
@ -65,7 +64,7 @@ public class IdleTimeoutTest
|
||||||
{
|
{
|
||||||
_open=false;
|
_open=false;
|
||||||
_timer.stop();
|
_timer.stop();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -76,10 +75,10 @@ public class IdleTimeoutTest
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
_timeout.notIdle();
|
_timeout.notIdle();
|
||||||
}
|
}
|
||||||
|
|
||||||
Assert.assertNull(_expired);
|
Assert.assertNull(_expired);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIdle() throws Exception
|
public void testIdle() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -91,7 +90,7 @@ public class IdleTimeoutTest
|
||||||
Thread.sleep(1500);
|
Thread.sleep(1500);
|
||||||
Assert.assertNotNull(_expired);
|
Assert.assertNotNull(_expired);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testClose() throws Exception
|
public void testClose() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -104,7 +103,7 @@ public class IdleTimeoutTest
|
||||||
Thread.sleep(1500);
|
Thread.sleep(1500);
|
||||||
Assert.assertNull(_expired);
|
Assert.assertNull(_expired);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testClosed() throws Exception
|
public void testClosed() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -153,7 +152,7 @@ public class IdleTimeoutTest
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
Assert.assertNotNull(_expired);
|
Assert.assertNotNull(_expired);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue