通过 JConsole 公开资源以进行远程管理

原文: https://docs.oracle.com/javase/tutorial/jmx/remote/jconsole.html

如果使用现成的远程管理代理和现有的监视和管理工具(如 JConsole),则使用 JMX API 公开 Java 应用程序以进行远程管理非常简单。

要公开远程管理应用程序,需要使用正确的属性启动它。此示例显示如何公开 Main JMX 代理以进行远程管理。


Security consideration:

为简单起见,在此示例中禁用了身份验证和加密安全机制。但是,在实际环境中实现远程管理时,应实现这些安全机制。 下一步是什么? 提供了指向其他 JMX 技术文档的指针,这些文档展示了如何激活安全性。


此示例需要 Java SE 平台的第 6 版。要远程监视Main JMX 代理,请按照下列步骤操作:

  1. 如果尚未这样做,请将 jmx_examples.zip 保存到work_dir目录中。
  2. 在终端窗口中使用以下命令解压缩示例类包。

    1. unzip jmx_examples.zip
  3. 编译work_dir目录中的示例 Java 类。

    1. javac com/example/*.java
  4. Start the Main application, specifying the properties that expose Main for remote management. (For Windows, use the carat (^) instead of the backslash (\) to break up a long command into multiple lines):

    1. java -Dcom.sun.management.jmxremote.port=9999 \
    2. -Dcom.sun.management.jmxremote.authenticate=false \
    3. -Dcom.sun.management.jmxremote.ssl=false \
    4. com.example.Main

    生成Main正在等待发生某事的确认。

  5. Start JConsole in a different terminal window on a different machine:

    1. jconsole

    将显示“新建连接”对话框,其中列出了可以在本地连接的正在运行的 JMX 代理。

  6. Select Remote Process, and type the following in the Remote Process field:

    1. hostname:9999

    在此地址中,hostname是运行Main应用程序的远程机器的名称,9999 是开箱即用的 JMX 连接器将连接的端口号。

  7. Click Connect.

    显示正在运行Main的 Java 虚拟机(Java VM)的当前活动的摘要。

  8. Click the MBeans tab.

    此面板显示当前在远程 MBean 服务器中注册的所有 MBean。

  9. In the left-hand frame, expand the com.example node in the MBean tree.

    您会看到Main创建并注册的示例 MBean Hello。如果单击Hello,您将在 MBean 树中看到其关联的属性和操作节点,即使它在另一台计算机上运行。

  10. 要关闭 JConsole,请选择 Connection - >出口。