Java Code Examples for java.lang.management.ManagementFactory#newPlatformMXBeanProxy()

The following examples show how to use java.lang.management.ManagementFactory#newPlatformMXBeanProxy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JvmUtil.java    From Jpom with MIT License 6 votes vote down vote up
/**
 * 获取指定程序的线程信息
 *
 * @param jpomTag jpomTag
 * @return 没有运行或者获取数据
 * @throws Exception 异常
 * @see ThreadMXBean
 */
public static ThreadMXBean getThreadMXBean(String jpomTag) throws Exception {
    VirtualMachine virtualMachine = JvmUtil.getVirtualMachine(jpomTag);
    if (virtualMachine == null) {
        return null;
    }
    try {
        JMXServiceURL url = getJMXServiceURL(virtualMachine);
        if (url == null) {
            return null;
        }
        JMXConnector jmxConnector = JMXConnectorFactory.connect(url, null);
        MBeanServerConnection mBeanServerConnection = jmxConnector.getMBeanServerConnection();
        return ManagementFactory.newPlatformMXBeanProxy(mBeanServerConnection, ManagementFactory.THREAD_MXBEAN_NAME, ThreadMXBean.class);
    } finally {
        virtualMachine.detach();
    }
}
 
Example 2
Source File: GetVMOption.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    List<HotSpotDiagnosticMXBean> list =
        ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
    HotSpotDiagnosticMXBean mbean = list.get(0);
    checkVMOption(mbean);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
                HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
                HotSpotDiagnosticMXBean.class);
    checkVMOption(mbean);
}
 
Example 3
Source File: SetAllVMOptions.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    List<HotSpotDiagnosticMXBean> list =
        ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
    HotSpotDiagnosticMXBean mbean = list.get(0);
    setAllVMOptions(mbean);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
                HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
                HotSpotDiagnosticMXBean.class);
    setAllVMOptions(mbean);
}
 
Example 4
Source File: JMServer.java    From jmonitor with GNU General Public License v2.0 5 votes vote down vote up
public static JSONObject geGCInfo(String app) throws Exception {
    ObjectName obj = new ObjectName("java.lang:type=GarbageCollector,*");
    MBeanServer conn = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectInstance> MBeanset = conn.queryMBeans(obj, null);
    Class<GarbageCollectorMXBean> cls = GarbageCollectorMXBean.class;
    JSONObject data = new JSONObject();
    for (ObjectInstance objx : MBeanset) {
        String name = objx.getObjectName().getCanonicalName();
        String keyName = objx.getObjectName().getKeyProperty("name");
        GarbageCollectorMXBean gc = ManagementFactory.newPlatformMXBeanProxy(conn, name, cls);
        data.put(keyName + "-time", gc.getCollectionTime() / 1000.0);
        data.put(keyName + "-count", gc.getCollectionCount());
    }
    return data;
}
 
Example 5
Source File: MBeanProcessController.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Ensures that the other process identifies itself by the same pid used by 
 * this stopper to connect to that process. NOT USED EXCEPT IN TEST.
 * 
 * @return true if the pid matches
 * 
 * @throws IllegalStateException if the other process identifies itself by a different pid
 * @throws IOException if a communication problem occurred when accessing the MBeanServerConnection
 * @throws PidUnavailableException if parsing the pid from the RuntimeMXBean name fails
 */
boolean checkPidMatches() throws IllegalStateException, IOException, PidUnavailableException {
  final RuntimeMXBean proxy = ManagementFactory.newPlatformMXBeanProxy(
      this.server, ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
  final int remotePid = ProcessUtils.identifyPid(proxy.getName());
  if (remotePid != this.pid) {
    throw new IllegalStateException("Process has different pid '" + remotePid + "' than expected pid '" + this.pid + "'");
  } else {
    return true;
  }
}
 
Example 6
Source File: HeapDumper.java    From buck with Apache License 2.0 5 votes vote down vote up
private static PlatformManagedObject getDiagnosticBean() {
  try {
    Class<? extends PlatformManagedObject> clazz =
        Class.forName("com.sun.management.HotSpotDiagnosticMXBean")
            .asSubclass(PlatformManagedObject.class);
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    PlatformManagedObject bean =
        ManagementFactory.newPlatformMXBeanProxy(server, HOTSPOT_BEAN_NAME, clazz);
    return bean;
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example 7
Source File: GetDiagnosticOptions.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    List<HotSpotDiagnosticMXBean> list =
        ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
    HotSpotDiagnosticMXBean mbean = list.get(0);
    checkDiagnosticOptions(mbean);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
                HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
                HotSpotDiagnosticMXBean.class);
    checkDiagnosticOptions(mbean);
}
 
Example 8
Source File: JmxClient.java    From vjtools with Apache License 2.0 5 votes vote down vote up
public synchronized RuntimeMXBean getRuntimeMXBean() throws IOException {
	if (hasPlatformMXBeans && runtimeMBean == null) {
		runtimeMBean = ManagementFactory.newPlatformMXBeanProxy(server, ManagementFactory.RUNTIME_MXBEAN_NAME,
				RuntimeMXBean.class);
	}
	return runtimeMBean;
}
 
Example 9
Source File: GetVMOption.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    List<HotSpotDiagnosticMXBean> list =
        ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
    HotSpotDiagnosticMXBean mbean = list.get(0);
    checkVMOption(mbean);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
                HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
                HotSpotDiagnosticMXBean.class);
    checkVMOption(mbean);
}
 
Example 10
Source File: GetDiagnosticOptions.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    List<HotSpotDiagnosticMXBean> list =
        ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
    HotSpotDiagnosticMXBean mbean = list.get(0);
    checkDiagnosticOptions(mbean);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
                HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
                HotSpotDiagnosticMXBean.class);
    checkDiagnosticOptions(mbean);
}
 
Example 11
Source File: GetDoubleVMOption.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    List<HotSpotDiagnosticMXBean> list =
        ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
    HotSpotDiagnosticMXBean mbean = list.get(0);
    checkVMOption(mbean);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
                HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
                HotSpotDiagnosticMXBean.class);
    checkVMOption(mbean);
}
 
Example 12
Source File: TestElasticHeapMXBean.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    try {
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + "127.0.0.1:" + args[0] + "/jmxrmi");
        MBeanServerConnection connection = JMXConnectorFactory.connect(url, null).getMBeanServerConnection();
        ElasticHeapMXBean elasticHeapMXBean = ManagementFactory.newPlatformMXBeanProxy(connection,
            "com.alibaba.management:type=ElasticHeap",
            ElasticHeapMXBean.class);
        elasticHeapMXBean.setYoungGenCommitPercent(50);
        System.out.println("Set young percent 50: " + elasticHeapMXBean.getYoungGenCommitPercent());
        System.out.println("Set freed bytes 50M: " + elasticHeapMXBean.getTotalYoungUncommittedBytes());
        Thread.sleep(3000);
        elasticHeapMXBean.setYoungGenCommitPercent(100);
        System.out.println("Set young percent 100: " + elasticHeapMXBean.getYoungGenCommitPercent());
        System.out.println("Set freed bytes 0M: " + elasticHeapMXBean.getTotalYoungUncommittedBytes());

        elasticHeapMXBean.setYoungGenCommitPercent(0);
        Thread.sleep(3000);

        elasticHeapMXBean.setSoftmxPercent(70);
        Thread.sleep(3000);
        System.out.println("Set heap percent 70: " + elasticHeapMXBean.getSoftmxPercent());
        System.out.println("Set freed bytes 300M: " + elasticHeapMXBean.getTotalUncommittedBytes());
        Thread.sleep(3000);
        elasticHeapMXBean.setSoftmxPercent(50);
        Thread.sleep(3000);
        System.out.println("Set heap percent 50: " + elasticHeapMXBean.getSoftmxPercent());
        System.out.println("Set freed bytes 500M: " + elasticHeapMXBean.getTotalUncommittedBytes());
        Thread.sleep(3000);
        elasticHeapMXBean.setSoftmxPercent(0);
        Thread.sleep(3000);
        System.out.println("Evaluation mode: " + (elasticHeapMXBean.getEvaluationMode() == ElasticHeapEvaluationMode.INACTIVE? "inactive" : "unexpected"));

    } catch (Exception e) {
        System.out.println("Error: "+ e.getMessage());
    }
}
 
Example 13
Source File: SetAllVMOptions.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    List<HotSpotDiagnosticMXBean> list =
        ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
    HotSpotDiagnosticMXBean mbean = list.get(0);
    setAllVMOptions(mbean);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
                HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
                HotSpotDiagnosticMXBean.class);
    setAllVMOptions(mbean);
}
 
Example 14
Source File: GetDiagnosticOptions.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    List<HotSpotDiagnosticMXBean> list =
        ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
    HotSpotDiagnosticMXBean mbean = list.get(0);
    checkDiagnosticOptions(mbean);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
                HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
                HotSpotDiagnosticMXBean.class);
    checkDiagnosticOptions(mbean);
}
 
Example 15
Source File: JvmUtil.java    From Jpom with MIT License 5 votes vote down vote up
/**
 * 获取指定程序的jvm 信息
 *
 * @param pId pId
 * @return 没有运行或者获取数据
 * @throws Exception 异常
 * @see OperatingSystemMXBean
 */
public static OperatingSystemMXBean getOperatingSystemMXBean(String pId) throws Exception {
    VirtualMachine virtualMachine = getVirtualMachine(Integer.parseInt(pId));
    try {
        JMXServiceURL url = getJMXServiceURL(virtualMachine);
        if (url == null) {
            return null;
        }
        JMXConnector jmxConnector = JMXConnectorFactory.connect(url, null);
        MBeanServerConnection mBeanServerConnection = jmxConnector.getMBeanServerConnection();
        return ManagementFactory.newPlatformMXBeanProxy(mBeanServerConnection, ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, OperatingSystemMXBean.class);
    } finally {
        virtualMachine.detach();
    }
}
 
Example 16
Source File: SetAllVMOptions.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    List<HotSpotDiagnosticMXBean> list =
        ManagementFactory.getPlatformMXBeans(HotSpotDiagnosticMXBean.class);
    HotSpotDiagnosticMXBean mbean = list.get(0);
    setAllVMOptions(mbean);

    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
                HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
                HotSpotDiagnosticMXBean.class);
    setAllVMOptions(mbean);
}
 
Example 17
Source File: JMConnManager.java    From jmonitor with GNU General Public License v2.0 4 votes vote down vote up
public static <T> T getServer(String app, String beanBane, Class<T> cls) throws IOException {
    T ser = ManagementFactory.newPlatformMXBeanProxy(getConn(app), beanBane, cls);
    return ser;
}
 
Example 18
Source File: Basic.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Random rand = new Random();

    // allocate a few direct buffers
    int bufferCount = 5 + rand.nextInt(20);
    buffers = new ArrayList<ByteBuffer>(bufferCount);
    long totalCapacity = 0L;
    for (int i=0; i<bufferCount; i++) {
        int cap = 1024 + rand.nextInt(4096);
        buffers.add( ByteBuffer.allocateDirect(cap) );
        totalCapacity += cap;
    }

    // create a mapped buffer
    Path tmpfile = Files.createTempFile("blah", null);
    tmpfile.toFile().deleteOnExit();
    try (FileChannel fc = FileChannel.open(tmpfile, READ, WRITE)) {
        mbb = fc.map(FileChannel.MapMode.READ_WRITE, 10, 100);
        bufferCount++;
        totalCapacity += mbb.capacity();
    }

    // use platform MXBeans directly
    List<BufferPoolMXBean> pools =
        ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
    check(pools, bufferCount, totalCapacity);

    // use MBeanServer
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectName> mbeans = server.queryNames(
        new ObjectName("java.nio:type=BufferPool,*"), null);
    pools = new ArrayList<BufferPoolMXBean>();
    for (ObjectName name: mbeans) {
        BufferPoolMXBean pool = ManagementFactory
            .newPlatformMXBeanProxy(server, name.toString(), BufferPoolMXBean.class);
        pools.add(pool);
    }
    check(pools, bufferCount, totalCapacity);

    // attempt to unmap mapped buffer
    WeakReference<MappedByteBuffer> ref = new WeakReference<>(mbb);
    mbb = null;
    do {
        System.gc();
        Thread.sleep(250);
    } while (ref.get() != null);
}
 
Example 19
Source File: Basic.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Random rand = new Random();

    // allocate a few direct buffers
    int bufferCount = 5 + rand.nextInt(20);
    buffers = new ArrayList<ByteBuffer>(bufferCount);
    long totalCapacity = 0L;
    for (int i=0; i<bufferCount; i++) {
        int cap = 1024 + rand.nextInt(4096);
        buffers.add( ByteBuffer.allocateDirect(cap) );
        totalCapacity += cap;
    }

    // create a mapped buffer
    Path tmpfile = Files.createTempFile("blah", null);
    tmpfile.toFile().deleteOnExit();
    try (FileChannel fc = FileChannel.open(tmpfile, READ, WRITE)) {
        mbb = fc.map(FileChannel.MapMode.READ_WRITE, 10, 100);
        bufferCount++;
        totalCapacity += mbb.capacity();
    }

    // use platform MXBeans directly
    List<BufferPoolMXBean> pools =
        ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
    check(pools, bufferCount, totalCapacity);

    // use MBeanServer
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectName> mbeans = server.queryNames(
        new ObjectName("java.nio:type=BufferPool,*"), null);
    pools = new ArrayList<BufferPoolMXBean>();
    for (ObjectName name: mbeans) {
        BufferPoolMXBean pool = ManagementFactory
            .newPlatformMXBeanProxy(server, name.toString(), BufferPoolMXBean.class);
        pools.add(pool);
    }
    check(pools, bufferCount, totalCapacity);

    // attempt to unmap mapped buffer
    WeakReference<MappedByteBuffer> ref = new WeakReference<>(mbb);
    mbb = null;
    do {
        System.gc();
        Thread.sleep(250);
    } while (ref.get() != null);
}
 
Example 20
Source File: ClassProfiler.java    From openjdk-systemtest with Apache License 2.0 4 votes vote down vote up
private void getStatsViaProxy() {
	RuntimeMXBean runtimeBean = null;
	OperatingSystemMXBean osBean = null;
	LoggingMXBean logBean = null;
	ClassLoadingMXBean classBean = null;
	CompilationMXBean compBean = null;

	// Get the proxies for the runtime, os, log and class MXBeans
	try {
		runtimeBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, ManagementFactory.RUNTIME_MXBEAN_NAME,
				RuntimeMXBean.class);

		osBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME,
				OperatingSystemMXBean.class);

		logBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, LogManager.LOGGING_MXBEAN_NAME,
				LoggingMXBean.class);

		classBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, ManagementFactory.CLASS_LOADING_MXBEAN_NAME,
				ClassLoadingMXBean.class);

		// Check the compiler is being used and get the compilation MXBean
		Map<String, String> props = runtimeBean.getSystemProperties();
		String sys_comp = props.get("java.compiler");

		if ((sys_comp != null) && (!(sys_comp.equals("")))) {
			compBean = ManagementFactory.newPlatformMXBeanProxy(this.mbs, ManagementFactory.COMPILATION_MXBEAN_NAME,
					CompilationMXBean.class);
		}
	} catch (IOException ioe) {
		Message.logOut("A communication problem occurred when accessing the MBeanServerConnection");
		ioe.printStackTrace();
		Assert.fail("A communication problem occurred when accessing the MBeanServerConnection");
	}

	try {
		Message.logOut("Starting to write data");
		// Record the environment data in the log
		this.envData.writeData(runtimeBean, osBean, logBean, false);

		// Record the number of class loaded over time in a csv and record
		// the class data in the log every 10 seconds
		int secondsCnt = 0;
		int writesCnt = 0;
		while (writesCnt < 30) {
			// Write out the threadData every 10 seconds
			if (secondsCnt == 10) {
				System.out.print(".");
				this.classData.writeData(classBean, compBean, runtimeBean, true);
				secondsCnt = 0;
				writesCnt++;
			}
			this.recordClassStats(runtimeBean, classBean);
			Thread.sleep(1000);
			secondsCnt++;
		}
	} catch (InterruptedException ie) {
		Message.logOut("The sleeping profiler was interrupted");
		ie.printStackTrace();
		Assert.fail("The sleeping profiler was interrupted");
	} catch (UndeclaredThrowableException ue) {
		Throwable cause = ue.getCause();
		Class<ConnectException> connectExcept = ConnectException.class;
		Class<UnmarshalException> unmarshalExcept = UnmarshalException.class;

		if (connectExcept.isInstance(cause) || unmarshalExcept.isInstance(cause)) {
			
			// If the exception was caused by a Connect or Unmarshal
			// Exception, assume the monitored JVM has finished. 
			this.closeCSVFile();
			Message.logOut("Exiting as JVM we are connected to has finished");
			Assert.fail("Exiting as JVM we are connected to has finished");
		} else {
			Message.logOut(ue.getMessage());
			ue.printStackTrace();
			Assert.fail(ue.getMessage());
		}
	} finally {
		this.closeCSVFile();
	}
}