How to get JVM Metrics via code
search cancel

How to get JVM Metrics via code

book

Article ID: 294352

calendar_today

Updated On:

Products

VMware Tanzu Gemfire

Issue/Introduction

Customer wants to fetch JVM metrics via Gemfire MBean API, but mgmtSvc.getMemberMXBean() is coming with null.

Environment

Product Version: 9.8

Resolution

Here is steps to get JVM Metrics instead of null.

Step 1:
We need to make sure to enable JMX Manager by set jmx-manager as true for nodes (loctors or cacheservers) so that JMX Manager's MBean object is available for retrieving.
jmx-manager=true

Step 2:
You can write a Java class like the below to retrieve the JVM Metrics.
DistributedSystem m_distributedSystem = cache.getDistributedSystem(); 

DistributedMember member = m_distributedSystem.getDistributedMember();

ManagementService mgmtSvc = ManagementService.getManagementService(cache);

javax.management.ObjectName memberMBeanName = mgmtSvc.getMemberMBeanName(member);

MemberMXBean memberMxBean = mgmtSvc.getMBeanInstance(memberMBeanName, MemberMXBean.class);

JVMMetrics jvmMetrics = memberMxBean.showJVMMetrics();

System.out.println("---------initmemorysize=" + jvmMetrics.getInitMemory());

You can refer a full sample by Spring Boot implementation:
https://github.com/GSSJacky/GemfireMBeanRetrieveApplication