Using JSR-160 Connectors

为了远程访问,Spring JMX 模块在 org.springframework.jmx.support 程序包内提供两个 FactoryBean 实现,用于创建服务器和客户端连接器。

For remote access, Spring JMX module offers two FactoryBean implementations inside the org.springframework.jmx.support package for creating both server- and client-side connectors.

Server-side Connectors

要让 Spring JMX 创建、启动和公开 JSR-160 JMXConnectorServer,你可以使用以下配置:

To have Spring JMX create, start, and expose a JSR-160 JMXConnectorServer, you can use the following configuration:

<bean id="serverConnector" class="org.springframework.jmx.support.ConnectorServerFactoryBean"/>

默认情况下,ConnectorServerFactoryBean 创建一个绑定到 service:jmx:jmxmp://localhost:9875JMXConnectorServer。因此,serverConnector bean 通过 localhost 上的 JMXMP 协议,在 9875 端口向客户端公开 local MBeanServer。请注意,JMXMP 协议标记为 JSR 160 规范中的可选协议。目前,主要的开源 JMX 实现,MX4J,和 JDK 提供的 JMX 实现不支持 JMXMP。

By default, ConnectorServerFactoryBean creates a JMXConnectorServer bound to service:jmx:jmxmp://localhost:9875. The serverConnector bean thus exposes the local MBeanServer to clients through the JMXMP protocol on localhost, port 9875. Note that the JMXMP protocol is marked as optional by the JSR 160 specification. Currently, the main open-source JMX implementation, MX4J, and the one provided with the JDK do not support JMXMP.

要指定另一个 URL,并用 ObjectName 属性将 JMXConnectorServer 本身注册到 MBeanServer,你可以使用以下示例所示:

To specify another URL and register the JMXConnectorServer itself with the MBeanServer, you can use the serviceUrl and ObjectName properties, respectively, as the following example shows:

<bean id="serverConnector"
		class="org.springframework.jmx.support.ConnectorServerFactoryBean">
	<property name="objectName" value="connector:name=rmi"/>
	<property name="serviceUrl"
			value="service:jmx:rmi://localhost/jndi/rmi://localhost:1099/myconnector"/>
</bean>

如果设置 ObjectName 属性,Spring 会自动使用该 ObjectName 将你的连接器注册到 MBeanServer。以下示例显示了在创建 JMXConnector 时可以传递给 ConnectorServerFactoryBean 的全部参数:

If the ObjectName property is set, Spring automatically registers your connector with the MBeanServer under that ObjectName. The following example shows the full set of parameters that you can pass to the ConnectorServerFactoryBean when creating a JMXConnector:

<bean id="serverConnector"
		class="org.springframework.jmx.support.ConnectorServerFactoryBean">
	<property name="objectName" value="connector:name=iiop"/>
	<property name="serviceUrl"
		value="service:jmx:iiop://localhost/jndi/iiop://localhost:900/myconnector"/>
	<property name="threaded" value="true"/>
	<property name="daemon" value="true"/>
	<property name="environment">
		<map>
			<entry key="someKey" value="someValue"/>
		</map>
	</property>
</bean>

请注意,当你使用基于 RMI 的连接器时,你需要启动查找服务 (tnameservrmiregistry),才能完成名称注册。

Note that, when you use a RMI-based connector, you need the lookup service (tnameserv or rmiregistry) to be started in order for the name registration to complete.

Client-side Connectors

要创建一个到远程支持 JSR-160 的 MBeanServerMBeanServerConnection,你可以使用以下示例所示的 MBeanServerConnectionFactoryBean

To create an MBeanServerConnection to a remote JSR-160-enabled MBeanServer, you can use the MBeanServerConnectionFactoryBean, as the following example shows:

<bean id="clientConnector" class="org.springframework.jmx.support.MBeanServerConnectionFactoryBean">
	<property name="serviceUrl" value="service:jmx:rmi://localhost/jndi/rmi://localhost:1099/jmxrmi"/>
</bean>

JMX over Hessian or SOAP

JSR-160 允许扩展客户端和服务器之间进行通信的方式。前面各节中所示的示例使用了 JSR-160 规范(IIOP 和 JRMP)所需的强制性基于 RMI 的实现以及(可选的)JMXMP。通过使用其他提供程序或 JMX 实现(例如 MX4J),你可以利用通过简单的 HTTP 或 SSL 等协议的 SOAP 或 Hessian 等协议,如下例所示:

JSR-160 permits extensions to the way in which communication is done between the client and the server. The examples shown in the preceding sections use the mandatory RMI-based implementation required by the JSR-160 specification (IIOP and JRMP) and the (optional) JMXMP. By using other providers or JMX implementations (such as MX4J) you can take advantage of protocols such as SOAP or Hessian over simple HTTP or SSL and others, as the following example shows:

<bean id="serverConnector" class="org.springframework.jmx.support.ConnectorServerFactoryBean">
	<property name="objectName" value="connector:name=burlap"/>
	<property name="serviceUrl" value="service:jmx:burlap://localhost:9874"/>
</bean>

在上一个示例中,我们使用了 MX4J 3.0.0。请参阅官方 MX4J 文档以获取更多信息。

In the preceding example, we used MX4J 3.0.0. See the official MX4J documentation for more information.