Package io.microsphere.process
Class VirtualMachineProcessIdResolver
- java.lang.Object
-
- io.microsphere.process.VirtualMachineProcessIdResolver
-
- All Implemented Interfaces:
Prioritized
,ProcessIdResolver
,java.lang.Comparable<Prioritized>
public class VirtualMachineProcessIdResolver extends java.lang.Object implements ProcessIdResolver
AProcessIdResolver
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 thegetProcessId()
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
-
-
Field Summary
-
Fields inherited from interface io.microsphere.lang.Prioritized
COMPARATOR, MAX_PRIORITY, MIN_PRIORITY, NORMAL_PRIORITY
-
Fields inherited from interface io.microsphere.process.ProcessIdResolver
UNKNOWN_PROCESS_ID
-
-
Constructor Summary
Constructors Constructor Description VirtualMachineProcessIdResolver()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Long
current()
Resolve the current process idint
getPriority()
Get the priorityboolean
supports()
Whether supports to resolve the process id or not?-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.microsphere.lang.Prioritized
compareTo
-
-
-
-
Method Detail
-
supports
public boolean supports()
Description copied from interface:ProcessIdResolver
Whether supports to resolve the process id or not?- Specified by:
supports
in interfaceProcessIdResolver
- Returns:
true
if supports, otherwisefalse
-
current
public java.lang.Long current()
Description copied from interface:ProcessIdResolver
Resolve the current process id- Specified by:
current
in interfaceProcessIdResolver
- Returns:
>null
if can't be resolved
-
getPriority
public int getPriority()
Description copied from interface:Prioritized
Get the priority- Specified by:
getPriority
in interfacePrioritized
- Returns:
- the default is
minimum one
-
-