5.1 通过JConsole暴露一个资源以供远程管理

2016-01-13 22:14:25 5,322 0

如果你使用开箱即用的 remote management agent 和现有的诸如JConsole之类的监控与管理工具,那么利用JMX API暴露你的Java应用程序用于远程管理就变得非常简单。

要想暴露你的应用程序(YBXIANG:指的是MBean Agent所在的程序)以便远程管理,你需要用正确的系统属性启动它。这个例子展示了如何暴露 Main JMX agent 以供远程管理。

安全考虑:

为了简便起见,在本例中,禁掉了认证和加密安全机制。然而,当你在现实环境中实现远程管理时,你应该实现这些安全机制。What Next? 提供了其它JMX技术文档的参考,展示了如何激活安全。

This example requires version 6 of the Java SE platform. To monitor the Main JMX agent remotely, follow these steps:

    If you have not done so already, save jmx_examples.zip into your work_dir directory.
    Unzip the bundle of sample classes by using the following command in a terminal window.

    unzip jmx_examples.zip

    Compile the example Java classes from within the work_dir directory.

    javac com/example/*.java

    Start the Main application, specifying the properties that expose Main for remote management:

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

    A confirmation that Main is waiting for something to happen is generated.
    Start JConsole in a different terminal window on a different machine:

    jconsole

    The New Connection dialog box is displayed, presenting a list of running JMX agents that you can connect to locally.
    Select Remote Process, and type the following in the Remote Process field:

    hostname:9999

    In this address, hostname is the name of the remote machine on which the Main application is running and 9999 is the number of the port on which the out-of-the-box JMX connector will be connected.
    Click Connect.

    A summary of the current activity of the Java Virtual Machine (Java VM) in which Main is running is displayed.
    Click the MBeans tab.

    This panel shows all the MBeans that are currently registered in the remote MBean server.
    In the left-hand frame, expand the com.example node in the MBean tree.

    You see the example MBean Hello that was created and registered by Main. If you click Hello, you see its associated Attributes and Operations nodes in the MBean tree, even though it is running on a different machine.
    To close JConsole, select Connection -> Exit.