emoting is an essential component of enterprise distributed computing. A service or class you call from within the same server (Java virtual machine) doesn't need to be exposed as a remote service, but if you need to communicate with an external program (on a different server or in a different organization), it has to be implemented as a remote service. The Spring framework
provides a unique and flexible way of exposing business classes as remote services.
At the heart of the Spring remoting architecture are service objects, which are POJOs also known as Spring beans. The Spring framework insulates these service objects from infrastructure details, such as how they are exposed as remote services, so the developer can concentrate on the business interface of the service object rather than getting bogged down with these details.
This remoting model provides remote abstraction to business services. It takes care of marshalling and unmarshalling method arguments and also handles any exceptions thrown in the service methods, wrapping them with unchecked RemoteAccessException. To implement a variety of services, Spring uses several design patterns. For example, it uses the proxy design pattern to translate your calls to HTTP POST requests to the URL pointing to the exported service.
This article explains how to implement a remote service using Spring. It demonstrates using the Spring Remoting API to convert a plain old Java object (POJO) into a remote service, which external programs can call for its business function. The demonstration uses a sample loan-processing application to implement a business service as a remote HTTP service and invoke the business methods in the class from a test client.
How Does Spring Remoting Work?
Let's take a closer look at how the Spring remoting mechanism works. It provides the following elements to implement a Java class as a remote service:
- Remote Service Exporters These classes are used to create the remote service end-points for the client programs to call. Service exporters also manage any registries to look up the remote services.
- Proxy Factory Beans These are the factory classes used to create proxies that clients can use to connect to the remote services.
- HTTP Invoker As previously mentioned, Spring HTTP invoker employs a remoting model that you can use to make remote calls across HTTP and at the same time pass Java objects using Java serialization techniques. It makes implementing a remote service from a plain Java class a lot easier, and allows you to concentrate on the business interface of the remote service rather than remoting infrastructure details. It relies on the infrastructure of RMI invokers, but uses HTTP as the transport protocol.
On the client side, Spring HTTP invoker provides two types of clients: the standard API provided by Java SE and the Commons HttpClient API. By default, it uses HttpClient.
See Sidebar 1. Supported Remoting Technologies for a full listing of available remoting technologies for Spring.