Spring Book – Chapter 18 – Remoting

Spring’s HttpInvoker

Spring’s HttpInvoker use standard Java serialization mechanism to expose services through the lighter HTTP protocol. Similar to RMI, Spring supports HttpInvoker infrastructure via org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean and org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter.

HttpInvokerProxyFactoryBean is the FactoryBean for HTTP invoker proxies. This class exposes the proxied service for use as a bean reference, using the specified service interface. The service URL must be an HTTP URL exposing an HTTP invoker service. Optionally, a codebase URL can be specified for on-demand dynamic code download from a remote location.

HttpInvokerServiceExporter is the Servlet-API-based HTTP request handler that exports the specified service bean as HTTP invoker service endpoint, accessible via an HTTP invoker proxy. Spring also provides an alternative version of this exporter, for Sun’s JRE 1.6 HTTP server namely org.springframework.remoting.httpinvoker.SimpleHttpInvokerServiceExporter.

HttpInvoker is the recommended protocol for Java-to-Java remoting. It is more powerful and more extensible than Hessian and Burlap, at the expense of being tied to Java. Nevertheless, it is as easy to set up as Hessian and Burlap, which is its main advantage compared to RMI. Instead of the custom serialization found in Hessian and Burlap, HTTP invoker uses Java serialization similar to RMI.

Because of the nature of HttpInvoker, both the client side and the server side need to be based on Spring and on Java because of the use of Java serialization. In contrast to Hessian and Burlap, there is no option for cross-platform remoting using HttpInvoker.

We will now see the step-by-step configuration required to expose our sample application’s LoyaltyService as a remote service using Spring’s HttpInvoker as the remoting technology. The various steps remain the same as explained in section earlier, only there is change in various classes being used. The steps are as follows:

1. In the Spring Configuration file applicationContext.xml, define a bean which needs to be exposed as a remote service using Spring remoting. Listing 19-5 below shows exposing LoyaltyService bean as remote service in the server side.

Listing 19-5. Bean configuration of LoyaltyService

2. In the Spring configuration file, now declare the service exported bean according to the remoting technology used using the bean declared in Step1. Listing 19-6 shows configuring the service exporter for RMI in the server side.

Listing 19-6. Configuring HttpInvoker service exporter as a bean

3. In this step, in the Spring configuration file, declare the client proxy bean according to the remoting technology used. Listing 19-7 below shows declaring client proxy in the client layer.

Listing 19-7. Declaring client proxy

4. Now in your client, inject the factory bean declared in Step 3 and use the appropriate method in the exposed remote service. Listing 19-8 shows declaring of the client bean in which client proxy is injected to be used for calling service methods.

Listing 19-8. Injecting client proxy in the client class

Addtionally, if you want to set the various connection properties while using HttpInvoker, the HttpInvokerProxyFactoryBean takes in httpInvokerRequestExecutor as a parameter which in turn takes in httpClient(org.apache.commons.httpclient.HttpClient). Hence all properties for HttpClient as timeout, connectiontime etc can be set. Listing 19-9 below shows setting connection timeout in HttpInvoker set to 10 seconds.

Listing 19-9. Setting connection timeout to 10 seconds while using HttpInvoker

Page Visitors: 4814

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

Leave a Reply

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