Accessing MBeans through Proxies
Spring JMX 允许你创建代理,该代理会重新路由对 alocal 或 remote MBeanServer
中注册的 MBean 的调用。这些代理为你提供了标准 Java 接口,你可以通过该接口与 MBean 交互。以下代码显示了如何为在 local MBeanServer
中运行的 MBean 配置代理:
Spring JMX lets you create proxies that re-route calls to MBeans that are registered in a
local or remote MBeanServer
. These proxies provide you with a standard Java interface,
through which you can interact with your MBeans. The following code shows how to configure a
proxy for an MBean running in a local MBeanServer
:
<bean id="proxy" class="org.springframework.jmx.access.MBeanProxyFactoryBean">
<property name="objectName" value="bean:name=testBean"/>
<property name="proxyInterface" value="org.springframework.jmx.IJmxTestBean"/>
</bean>
在上一个示例中,你可以看到,对 ObjectName
为 bean:name=testBean
的 MBean 创建了一个代理。代理实现的接口集受 proxyInterfaces
属性控制,而方法和属性的映射规则这些接口在操作和属性在 MBean 上,用于 InterfaceBasedMBeanInfoAssembler
的规则相同。
In the preceding example, you can see that a proxy is created for the MBean registered under the
ObjectName
of bean:name=testBean
. The set of interfaces that the proxy implements
is controlled by the proxyInterfaces
property, and the rules for mapping methods and
properties on these interfaces to operations and attributes on the MBean are the same
rules used by the InterfaceBasedMBeanInfoAssembler
.
MBeanProxyFactoryBean
可以通过 MBeanServerConnection
创建代理到任何 MBean。默认情况下,会找到并使用 local MBeanServer
,但你可以覆盖它,并提供一个指向 remote MBeanServer
的 MBeanServerConnection
,以迎合指向 remote MBean 的代理:
The MBeanProxyFactoryBean
can create a proxy to any MBean that is accessible through an
MBeanServerConnection
. By default, the local MBeanServer
is located and used, but
you can override this and provide an MBeanServerConnection
that points to a remote
MBeanServer
to cater for proxies that point to remote MBeans:
<bean id="clientConnector"
class="org.springframework.jmx.support.MBeanServerConnectionFactoryBean">
<property name="serviceUrl" value="service:jmx:rmi://remotehost:9875"/>
</bean>
<bean id="proxy" class="org.springframework.jmx.access.MBeanProxyFactoryBean">
<property name="objectName" value="bean:name=testBean"/>
<property name="proxyInterface" value="org.springframework.jmx.IJmxTestBean"/>
<property name="server" ref="clientConnector"/>
</bean>
在上一个示例中,我们创建了一个 MBeanServerConnection
,该连接指向使用 MBeanServerConnectionFactoryBean
的远程计算机。然后通过 server
属性将此 MBeanServerConnection
传递给 MBeanProxyFactoryBean
。创建的代理通过此 MBeanServerConnection
将所有调用转发到 MBeanServer
。
In the preceding example, we create an MBeanServerConnection
that points to a remote machine
that uses the MBeanServerConnectionFactoryBean
. This MBeanServerConnection
is then
passed to the MBeanProxyFactoryBean
through the server
property. The proxy that is
created forwards all invocations to the MBeanServer
through this
MBeanServerConnection
.