【发布时间】:2021-11-03 00:39:27
【问题描述】:
我正在尝试获取 Spring-boot Hikari 池中的活动连接数。在我的日志中,它打印了两个名为 HikariPool-1 和 HikariPool-2 的池。
@Slf4j
@RequiredArgsConstructor
public class HikariJmxElf {
private final ObjectName poolAccessor;
private final MBeanServer mBeanServer;
public HikariJmxElf(final String poolName) {
try {
mBeanServer = java.lang.management.ManagementFactory.getPlatformMBeanServer();
poolAccessor = new ObjectName("com.zaxxer.hikari:type=Pool (" + poolName + ")");
} catch (MalformedObjectNameException e) {
throw new RuntimeException("Pool " + poolName + " could not be found", e);
}
}
public int getIdleConnections() {
try {
return (Integer) mBeanServer.getAttribute(poolAccessor, "IdleConnections");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int getActiveConnections() {
try {
return (Integer) mBeanServer.getAttribute(poolAccessor, "ActiveConnections");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int getTotalConnections() {
try {
return (Integer) mBeanServer.getAttribute(poolAccessor, "TotalConnections");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
我正在尝试使用执行器信息端点获取该数据。
@Slf4j
@Component
@RequiredArgsConstructor
public class HikariPoolInfoContributor implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
builder
.withDetail("connectionDetails", new HikariJmxElf("HikariPool-1").getActiveConnections())
.build();
}
}
我得到的完整错误是
java.lang.RuntimeException: javax.management.InstanceNotFoundException: com.zaxxer.hikari:type=Pool (HikariPool-1)
我有这些问题。
- mBeanServer 服务器需要一个带有池名称的对象名称。 HikariPool-1 不是 if 的实际名称吗?
- 有没有一种方法可以让所有 Hikari 池不使用名称?
【问题讨论】:
标签: java spring-boot spring-boot-actuator hikaricp