Example usage for java.lang.management RuntimeMXBean getClass

List of usage examples for java.lang.management RuntimeMXBean getClass

Introduction

In this page you can find the example usage for java.lang.management RuntimeMXBean getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:brooklyn.entity.software.SshEffectorTasksTest.java

public static Integer getMyPid() {
    try {/*w  ww  . jav a 2 s . c o  m*/
        java.lang.management.RuntimeMXBean runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();
        java.lang.reflect.Field jvm = runtime.getClass().getDeclaredField("jvm");
        jvm.setAccessible(true);
        //            sun.management.VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime);
        Object mgmt = jvm.get(runtime);
        java.lang.reflect.Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId");
        pid_method.setAccessible(true);

        return (Integer) pid_method.invoke(mgmt);
    } catch (Exception e) {
        throw new PropagatedRuntimeException(
                "Test depends on (fragile) getMyPid method which does not work here", e);
    }
}

From source file:org.apache.airavata.server.ServerMain.java

private static int getPID() {
    try {// w  ww.  j  a v a 2 s .  c om
        java.lang.management.RuntimeMXBean runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();
        java.lang.reflect.Field jvm = runtime.getClass().getDeclaredField("jvm");
        jvm.setAccessible(true);
        sun.management.VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime);
        java.lang.reflect.Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId");
        pid_method.setAccessible(true);

        int pid = (Integer) pid_method.invoke(mgmt);
        return pid;
    } catch (Exception e) {
        return -1;
    }
}

From source file:org.apache.flink.streaming.tests.StickyAllocationAndLocalRecoveryTestJob.java

/**
 * This code is copied from Stack Overflow.
 *
 * <p><a href="https://stackoverflow.com/questions/35842">https://stackoverflow.com/questions/35842</a>, answer
 * <a href="https://stackoverflow.com/a/12066696/9193881">https://stackoverflow.com/a/12066696/9193881</a>
 *
 * <p>Author: <a href="https://stackoverflow.com/users/446591/brad-mace">Brad Mace</a>)
 *//*  w w  w  .j a  v  a2  s  .  com*/
private static int getJvmPid() throws Exception {
    java.lang.management.RuntimeMXBean runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();
    java.lang.reflect.Field jvm = runtime.getClass().getDeclaredField("jvm");
    jvm.setAccessible(true);
    sun.management.VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime);
    java.lang.reflect.Method pidMethod = mgmt.getClass().getDeclaredMethod("getProcessId");
    pidMethod.setAccessible(true);

    return (int) (Integer) pidMethod.invoke(mgmt);
}

From source file:org.wso2.carbon.integration.tests.carbontools.CarbonServerBasicOperationTestCase.java

@Test(groups = { "carbon.core" }, description = "Testing carbondump.bat execution", dependsOnMethods = {
        "testStopCommand" })
public void testCarbonDumpCommandOnWindows() throws Exception {
    Process processDump = null;//from w  w  w  .ja va2  s.co  m
    String carbonHome = System.getProperty(ServerConstants.CARBON_HOME);
    try {
        if (CarbonCommandToolsUtil.getCurrentOperatingSystem()
                .contains(OperatingSystems.WINDOWS.name().toLowerCase())) {

            RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
            Field jvmField = runtimeMXBean.getClass().getDeclaredField("jvm");
            jvmField.setAccessible(true);
            VMManagement vmManagement = (VMManagement) jvmField.get(runtimeMXBean);
            Method getProcessIdMethod = vmManagement.getClass().getDeclaredMethod("getProcessId");
            getProcessIdMethod.setAccessible(true);
            Integer processId = (Integer) getProcessIdMethod.invoke(vmManagement);

            String[] cmdArray = new String[] { "cmd.exe", "/c", "carbondump.bat", "-carbonHome", carbonHome,
                    "-pid", Integer.toString(processId) };
            processDump = CarbonCommandToolsUtil.runScript(carbonHome + "/bin", cmdArray);
            assertTrue(isDumpFileFound(carbonHome), "Couldn't find the dump file");
        } else {
            //Skip test from linux
            throw new SkipException(" This test method is only for windows");
        }
    } finally {
        if (processDump != null) {
            processDump.destroy();
        }
    }
}