Class SessionKeepAliveDaemon

java.lang.Object
java.lang.Thread
org.tentackle.task.DefaultTaskDispatcher
org.tentackle.session.SessionKeepAliveDaemon
All Implemented Interfaces:
Runnable, Killable, Supervisable, Terminatable, TaskDispatcher

public class SessionKeepAliveDaemon extends DefaultTaskDispatcher
A task dispatcher to keep sessions alive.

Each session with a keep-alive interval gets its own SessionKeepAliveTask, repeated at that interval, but not more often than the minimum alive-interval. A negative interval requests a non-blocking close of the session.

Usually not used directly, but via SessionUtilities.keepAliveIntervalChanged(Session).

Author:
harald
  • Constructor Details

    • SessionKeepAliveDaemon

      public SessionKeepAliveDaemon(long minAliveInterval)
      Creates the daemon.
      Parameters:
      minAliveInterval - the minimum alive-interval in ms
  • Method Details

    • cleanup

      protected void cleanup()
      Description copied from class: DefaultTaskDispatcher
      Does any necessary cleanup after the dispatcher thread has been terminated.

      Invoked by the dispatcher thread when it terminates, and by the thread invoking DefaultTaskDispatcher.terminate(). If the dispatcher thread did not terminate in time, it is marked killed and still running while the terminating thread cleans up. Resources the dispatcher thread may still be using must not be cleaned up then, see DefaultTaskDispatcher.isCleanupDeferred().

      Overrides:
      cleanup in class DefaultTaskDispatcher
    • run

      public void run()
      Specified by:
      run in interface Runnable
      Overrides:
      run in class DefaultTaskDispatcher
    • keepAliveIntervalChanged

      public void keepAliveIntervalChanged(Session session)
      Handles the change of a keep alive-interval of a session.
      Parameters:
      session - the session
    • removeAliveTask

      public void removeAliveTask(Session session)
      Removes the task for the given session.
      Parameters:
      session - the session
    • removeAliveTask

      public boolean removeAliveTask(Session session, Task task)
      Removes the given task for the given session.

      Unlike removeAliveTask(Session) the task is removed only if it still is the session's current task. This is what a running SessionKeepAliveTask must use to unregister itself: another thread may have replaced it via keepAliveIntervalChanged(Session) in the meantime, and removing by session alone would drop that replacement and leave the session without a keep-alive.

      Deliberately not synchronized on the daemon's alive-lock: the invocation comes from the daemon thread while executing the task, and the compare-and-remove is atomic in itself.

      Parameters:
      session - the session
      task - the task requesting its own removal
      Returns:
      true if removed, false if the task no longer is the session's current task
    • getKeepAliveSessions

      public Collection<Session> getKeepAliveSessions()
      Gets the sessions currently kept alive.
      Useful to hand the registrations over to another daemon, should this one have died.
      Returns:
      a snapshot of the sessions, never null
    • getExecutorService

      public ExecutorService getExecutorService()
      Gets the executor service.
      Returns:
      the executor service
    • createExecutorService

      protected ExecutorService createExecutorService()
      Creates the executor service.
      Returns:
      the executor service
    • createAliveTask

      protected Task createAliveTask(Session session, long interval)
      Creates a keep-alive task.
      Parameters:
      session - the session
      interval - the keep-alive interval in milliseconds
      Returns:
      the created task