Dustin's Pages

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.

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:

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.

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

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.