public class Sweeper extends AbstractLifeCycle implements java.lang.Runnable
A utility class to perform periodic sweeping of resources.
Sweeper.Sweepable resources may be added to or removed from a
Sweeper and the resource implementation decides whether
it should be swept or not.
If a Sweeper.Sweepable resources is itself a container of
other sweepable resources, it will forward the sweep operation
to children resources, and so on recursively.
Typical usage is to add Sweeper as a bean to an existing
container:
Server server = new Server(); server.addBean(new Sweeper(), true); server.start();Code that knows it has sweepable resources can then lookup the
Sweeper and offer the sweepable resources to it:
class MyComponent implements Sweeper.Sweepable
{
private final long creation;
private volatile destroyed;
MyComponent(Server server)
{
this.creation = System.nanoTime();
Sweeper sweeper = server.getBean(Sweeper.class);
sweeper.offer(this);
}
void destroy()
{
destroyed = true;
}
@Override
public boolean sweep()
{
return destroyed;
}
}
| Modifier and Type | Class and Description |
|---|---|
static interface |
Sweeper.Sweepable
A
Sweeper.Sweepable resource implements this interface to
signal to a Sweeper or to a parent container if it
needs to be swept or not. |
AbstractLifeCycle.AbstractLifeCycleListenerLifeCycle.Listener| Modifier and Type | Field and Description |
|---|---|
private java.util.concurrent.atomic.AtomicReference<java.util.List<Sweeper.Sweepable>> |
items |
private static Logger |
LOG |
private long |
period |
private Scheduler |
scheduler |
private java.util.concurrent.atomic.AtomicReference<Scheduler.Task> |
task |
FAILED, RUNNING, STARTED, STARTING, STOP_ON_FAILURE, STOPPED, STOPPING| Constructor and Description |
|---|
Sweeper(Scheduler scheduler,
long period) |
| Modifier and Type | Method and Description |
|---|---|
private void |
activate() |
private void |
deactivate() |
protected void |
doStart() |
protected void |
doStop() |
int |
getSize() |
boolean |
offer(Sweeper.Sweepable sweepable) |
boolean |
remove(Sweeper.Sweepable sweepable) |
void |
run() |
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, setStopTimeout, start, stopprivate static final Logger LOG
private final java.util.concurrent.atomic.AtomicReference<java.util.List<Sweeper.Sweepable>> items
private final java.util.concurrent.atomic.AtomicReference<Scheduler.Task> task
private final Scheduler scheduler
private final long period
public Sweeper(Scheduler scheduler, long period)
protected void doStart()
throws java.lang.Exception
doStart in class AbstractLifeCyclejava.lang.Exceptionprotected void doStop()
throws java.lang.Exception
doStop in class AbstractLifeCyclejava.lang.Exceptionpublic int getSize()
public boolean offer(Sweeper.Sweepable sweepable)
public boolean remove(Sweeper.Sweepable sweepable)
public void run()
run in interface java.lang.Runnableprivate void activate()
private void deactivate()