ITimerManager

The timer manager takes care of the management of the current timers. Timers in nyssr.net are messages that are sent to their target after a certain time.

Get the timer manager

The manager is fetched as a singleton from the service registry:

final ITimerManager tm = CServiceRegistry.getInstance()
                                         .getService(ITimerManager.class);

As always, make sure that the service starter of your package has a dependency on the ITimerManager to ensure that the service already exists.

Start a timer

One way to start a timer is to use the corresponding methods of the timer manager directly. The method requires

  • a record ID to identify the message
  • a target address for which target the message is to be received
  • a timeout in milliseconds (when does the message arrive)
  • a flag whether the message should be repeated
  • an optional record for payload data
long createAndStartTimer(@NotNull IId aMID,
                         @NotNull CTargetAddress aAddress,
                         long aDelay,
                         boolean aRepeat,
                         @Nullable CRecord aRecord);

The method returns a handle that can be used to identify the timer. Thus, other methods can be called to manage the timer. An invalid handle is ITimerManager.INVALID (0).

A timeout of less than 10 ms makes little sense, because the resolution of the timers does not allow this.

Be careful when using repeated timers:

If the application is at breakpoint during debugging, more timers are produced.

If the processing of a timer takes longer than the timeout, further timer messages are also sent. For short timeouts, it is usually better to use one-time timers. After processing the timer event, a new timer can then be raised.

Dismissing a timer

The timer can be stopped at any time. A timer that is not to be repeated is automatically deleted after the message is sent.

boolean dismissTimer(long aHandle);

The method returns false if the timer was not found.

Resetting a timer

A timer can be reset at any time. With this method it is stopped, but not deleted, but restarted immediately.

boolean resetTimer(long aHandle);

The method returns false if the timer was not found.

Check a timer

It can be checked whether a timer still exists.

boolean isTimerActive(long aHandle);

The method returns false if the timer was not found.

Likewise, it can be checked whether a timer is known for a given record ID and a destination address.

long getTimerHandle(@NotNull IId aMID,
                    @NotNull CTargetAddress aReceiver);

A return value of ITimerManager.INVALID (0) means that no timer was found.