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
1 2 3 |
<bean id="loyaltyService"> <property name="customerRepository" ref="customerRepository"/> </bean> |
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
1 2 3 4 5 |
<bean name="/loyaltyService"> <property name="serviceInterface" value="com.mybook.loyalty.services.LoyaltyService"/> <property name="service" ref="loyaltyService"/> </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
1 2 3 4 5 6 |
<bean id="myService"> <property name="serviceInterface" value="com.mybook.loyalty.sevices.LoyaltyService"/> <property name="serviceUrl" value="rmi://host:8080/loyalty/services/loyaltyService"/> </bean> |
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
1 2 3 |
<bean id="myDesktopUI"> <property name="loyaltyService" ref="loyaltyService"/> </bean> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<bean id="httpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor"> <property name="httpClient"> <bean class="org.apache.commons.httpclient.HttpClient"> <property name="timeout" value="10000" /> </bean> </property> </bean> <bean class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://host:8080/loyalty/services/loyaltyService" /> <property name="serviceInterface" value="com.mybook.loyalty.services.LoyaltyService" /> <property name="httpInvokerRequestExecutor" ref="httpInvokerRequestExecutor" /> </bean> |
Page Visitors: 4713

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023