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.
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.
      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