Top Description Inners Fields Constructors Methods
org.eclipse.jetty.io

public abstract Class CyclicTimeout

extends Object
implements Destroyable
Class Inheritance
All Implemented Interfaces
org.eclipse.jetty.util.component.Destroyable
Known Direct Subclasses
org.eclipse.jetty.io.CyclicTimeouts.Timeouts
Imports
java.util.concurrent.TimeUnit, java.util.concurrent.atomic.AtomicReference, org.eclipse.jetty.util.NanoTime, org.eclipse.jetty.util.component.Destroyable, org.eclipse.jetty.util.thread.Scheduler, org.slf4j.Logger, .LoggerFactory

An abstract implementation of a timeout.

Subclasses should implement onTimeoutExpired().

This implementation is optimised assuming that the timeout will mostly be cancelled and then reused with a similar value.

The typical scenario to use this class is when you have events that postpone (by re-scheduling), or cancel then re-schedule, a timeout for a single entity. For example: connection idleness, where for each connection there is a CyclicTimeout and a read/write postpones the timeout; when the timeout expires, the implementation checks against a timestamp if the connection is really idle. Another example: HTTP session expiration, where for each HTTP session there is a CyclicTimeout and at the beginning of the request processing the timeout is canceled (via cancel()), but at the end of the request processing the timeout is re-scheduled.

This implementation has a Timeout holding the time at which the scheduled task should fire, and a linked list of Wakeup, each holding the actual scheduled task.

Calling schedule(long, TimeUnit) the first time will create a Timeout with an associated Wakeup and submit a task to the scheduler. Calling schedule(long, TimeUnit) again with the same or a larger delay will cancel the previous Timeout, but keep the previous Wakeup without submitting a new task to the scheduler, therefore reducing the pressure on the scheduler and avoid it becomes a bottleneck. When the Wakeup task fires, it will see that the Timeout is now in the future and will attach a new Wakeup with the future time to the Timeout, and submit a scheduler task for the new Wakeup.

See Also
CyclicTimeouts

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
CyclicTimeout.Timeout

A timeout time with a link to a Wakeup chain.

private class
CyclicTimeout.Wakeup

A Wakeup chain of real scheduler tasks.

Field Summary

Modifier and TypeField and Description
private final Scheduler
private final AtomicReference<CyclicTimeout.Timeout>
private static final Scheduler.Task
private static final Logger
private static final CyclicTimeout.Timeout

Constructor Summary

AccessConstructor and Description
public
CyclicTimeout(Scheduler
A scheduler used to schedule wakeups
scheduler
)

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if this CyclicTimeout was scheduled to expire
cancel
()

Cancels this CyclicTimeout so that it won't expire.

public void
destroy()

Implements org.eclipse.jetty.util.component.Destroyable.destroy.

Destroys this CyclicTimeout.

public Scheduler
public abstract void
onTimeoutExpired()

Invoked when the timeout expires.

public boolean

Returns:

true if the timeout was already set.
schedule
(long
The period of time before the timeout expires.
delay
,
TimeUnit
The unit of time of the period.
units
)

Schedules a timeout, even if already set, cancelled or expired.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_schedulerback to summary
private final Scheduler _scheduler
_timeoutback to summary
private final AtomicReference<CyclicTimeout.Timeout> _timeout
DESTROYEDback to summary
private static final Scheduler.Task DESTROYED
LOGback to summary
private static final Logger LOG
NOT_SETback to summary
private static final CyclicTimeout.Timeout NOT_SET

Constructor Detail

CyclicTimeoutback to summary
public CyclicTimeout(Scheduler scheduler)
Parameters
scheduler:Scheduler

A scheduler used to schedule wakeups

Method Detail

cancelback to summary
public boolean cancel()

Cancels this CyclicTimeout so that it won't expire.

After being cancelled, this CyclicTimeout can be scheduled again.

Returns:boolean

true if this CyclicTimeout was scheduled to expire

See Also
destroy()
destroyback to summary
public void destroy()

Implements org.eclipse.jetty.util.component.Destroyable.destroy.

Destroys this CyclicTimeout.

After being destroyed, this CyclicTimeout is not used anymore.

Annotations
@Override
getSchedulerback to summary
public Scheduler getScheduler()
onTimeoutExpiredback to summary
public abstract void onTimeoutExpired()

Invoked when the timeout expires.

scheduleback to summary
public boolean schedule(long delay, TimeUnit units)

Schedules a timeout, even if already set, cancelled or expired.

If a timeout is already set, it will be cancelled and replaced by the new one.

Parameters
delay:long

The period of time before the timeout expires.

units:TimeUnit

The unit of time of the period.

Returns:boolean

true if the timeout was already set.

org.eclipse.jetty.io back to summary

private Class CyclicTimeout.Timeout

extends Object
Class Inheritance

A timeout time with a link to a Wakeup chain.

Field Summary

Modifier and TypeField and Description
private final long
private final CyclicTimeout.Wakeup

Constructor Summary

AccessConstructor and Description
private
Timeout(long timeoutAt, CyclicTimeout.Wakeup wakeup)

Method Summary

Modifier and TypeMethod and Description
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

_atback to summary
private final long _at
_wakeupback to summary
private final CyclicTimeout.Wakeup _wakeup

Constructor Detail

Timeoutback to summary
private Timeout(long timeoutAt, CyclicTimeout.Wakeup wakeup)

Method Detail

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
org.eclipse.jetty.io back to summary

private Class CyclicTimeout.Wakeup

extends Object
implements Runnable
Class Inheritance
All Implemented Interfaces
java.lang.Runnable

A Wakeup chain of real scheduler tasks.

Field Summary

Modifier and TypeField and Description
private final long
private final CyclicTimeout.Wakeup
private final AtomicReference<Scheduler.Task>

Constructor Summary

AccessConstructor and Description
private
Wakeup(long wakeupAt, CyclicTimeout.Wakeup next)

Method Summary

Modifier and TypeMethod and Description
private void
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.
private void
schedule(long now)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

_atback to summary
private final long _at
_nextback to summary
private final CyclicTimeout.Wakeup _next
_taskback to summary
private final AtomicReference<Scheduler.Task> _task

Constructor Detail

Wakeupback to summary
private Wakeup(long wakeupAt, CyclicTimeout.Wakeup next)

Method Detail

destroyback to summary
private void destroy()
runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override
scheduleback to summary
private void schedule(long now)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override