Class VirtualMachineProcessIdResolver

  • All Implemented Interfaces:
    Prioritized, ProcessIdResolver, java.lang.Comparable<Prioritized>

    public class VirtualMachineProcessIdResolver
    extends java.lang.Object
    implements ProcessIdResolver
    A ProcessIdResolver implementation for retrieving the process ID using the SUN JVM internal APIs.

    This resolver utilizes reflection to access the internal fields and methods of the JVM, specifically targeting the sun.management.VMManagementImpl class which provides a method to retrieve the native process ID.

    How It Works

    The process ID is obtained via reflection by accessing the hidden "jvm" field in the RuntimeMXBean instance, and then invoking the getProcessId() method on that internal object. This approach is specific to the HotSpot JVM and may not be available on all JVM implementations.

    Example Usage

    
     ProcessIdResolver resolver = new VirtualMachineProcessIdResolver();
     if (resolver.supports()) {
         Long pid = resolver.current();
         System.out.println("Current Process ID: " + pid);
     }
     

    Priority

    This resolver has a priority level of getPriority(), making it preferred over some other resolvers but not the highest priority. The priority helps determine which resolver should be used when multiple resolvers are available.

    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    ProcessIdResolver
    • Constructor Detail

      • VirtualMachineProcessIdResolver

        public VirtualMachineProcessIdResolver()
    • Method Detail

      • supports

        public boolean supports()
        Description copied from interface: ProcessIdResolver
        Whether supports to resolve the process id or not?
        Specified by:
        supports in interface ProcessIdResolver
        Returns:
        true if supports, otherwise false
      • current

        public java.lang.Long current()
        Description copied from interface: ProcessIdResolver
        Resolve the current process id
        Specified by:
        current in interface ProcessIdResolver
        Returns:
        >null if can't be resolved