29-03-2012, 01:10 PM
Remoting and web services using Spring
Remoting and web services using 23.docx (Size: 36.41 KB / Downloads: 41)
Introduction
Spring features integration classes for remoting support using various technologies. The remoting support eases
the development of remote-enabled services, implemented by your usual (Spring) POJOs. Currently, Spring
supports four remoting technologies:
• Remote Method Invocation (RMI). Through the use of the RmiProxyFactoryBean and the
RmiServiceExporter Spring supports both traditional RMI (with java.rmi.Remote interfaces and
java.rmi.RemoteException) and transparent remoting via RMI invokers (with any Java interface).
• Spring's HTTP invoker. Spring provides a special remoting strategy which allows for Java serialization via
HTTP, supporting any Java interface (just like the RMI invoker). The corresponding support classes are
HttpInvokerProxyFactoryBean and HttpInvokerServiceExporter.
• Hessian. By using Spring's HessianProxyFactoryBean and the HessianServiceExporter you can
transparently expose your services using the lightweight binary HTTP-based protocol provided by Caucho.
• Burlap. Burlap is Caucho's XML-based alternative to Hessian. Spring provides support classes such as
BurlapProxyFactoryBean and BurlapServiceExporter.
Exposing services using RMI
Using Spring's support for RMI, you can transparently expose your services through the RMI infrastructure.
After having this set up, you basically have a configuration similar to remote EJBs, except for the fact that there
is no standard support for security context propagation or remote transaction propagation. Spring does provide
hooks for such additional invocation context when using the RMI invoker, so you can for example plug in
security frameworks or custom security credentials here.
17.2.1. Exporting the service using the RmiServiceExporter
Using the RmiServiceExporter, we can expose the interface of our AccountService object as RMI object. The
interface can be accessed by using RmiProxyFactoryBean, or via plain RMI in case of a traditional RMI
service. The RmiServiceExporter explicitly supports the exposing of any non-RMI services via RMI invokers.
Linking in the service on the client
Using the we can link in the service at the client. The same principles apply as with the RMI example. We'll
create a separate bean factory or application context and mention the following beans where the SimpleObject
is using the AccountService to manage accounts:
Accessing web services using JAX-RPC
Spring provides two factory beans to create JAX-RPC web service proxies, namely
LocalJaxRpcServiceFactoryBean and JaxRpcPortProxyFactoryBean. The former can only return a JAX-RPC
service class for us to work with. The latter is the full-fledged version that can return a proxy that implements
our business service interface. In this example we use the latter to create a proxy for the AccountService
endpoint we exposed in the previous section.
Exporting standalone web services using JAX-WS
The built-in JAX-WS provider that comes with Sun's JDK 1.6 supports exposure of web services using the
built-in HTTP server that's included in JDK 1.6 as well. Spring's SimpleJaxWsServiceExporter detects all
@WebService annotated beans in the Spring application context, exporting them through the default JAX-WS
server (the JDK 1.6 HTTP server).
Considerations when choosing a technology
Each and every technology presented here has its drawbacks. You should carefully consider you needs, the
services your exposing and the objects you'll be sending over the wire when choosing a technology.
When using RMI, it's not possible to access the objects through the HTTP protocol, unless you're tunneling the
RMI traffic. RMI is a fairly heavy-weight protocol in that it support full-object serialization which is important
when using a complex data model that needs serialization over the wire. However, RMI-JRMP is tied to Java
clients: It is a Java-to-Java remoting solution.