Package com.oorian

Class OorianTimer

java.lang.Object
com.oorian.OorianTimer

public class OorianTimer extends Object
A session-aware server-side timer that fires TimerEvent instances on a schedule.

OorianTimer uses a ScheduledExecutorService internally for scheduling and integrates with OorianSession so that timer callbacks have full access to the session context, allowing them to modify elements and call sendUpdate().

Repeating timer:


 OorianTimer timer = new OorianTimer(5000); // 5 second interval
 timer.registerListener(this);
 timer.start();
 

Repeating timer with initial delay:


 OorianTimer timer = new OorianTimer(5000);
 timer.setInitialDelay(2000); // wait 2 seconds before first tick
 timer.registerListener(this);
 timer.start();
 

One-shot timer:


 OorianTimer timer = new OorianTimer(3000, false); // fires once after 3 seconds
 timer.registerListener(this);
 timer.start();
 

Timers are automatically stopped when their session is invalidated or destroyed. For page-scoped timers, stop the timer in onUnloaded() as a best practice.

Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • OorianTimer

      public OorianTimer(long interval)
      Creates a new repeating timer with the specified interval.
      Parameters:
      interval - the interval between ticks in milliseconds
    • OorianTimer

      public OorianTimer(long interval, boolean repeating)
      Creates a new timer with the specified interval and repeat behavior.
      Parameters:
      interval - the interval in milliseconds (for repeating timers) or delay (for one-shot timers)
      repeating - true for a repeating timer, false for a one-shot timer
  • Method Details

    • setInitialDelay

      public void setInitialDelay(long initialDelay)
      Sets the initial delay before the first tick.

      If not set, the default is 0 for repeating timers (fires immediately) and interval for one-shot timers.

      Parameters:
      initialDelay - the initial delay in milliseconds
    • registerListener

      public void registerListener(TimerListener listener)
      Registers a listener to receive TimerEvent notifications.
      Parameters:
      listener - the listener to register
    • start

      public void start()
      Starts the timer.

      Captures the current OorianSession and registers this timer with the session for lifecycle management. The timer begins scheduling ticks according to its configuration.

      For repeating timers, ticks fire at a fixed rate using ScheduledExecutorService.scheduleAtFixedRate(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit). For one-shot timers, a single tick is scheduled using ScheduledExecutorService.schedule(java.lang.Runnable, long, java.util.concurrent.TimeUnit).

    • stop

      public void stop()
      Stops the timer.

      Cancels any pending or repeating ticks and shuts down the executor. A stopped timer cannot be restarted.

    • getInterval

      public long getInterval()
      Returns the interval in milliseconds.
      Returns:
      the timer interval
    • isRepeating

      public boolean isRepeating()
      Returns whether this timer repeats.
      Returns:
      true if this is a repeating timer, false for one-shot
    • getTickCount

      public int getTickCount()
      Returns the number of times this timer has fired.
      Returns:
      the tick count