简体   繁体   中英

JMX Spring connect to localhost without specifying the server address and port

I am referring here

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/jmx.html#jmx-proxy

I am trying to connect to localhost MBeanServer and perform some operation using Spring Proxy. The problem is just specifying the ObjectName and ProxyInterface, I am unable to connect to localhost. I am able to solve this problem by specifying the server property of the MBeanProxyFactoryBean.

Here is my spring context.xml

bean id="proxyWithoutServer" class="org.springframework.jmx.access.MBeanProxyFactoryBean"
          p:objectName="com.xxx.yyy"
          p:proxyInterface="com.MyInterface" 

bean id="proxyWithServer" class="org.springframework.jmx.access.MBeanProxyFactoryBean"
          p:objectName="com.xxx.yyy"
          p:proxyInterface="com.MyInterface" 
p:server-ref="clientConnector"

<bean id="clientConnector" class="org.springframework.jmx.support.MBeanServerConnectionFactoryBean"
          p:serviceUrl="service:jmx:rmi://localhost/jndi/rmi://localhost:8001/jmxrmi" />

In Java code, I simply do

MyInterface myInterface = context.getBean("proxyWithoutServer");
myInterface.myMethod();

But this doesnt work. - Says Unable to connect to localhost

But If I use

MyInterface myInterface = context.getBean("proxyWithServer");
myInterface.myMethod();

This works.

The problem here is that this code would be run on multiple servers and the jmx port in each would be different. SO I would not want to specify the port number. Hence I would like the proxyWithoutServer version of this to work. Looking at the spring jmx documentation it seems we do not need to specify the server port if we are trying to connect to local MBean Server. Would need some advice on what I am doing wrong here.

Thanks in advance

Create a local reference to the MBeanServer.

<context:mbean-server id="localMBeanServer"/>

Then use localMBeanServer in the p:server-ref.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM