Java Reference
In-Depth Information
Considering the typical RMI usage scenario, to expose a service through RMI, you have to
create the service interface that extends java.rmi.Remote and whose methods declare throwing
java.rmi.RemoteException . Then you create the service implementation for this interface. After that,
you start an RMI registry and register your service to it. As you can see, there are quite a lot of steps
required for exposing a simple service.
To invoke a service through RMI, you first look up the remote service reference in an RMI registry,
and then you can call the methods on it. However, to call the methods on a remote service, you must
handle java.rmi.RemoteException in case of any exception thrown by the remote service.
Fortunately, Spring's remoting facilities can significantly simplify the RMI usage on both the server
and client sides. On the server side, you can use RmiServiceExporter to export a Spring bean as an RMI
service, whose methods can be invoked remotely. It's just several lines of bean configuration without
any programming. Beans exported in this way don't need to implement java.rmi.Remote or throw
java.rmi.RemoteException . On the client side, you can simply use RmiProxyFactoryBean to create a proxy
for the remote service. It allows you to use the remote service as if it were a local bean. Again, it requires
no additional programming at all.
How It Works
Suppose you are going to build a weather web service for clients running on different platforms to
invoke. This service includes an operation for querying a city's temperatures on multiple dates. First, you
create the TemperatureInfo class representing the minimum, maximum, and average temperatures of a
particular city and date.
package com.apress.springenterpriserecipes.weather;
...
public class TemperatureInfo implements Serializable {
private String city;
private Date date;
private double min;
private double max;
private double average;
// Constructors, Getters and Setters
...
}
Next, you define the service interface that includes the getTemperatures() operation, which returns
a city's temperatures on multiple dates as requested.
package com.apress.springenterpriserecipes.weather;
...
public interface WeatherService {
public List<TemperatureInfo> getTemperatures(String city, List<Date> dates);
}
You have to provide an implementation for this interface. In a production application, you probably
want to implement this service interface by querying the database. Here, you may hard-code the
temperatures for testing purposes.
Search WWH ::




Custom Search