All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.marschall.micrometer.jfr.AbstractJfrGauge Maven / Gradle / Ivy

The newest version!
package com.github.marschall.micrometer.jfr;

import java.lang.ref.WeakReference;
import java.util.function.ToDoubleFunction;

import io.micrometer.core.instrument.Gauge;

abstract class AbstractJfrGauge, E extends AbstractJfrMeterEvent & DoubleValueEvent> extends AbstractJfrMeter implements Gauge {

  final WeakReference reference;
  final ToDoubleFunction valueFunction;
  private final Runnable hook;

  AbstractJfrGauge(Id id, T obj, ToDoubleFunction valueFunction, F meterEventFactory) {
    super(id, meterEventFactory);
    // all the other code uses a WeakReference
    this.reference = new WeakReference<>(obj);
    this.valueFunction = valueFunction;
    this.hook = this::value;
    this.meterEventFactory.registerPeriodicEvent(this.jfrEventFactory, this.hook);
  }

  @Override
  public double value() {
    T obj = this.reference.get();
    double value;
    if (obj != null) {
      value = this.valueFunction.applyAsDouble(obj);
    } else {
      value = Double.NaN;
    }
    E event = this.newEmptyEvent();
    event.setValue(value);
    event.commit();
    return value;
  }

  @Override
  public void close() {
    this.meterEventFactory.unregisterPeriodicEvent(this.hook);
    super.close();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy