How to use the SimpleHttpInvokerServiceExporter

The JRE 1.6 ships with small HTTP-Server implementation. The Javadocs can be found here.

Spring has provides many different ways of configuring remote access to beans (aka spring remoting), but the remoting documentation fails when it comes to the usage of the HTTP Service exporter that works with the new JRE 1.6 HttpServer.

So here is a small example on how wire up the embedded JDK HttpServer and spring remoting.

First we need some service that we would like to expose as a remote service. For this short example I am using an very simple “EchoService” as follows:

The interface:

and some implementation:

As you can see in the implementation, I am using annotation based configuration in this example.

That is pretty much all implementation I need to provide the services. Now I have to setup the HttpServer and configure the service exporting. The HttpServer implementation provided by the JRE uses factory methods for the setup. It is a pain to configure beans using factory-methods in the spring xml, and that’s why we usually create a FactoryBean to wrap up the ugly initialization of a bean. Fortunately we do not have to do this ourselves, the Springframework already provides (since version 2.5.1) such a factory bean.

So here is the context xml configuration file I am using to start the server and to export the services:

As you can see I also added the xml snippet required for the clients that will be using the remoted service. The qualifier will help me to get the remoted instance and not the local one, hence both instances are kept in my application context in this sample. A simple testcase would look this way:

The first test method echoTestUsingConfiguredClient does use the bean in the spring context (autowired, using the qualifier to get the right instance), the second method echoTestManually creates a proxy bean manually.

Both of them use the remoted proxy and not the local service implementation during the test.

I used maven to setup the eclipse project and to manage my project dependencies.
The project can be downloaded here.

3 thoughts on “How to use the SimpleHttpInvokerServiceExporter”

  1. Great article. I was looking for just this. But a question. It works the first time. I made a small modification and ran the client again and I get:

    Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘httpServer’ defined in class path resource [beans-jvmserver.xml]: Invocation of init method failed; nested exception is Address already in use: bind

    Seems like the factory creates the bean and then you can’t modify your class using this method. Any suggestions? Thnx.

Leave a Reply

Your email address will not be published. Required fields are marked *