CounterMonitor (Java SE 26 & JDK 26)
Description: declaration: module: java.management, package: javax.management.monitor, class: CounterMonitor
Keywords: javax.management.monitor.CounterMonitor class

# Class CounterMonitor

java.lang.Object
javax.management.NotificationBroadcasterSupport
javax.management.monitor.Monitor
javax.management.monitor.CounterMonitor

All Implemented Interfaces:
  `MBeanRegistration, CounterMonitorMBean, MonitorMBean, NotificationBroadcaster, NotificationEmitter`

public class CounterMonitor extends Monitor implements CounterMonitorMBean
Defines a monitor MBean designed to observe the values of a counter attribute.

A counter monitor sends a `threshold notification` when the value of the counter reaches or exceeds a threshold known as the comparison level. The notify flag must be set to `true`.

In addition, an offset mechanism enables particular counting intervals to be detected. If the offset value is not zero, whenever the threshold is triggered by the counter value reaching a comparison level, that comparison level is incremented by the offset value. This is regarded as taking place instantaneously, that is, before the count is incremented. Thus, for each level, the threshold triggers an event notification every time the count increases by an interval equal to the offset value.

If the counter can wrap around its maximum value, the modulus needs to be specified. The modulus is the value at which the counter is reset to zero.

If the counter difference mode is used, the value of the derived gauge is calculated as the difference between the observed counter values for two successive observations. If this difference is negative, the value of the derived gauge is incremented by the value of the modulus. The derived gauge value (V[t]) is calculated using the following method:

- if (counter[t] - counter[t-GP]) is positive then V[t] = counter[t] - counter[t-GP]
- if (counter[t] - counter[t-GP]) is negative then V[t] = counter[t] - counter[t-GP] + MODULUS

This implementation of the counter monitor requires the observed attribute to be of the type integer (`Byte`, `Integer`, `Short`, `Long`).

Since:
  1.5

- ## Field Summary

  ### Fields declared in class Monitor

  `alreadyNotified, alreadyNotifieds, capacityIncrement, dbgTag, elementCount, OBSERVED_ATTRIBUTE_ERROR_NOTIFIED, OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED, OBSERVED_OBJECT_ERROR_NOTIFIED, RESET_FLAGS_ALREADY_NOTIFIED, RUNTIME_ERROR_NOTIFIED, server`
  Modifier and Type
  Field
  Description
  `protected int`
  `alreadyNotified`
  Deprecated.
  equivalent to `Monitor.alreadyNotifieds`[0].
  `protected int[]`
  `alreadyNotifieds`
  Selected monitor errors that have already been notified.
  `protected static final int`
  `capacityIncrement`
  The amount by which the capacity of the monitor arrays are automatically incremented when their size becomes greater than their capacity.
  `protected String`
  `dbgTag`
  Deprecated.
  No replacement.
  `protected int`
  `elementCount`
  The number of valid components in the vector of observed objects.
  `protected static final int`
  `OBSERVED_ATTRIBUTE_ERROR_NOTIFIED`
  Flag denoting that a notification has occurred after changing the observed attribute.
  `protected static final int`
  `OBSERVED_ATTRIBUTE_TYPE_ERROR_NOTIFIED`
  Flag denoting that a notification has occurred after changing the observed object or the observed attribute.
  `protected static final int`
  `OBSERVED_OBJECT_ERROR_NOTIFIED`
  Flag denoting that a notification has occurred after changing the observed object.
  `protected static final int`
  `RESET_FLAGS_ALREADY_NOTIFIED`
  This flag is used to reset the `alreadyNotifieds` monitor attribute.
  `protected static final int`
  `RUNTIME_ERROR_NOTIFIED`
  Flag denoting that a notification has occurred after changing the observed object or the observed attribute.
  `protected MBeanServer`
  `server`
  Reference to the MBean server.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `CounterMonitor()`
  Default constructor.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Number`
  `getDerivedGauge()`
  Deprecated.
  As of JMX 1.2, replaced by `getDerivedGauge(ObjectName)`
  `Number`
  `getDerivedGauge(ObjectName object)`
  Gets the derived gauge of the specified object, if this object is contained in the set of observed MBeans, or `null` otherwise.
  `long`
  `getDerivedGaugeTimeStamp()`
  Deprecated.
  As of JMX 1.2, replaced by `getDerivedGaugeTimeStamp(ObjectName)`
  `long`
  `getDerivedGaugeTimeStamp(ObjectName object)`
  Gets the derived gauge timestamp of the specified object, if this object is contained in the set of observed MBeans, or `0` otherwise.
  `boolean`
  `getDifferenceMode()`
  Gets the difference mode flag value common to all observed MBeans.
  `Number`
  `getInitThreshold()`
  Gets the initial threshold value common to all observed objects.
  `Number`
  `getModulus()`
  Gets the modulus value common to all observed MBeans.
  `MBeanNotificationInfo[]`
  `getNotificationInfo()`
  Returns a `NotificationInfo` object containing the name of the Java class of the notification and the notification types sent by the counter monitor.
  `boolean`
  `getNotify()`
  Gets the notification's on/off switch value common to all observed MBeans.
  `Number`
  `getOffset()`
  Gets the offset value common to all observed MBeans.
  `Number`
  `getThreshold()`
  Deprecated.
  As of JMX 1.2, replaced by `getThreshold(ObjectName)`
  `Number`
  `getThreshold(ObjectName object)`
  Gets the current threshold value of the specified object, if this object is contained in the set of observed MBeans, or `null` otherwise.
  `void`
  `setDifferenceMode(boolean value)`
  Sets the difference mode flag value common to all observed MBeans.
  `void`
  `setInitThreshold(Number value)`
  Sets the initial threshold value common to all observed objects.
  `void`
  `setModulus(Number value)`
  Sets the modulus value common to all observed MBeans.
  `void`
  `setNotify(boolean value)`
  Sets the notification's on/off switch value common to all observed MBeans.
  `void`
  `setOffset(Number value)`
  Sets the offset value common to all observed MBeans.
  `void`
  `setThreshold(Number value)`
  Deprecated.
  As of JMX 1.2, replaced by `setInitThreshold(Number)`
  `void`
  `start()`
  Starts the counter monitor.
  `void`
  `stop()`
  Stops the counter monitor.

  ### Methods declared in class Monitor

  `addObservedObject, containsObservedObject, getGranularityPeriod, getObservedAttribute, getObservedObject, getObservedObjects, isActive, postDeregister, postRegister, preDeregister, preRegister, removeObservedObject, setGranularityPeriod, setObservedAttribute, setObservedObject`
  Modifier and Type
  Method
  Description
  `void`
  `addObservedObject(ObjectName object)`
  Adds the specified object in the set of observed MBeans, if this object is not already present.
  `boolean`
  `containsObservedObject(ObjectName object)`
  Tests whether the specified object is in the set of observed MBeans.
  `long`
  `getGranularityPeriod()`
  Gets the granularity period (in milliseconds).
  `String`
  `getObservedAttribute()`
  Gets the attribute being observed.
  `ObjectName`
  `getObservedObject()`
  Deprecated.
  As of JMX 1.2, replaced by `Monitor.getObservedObjects()`
  `ObjectName[]`
  `getObservedObjects()`
  Returns an array containing the objects being observed.
  `boolean`
  `isActive()`
  Tests whether the monitor MBean is active.
  `void`
  `postDeregister()`
  Allows the monitor MBean to perform any operations needed after having been unregistered by the MBean server.
  `void`
  `postRegister(Boolean registrationDone)`
  Allows the monitor MBean to perform any operations needed after having been registered in the MBean server or after the registration has failed.
  `void`
  `preDeregister()`
  Allows the monitor MBean to perform any operations it needs before being unregistered by the MBean server.
  `ObjectName`
  `preRegister(MBeanServer server, ObjectName name)`
  Allows the monitor MBean to perform any operations it needs before being registered in the MBean server.
  `void`
  `removeObservedObject(ObjectName object)`
  Removes the specified object from the set of observed MBeans.
  `void`
  `setGranularityPeriod(long period)`
  Sets the granularity period (in milliseconds).
  `void`
  `setObservedAttribute(String attribute)`
  Sets the attribute to observe.
  `void`
  `setObservedObject(ObjectName object)`
  Deprecated.
  As of JMX 1.2, replaced by `Monitor.addObservedObject(ObjectName)`

  ### Methods declared in class NotificationBroadcasterSupport

  `addNotificationListener, handleNotification, removeNotificationListener, removeNotificationListener, sendNotification`
  Modifier and Type
  Method
  Description
  `void`
  `addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback)`
  Adds a listener.
  `protected void`
  `handleNotification(NotificationListener listener, Notification notif, Object handback)`
  This method is called by `sendNotification` for each listener in order to send the notification to that listener.
  `void`
  `removeNotificationListener(NotificationListener listener)`
  Removes a listener from this MBean.
  `void`
  `removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback)`
  Removes a listener from this MBean.
  `void`
  `sendNotification(Notification notification)`
  Sends a notification.

  ### Methods declared in class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  Creates and returns a copy of this object.
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `protected void`
  `finalize()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Finalization is deprecated and subject to removal in a future release.
  `final Class<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `int`
  `hashCode()`
  Returns a hash code value for this object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `String`
  `toString()`
  Returns a string representation of the object.
  `final void`
  `wait()`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
  `final void`
  `wait(long timeoutMillis)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  `final void`
  `wait(long timeoutMillis, int nanos)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

- ## Constructor Details

  - ### CounterMonitor

    public CounterMonitor()
    Default constructor.

- ## Method Details

  - ### start

    public void start()
    Starts the counter monitor.
    Specified by:
      `start` in interface `MonitorMBean`
    Specified by:
      `start` in class `Monitor`

  - ### stop

    public void stop()
    Stops the counter monitor.
    Specified by:
      `stop` in interface `MonitorMBean`
    Specified by:
      `stop` in class `Monitor`

  - ### getDerivedGauge

    public Number getDerivedGauge(ObjectName object)
    Gets the derived gauge of the specified object, if this object is contained in the set of observed MBeans, or `null` otherwise.
    Specified by:
      `getDerivedGauge` in interface `CounterMonitorMBean`
    Parameters:
      `object` - the name of the object whose derived gauge is to be returned.
    Returns:
      The derived gauge of the specified object.

  - ### getDerivedGaugeTimeStamp

    public long getDerivedGaugeTimeStamp(ObjectName object)
    Gets the derived gauge timestamp of the specified object, if this object is contained in the set of observed MBeans, or `0` otherwise.
    Specified by:
      `getDerivedGaugeTimeStamp` in interface `CounterMonitorMBean`
    Parameters:
      `object` - the name of the object whose derived gauge timestamp is to be returned.
    Returns:
      The derived gauge timestamp of the specified object.

  - ### getThreshold

    public Number getThreshold(ObjectName object)
    Gets the current threshold value of the specified object, if this object is contained in the set of observed MBeans, or `null` otherwise.
    Specified by:
      `getThreshold` in interface `CounterMonitorMBean`
    Parameters:
      `object` - the name of the object whose threshold is to be returned.
    Returns:
      The threshold value of the specified object.
    See Also:
      - `CounterMonitorMBean.setThreshold(Number)`

  - ### getInitThreshold

    public Number getInitThreshold()
    Gets the initial threshold value common to all observed objects.
    Specified by:
      `getInitThreshold` in interface `CounterMonitorMBean`
    Returns:
      The initial threshold.
    See Also:
      - `setInitThreshold(Number)`

  - ### setInitThreshold

    public void setInitThreshold(Number value) throws IllegalArgumentException
    Sets the initial threshold value common to all observed objects.
    The current threshold of every object in the set of observed MBeans is updated consequently.
    Specified by:
      `setInitThreshold` in interface `CounterMonitorMBean`
    Parameters:
      `value` - The initial threshold value.
    Throws:
      `IllegalArgumentException` - The specified threshold is null or the threshold value is less than zero.
    See Also:
      - `getInitThreshold()`

  - ### getDerivedGauge

    @Deprecated public Number getDerivedGauge()
    Deprecated.
    As of JMX 1.2, replaced by `getDerivedGauge(ObjectName)`
    Returns the derived gauge of the first object in the set of observed MBeans.
    Specified by:
      `getDerivedGauge` in interface `CounterMonitorMBean`
    Returns:
      The derived gauge.

  - ### getDerivedGaugeTimeStamp

    @Deprecated public long getDerivedGaugeTimeStamp()
    Deprecated.
    As of JMX 1.2, replaced by `getDerivedGaugeTimeStamp(ObjectName)`
    Gets the derived gauge timestamp of the first object in the set of observed MBeans.
    Specified by:
      `getDerivedGaugeTimeStamp` in interface `CounterMonitorMBean`
    Returns:
      The derived gauge timestamp.

  - ### getThreshold

    @Deprecated public Number getThreshold()
    Deprecated.
    As of JMX 1.2, replaced by `getThreshold(ObjectName)`
    Gets the threshold value of the first object in the set of observed MBeans.
    Specified by:
      `getThreshold` in interface `CounterMonitorMBean`
    Returns:
      The threshold value.
    See Also:
      - `setThreshold(Number)`

  - ### setThreshold

    @Deprecated public void setThreshold(Number value) throws IllegalArgumentException
    Deprecated.
    As of JMX 1.2, replaced by `setInitThreshold(Number)`
    Sets the initial threshold value.
    Specified by:
      `setThreshold` in interface `CounterMonitorMBean`
    Parameters:
      `value` - The initial threshold value.
    Throws:
      `IllegalArgumentException` - The specified threshold is null or the threshold value is less than zero.
    See Also:
      - `getThreshold()`

  - ### getOffset

    public Number getOffset()
    Gets the offset value common to all observed MBeans.
    Specified by:
      `getOffset` in interface `CounterMonitorMBean`
    Returns:
      The offset value.
    See Also:
      - `setOffset(Number)`

  - ### setOffset

    public void setOffset(Number value) throws IllegalArgumentException
    Sets the offset value common to all observed MBeans.
    Specified by:
      `setOffset` in interface `CounterMonitorMBean`
    Parameters:
      `value` - The offset value.
    Throws:
      `IllegalArgumentException` - The specified offset is null or the offset value is less than zero.
    See Also:
      - `getOffset()`

  - ### getModulus

    public Number getModulus()
    Gets the modulus value common to all observed MBeans.
    Specified by:
      `getModulus` in interface `CounterMonitorMBean`
    Returns:
      The modulus value.
    See Also:
      - `setModulus(Number)`

  - ### setModulus

    public void setModulus(Number value) throws IllegalArgumentException
    Sets the modulus value common to all observed MBeans.
    Specified by:
      `setModulus` in interface `CounterMonitorMBean`
    Parameters:
      `value` - The modulus value.
    Throws:
      `IllegalArgumentException` - The specified modulus is null or the modulus value is less than zero.
    See Also:
      - `getModulus()`

  - ### getNotify

    public boolean getNotify()
    Gets the notification's on/off switch value common to all observed MBeans.
    Specified by:
      `getNotify` in interface `CounterMonitorMBean`
    Returns:
      `true` if the counter monitor notifies when exceeding the threshold, `false` otherwise.
    See Also:
      - `setNotify(boolean)`

  - ### setNotify

    public void setNotify(boolean value)
    Sets the notification's on/off switch value common to all observed MBeans.
    Specified by:
      `setNotify` in interface `CounterMonitorMBean`
    Parameters:
      `value` - The notification's on/off switch value.
    See Also:
      - `getNotify()`

  - ### getDifferenceMode

    public boolean getDifferenceMode()
    Gets the difference mode flag value common to all observed MBeans.
    Specified by:
      `getDifferenceMode` in interface `CounterMonitorMBean`
    Returns:
      `true` if the difference mode is used, `false` otherwise.
    See Also:
      - `setDifferenceMode(boolean)`

  - ### setDifferenceMode

    public void setDifferenceMode(boolean value)
    Sets the difference mode flag value common to all observed MBeans.
    Specified by:
      `setDifferenceMode` in interface `CounterMonitorMBean`
    Parameters:
      `value` - The difference mode flag value.
    See Also:
      - `getDifferenceMode()`

  - ### getNotificationInfo

    public MBeanNotificationInfo[] getNotificationInfo()
    Returns a `NotificationInfo` object containing the name of the Java class of the notification and the notification types sent by the counter monitor.
    Specified by:
      `getNotificationInfo` in interface `NotificationBroadcaster`
    Returns:
      the array of possible notifications.