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

su.nlq.prometheus.jmx.connection.remote.ClosableConnection Maven / Gradle / Ivy

There is a newer version: 0.8.0
Show newest version
package su.nlq.prometheus.jmx.connection.remote;

import org.jetbrains.annotations.NotNull;
import su.nlq.prometheus.jmx.connection.Connection;
import su.nlq.prometheus.jmx.connection.ConnectionConsumer;

import javax.management.remote.JMXConnector;
import java.io.IOException;
import java.util.Optional;

public final class ClosableConnection extends Connection {
  private final @NotNull Connector supplier;

  public ClosableConnection(@NotNull Optional name, @NotNull Connector supplier) {
    super(name);
    this.supplier = supplier;
  }

  @Override
  public void accept(@NotNull ConnectionConsumer consumer) {
    try (final JMXConnector connector = supplier.connect()) {
      consumer.accept(name(supplier.address()), connector.getMBeanServerConnection());
    } catch (IOException e) {
      Connection.fail(supplier.address(), e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy