Module kivakit.core

Class BaseMessageAlarm

java.lang.Object
com.telenav.kivakit.core.messaging.alarms.BaseMessageAlarm
All Implemented Interfaces:
MessageAlarm, Listener, Transceiver, Receiver, Transmitter, NamedObject
Direct Known Subclasses:
EmailMessageAlarm

public abstract class BaseMessageAlarm extends Object implements MessageAlarm
Base class for implementing message alarms.

Installing a Message Alarm

A MessageAlarm is a Listener which can listen to messages from any Broadcaster or Repeater when installed with Listener.listenTo(Broadcaster). For example:

 public void onRun()
 {
     // Send alarm email if this application exceeds 10 errors per minute
     new EmailAlarm(...).listenTo(this);
 }

The installed alarm will be triggered when the error rate() exceeds triggerRate(), which defaults to 10 errors per minute. This default value can be overridden with triggerRate(Rate). The maximum alarm trigger frequency defaults to once every 30 minutes. This value can be overridden with maximumTriggerFrequency(Frequency).

Implementing an Alarm

To implement an alarm, override onTrigger(Rate) and implement the alarm action. By default, the alarm will be triggered when shouldTrigger() returns true. By default, shouldTrigger() returns true if the current error rate() exceeds triggerRate(). The current error rate() is computed with a RateCalculator that automatically resets once a minute. Messages are categorized as errors (or not) by isError(Message), which returns true if the message status is worse-than-or-equal-to Problem by default.

Author:
jonathanl (shibo)
  • Constructor Details

    • BaseMessageAlarm

      public BaseMessageAlarm()
  • Method Details

    • maximumTriggerFrequency

      public BaseMessageAlarm maximumTriggerFrequency(Frequency maximumTriggerFrequency)
      Sets the maximum frequency at which this alarm can be triggered
    • onMessage

      public final void onMessage(Message message)
      Functional interface method called when a message is received by this listener
      Specified by:
      onMessage in interface Listener
      Parameters:
      message - The message
    • shouldTrigger

      public boolean shouldTrigger()
      Called to determine if this alarm should be triggered
      Specified by:
      shouldTrigger in interface MessageAlarm
      Returns:
      True if this alarm should be triggered
    • trigger

      public final void trigger(Rate rate)
      Executes the action for this message alarm. The action that takes place depends on the implementation. For example, the alarm might send an email or text message, or it might write information to an incident log.
      Specified by:
      trigger in interface MessageAlarm
      Parameters:
      rate - The rate of errors that triggered this alarm
    • triggerRate

      public BaseMessageAlarm triggerRate(Rate triggerRate)
      Sets the error rate above which this alarm will be triggered
      Parameters:
      triggerRate - The rate of error messages at which this alarm will be triggered
    • triggerRate

      public Rate triggerRate()
      Returns:
      The rate above which this alarm will be triggered
    • isError

      protected boolean isError(Message message)
      Returns true if the given message should count towards setting off this alarm
      Parameters:
      message - The message
      Returns:
      True if the message is a problem that should be counted
    • onTrigger

      protected abstract void onTrigger(Rate rate)
      Alarm action implementation
      Parameters:
      rate - The error rate that triggered this alarm
    • rate

      protected Rate rate()
      Returns:
      The current rate of error messages