groovyx.gpars.dataflow.operator.component
Class GracefulShutdownMonitor

java.lang.Object
  extended by groovyx.gpars.dataflow.operator.component.GracefulShutdownMonitor
All Implemented Interfaces:
OperatorStateMonitor

public final class GracefulShutdownMonitor
extends java.lang.Object
implements OperatorStateMonitor

The monitor will orchestrate a graceful shutdown, when its shutdownNetwork() method is called. For this mechanism to work, all selectors/operators in the network have to have their own instance of GracefulShutdownListener added and these listeners have to share the same GracefulShutdownMonitor instance. Each listener observes the activity in the input channels of its corresponding operator/selector. Upon graceful shutdown initialization, the listeners also start watching the state of their operators/selectors. The GracefulShutdownMonitor then repeatedly watches, whether listeners report activity in the operators. When the activity ceases, the monitor will poll all listeners about the state of their operator/selector and its input channels. If all listeners report no activity and no incoming messages, the monitor can safely terminate all operators.

Author:
Vaclav Pech

Field Summary
private static long DEFAULT_DELAY
           
private  long delay
           
private  java.util.Collection<GracefulShutdownListener> listeners
          All listeners that watch the operators/selectors in the network
private  java.util.concurrent.atomic.AtomicBoolean notificationArrivedFlag
          Indicates, whether some activity has been reported since last time
private  DataflowVariable<java.lang.Boolean> result
          The final latch that indicates the network has been shutdown
private  boolean shutdownFlag
          Indicates whether shutdown has been initialized
private  java.util.Timer timer
          A times used to repeatedly poll the state of the network
 
Constructor Summary
GracefulShutdownMonitor()
          Uses the default timer delay
GracefulShutdownMonitor(long delay)
          Allows to use a customized delay
 
Method Summary
private  boolean checkWhetherAnyEventsHaveArrived()
          Checks the notification flag whether there has been some activity since last timer run.
(package private)  void onTimer()
          Checks, whether the network is no longer active.
 void registerProcessorListener(GracefulShutdownListener listener)
          Invoked by GracefulShutdownListeners, which listen on operators/selectors for lifecycle events, to get registered with the monitor.
 Promise<java.lang.Boolean> shutdownNetwork()
          Initializes the shutdown process.
private  void schedule()
          Clears the notification flag so as we know whether some activity happens between subsequent checks.
 void stateChanged()
          Invoked whenever a processor changes state
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_DELAY

private static final long DEFAULT_DELAY
See Also:
Constant Field Values

delay

private final long delay

shutdownFlag

private boolean shutdownFlag
Indicates whether shutdown has been initialized


notificationArrivedFlag

private final java.util.concurrent.atomic.AtomicBoolean notificationArrivedFlag
Indicates, whether some activity has been reported since last time


result

private final DataflowVariable<java.lang.Boolean> result
The final latch that indicates the network has been shutdown


listeners

private final java.util.Collection<GracefulShutdownListener> listeners
All listeners that watch the operators/selectors in the network


timer

private final java.util.Timer timer
A times used to repeatedly poll the state of the network

Constructor Detail

GracefulShutdownMonitor

public GracefulShutdownMonitor()
Uses the default timer delay


GracefulShutdownMonitor

public GracefulShutdownMonitor(long delay)
Allows to use a customized delay

Parameters:
delay - A timeout in milliseconds to wait between two subsequent polls on processors' state. Lower values will reduce the wait time for network shutdown, but maz have impact on the performance when the shutdown process gets initialized.
Method Detail

stateChanged

public void stateChanged()
Invoked whenever a processor changes state

Specified by:
stateChanged in interface OperatorStateMonitor

registerProcessorListener

public void registerProcessorListener(GracefulShutdownListener listener)
Invoked by GracefulShutdownListeners, which listen on operators/selectors for lifecycle events, to get registered with the monitor. The monitor will query these registered listeners about their respective operator/selector state when performing graceful shutdown.

Specified by:
registerProcessorListener in interface OperatorStateMonitor
Parameters:
listener - The listener to register

shutdownNetwork

public Promise<java.lang.Boolean> shutdownNetwork()
Initializes the shutdown process. New listeners cannot be registered after this point. New messages should not enter the dataflow network from the outside, since this may prevent the network from terminating.

Returns:
A Promise, which may be used to wait for or get notified about the shutdown success.

onTimer

void onTimer()
Checks, whether the network is no longer active. To reduce effect on system performance, cheap criteria are being checked first.


checkWhetherAnyEventsHaveArrived

private boolean checkWhetherAnyEventsHaveArrived()
Checks the notification flag whether there has been some activity since last timer run.

Returns:
True, if events have been registered since last time, false otherwise.

schedule

private void schedule()
Clears the notification flag so as we know whether some activity happens between subsequent checks. Schedules the timer to run again after the delay set on the monitor.


Copyright © 2008–2012 Václav Pech. All Rights Reserved.