Tuesday, March 4, 2008

Spring Exposing RMI With and Without Declared RMI Host

This blog entry is intended to provide some details about using Spring's RMI Registry exporting capabilities. The two listings below show snippets of a Spring configuration file and are exactly the same snippets of XML with the exception of one line. The one line that makes these snippets different is not contained in the first listing and is highlighted in the second listing. This line specifies a host to use for the Spring-loaded RMI Registry.


<bean id="registry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
<property name="port" value="1099"/>
</bean>

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



<bean id="registry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
<property name="host" value="localhost" />
<property name="port" value="1099"/>
</bean>

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


At first glance, it would seem simple to assume, because localhost is the default setting for RMI registry host exposed by Spring, that these two XML snippets would result in the same functionality for the Spring-based application. However, there is a significant difference caused by the presence or lack of this one line.

In the first case (no host specified), Spring will start an RMI registry if it cannot find one. That RMI registry will be started on the localhost.

At this point, it is natural to assume that the second listing will lead to the same result because localhost is explicitly specified as the host. However, this is not the case. When localhost is explicitly specified as a host, the RMI registry must be started externally first (for example, by running rmiregistry) and then Spring will use that running RMI registry. If one specifies localhost explicitly and has not previously started an RMI registry on that host and port, an exception will be encountered that includes this line: java.rmi.ConnectException: Connection refused to host: localhost.

This is the case for RmiRegistryFactoryBean as used in the examples above, but the same thing is just as true for using RmiServiceExporter for RMI remoting. The only difference is that RmiServiceExporter has registryHost rather than host for the attribute that affects whether an RMI registry needs to be running before the Spring container is started.

6 comments:

GKB said...

Why can't I be as smart as you. I did a Google search on you today to see what you've been writing lately and I found your blog. I was going to finally catch up on the Slackers email, but I can't get into my Comcast account. It isn't easy being dumb...I hope you and your family are doing well. By the way, this comment is in no way related to what you wrote. Meh.

lisdey89 said...

Hello, i am new in Spring and I have a problem exposing my MBean using RMI, I have the same configuration that you shows in your post, but anyway I get this error: "javax.management.MalformedObjectNameException: Key properties cannot be empty", I know that your blog is not a forum, but I do not know what to do, I have been searching in internet but i do not found nothing useful. I will thanks you if you can help me

@DustinMarx said...

lisdey89,

The MalformedObjectNameException with string "Key properties cannot be empty" typically means that you don't have your ObjectName string specified in a format like name=value. For example, in my post above, one of these that I specify a couple times is connector:name=rmi. The name ("connector:name") is on the left side and the value is on the right side ("rmi") and they are separated by an equals sign.

Dustin

@DustinMarx said...

lisdey89,

For more details on a valid ObjectName, see the Javadoc documentation for ObjectName.

Dustin

lisdey89 said...

Thanks a lot for your recommendation, this i post the piece of code that i put in my configuration

It keep throws me the same error, then i change and i put this into a exporter beans, exactly inside the property beans


And it works fine, what do you think about it? may be this configuration is wrong, I don't know, I am new in this technology. Thanks for your support

Unknown said...

Hi Lisdey89,

Can you please show how you did the change.


Thanks,
Gangadhar