传统的实现RMI,需要
1.服务接口必须从Remote派生,每个方法抛出RemoteException
2.实现类必须从UnicastRemoteObject派生
3.所有方法的参数和返回值,必须是基本类型,或者实现了Serializable接口
Spring对RMI提供的支持
不用写一行代码,直接在Spring的配置文件中声明,就可以将一个传统的java类作为RMI服务输出
唯一编写的代码是main()方法,启动Spring容器
客户端代码
如果客户端也在Spring容器中启动,完全可以在XML配置文件中定义UserService并直接使用
RMI虽然是Java标准的远程调用模式,但是它使用特定的Java Remote Method Protocol二进制协议,很难穿透防火墙,如果要跨防火墙,应该使用HTTP协议为基础的远程调用。
Hessian; Burlap; Spring HTTP Invoker。使用私有协议的Http远程调用没有成为标准,实际应用较少,只能用于Java应用程序之间的远程调用。如果希望和异构平台实现远程调用,就必须使用标准的Web服务(Web Services)
1.服务接口必须从Remote派生,每个方法抛出RemoteException
2.实现类必须从UnicastRemoteObject派生
3.所有方法的参数和返回值,必须是基本类型,或者实现了Serializable接口
public class User implements Serializable {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
public interface RmiUserService extends Remote {
User login(String username, String password) throws RemoteException;
void create(String username, String password) throws RemoteException;
}
public class RmiUserServiceImpl extends UnicastRemoteObject implements RmiUserService {
protected RmiUserServiceImpl() throws RemoteException {
}
private Map<String, String> users = new HashMap<String, String>();
public void create(String username, String password) {
if (username == null || password == null)
throw new IllegalArgumentException("Invalid args.");
if (users.get(username) != null)
throw new RuntimeException("User exist!");
users.put(username, password);
}
public User login(String username, String password) {
if (username == null || password == null)
throw new IllegalArgumentException("Invalid args.");
if (password.equals(users.get(username)))
return new User(username, password);
throw new RuntimeException("Login failed.");
}
public static void main(String[] args) throws RemoteException, MalformedURLException, AlreadyBoundException {
LocateRegistry.createRegistry(1099);
Naming.bind("rmi://localhost:1099/UserService", new RmiUserServiceImpl());
}
}
public class Client {
public static void main(String[] args) throws RemoteException, MalformedURLException, NotBoundException {
RmiUserService service = (RmiUserService) Naming.lookup("rmi://localhost:1099/UserService");
service.create("xace", "1");
System.out.println(service.login("xace", "1"));
}
}
调用:
>rmic RmiUserServiceImpl
>java Client
Spring对RMI提供的支持
不用写一行代码,直接在Spring的配置文件中声明,就可以将一个传统的java类作为RMI服务输出
<bean id="userService" class="example.rmi.UserServiceImpl" />
<bean id="rmiService" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="UserService"/>
<property name="service" ref="userService"/>
<property name="serviceInterface" value="example.rmi.UserService"/>
<property name="registryPort" value="1099"/>
</bean>
唯一编写的代码是main()方法,启动Spring容器
public static void main(String[] args) {
new ClassPathXmlApplicationContext("config.xml");
}
客户端代码
public class Client {
public static void main(String[] args) throws Exception {
RmiProxyFactoryBean factory = new RmiProxyFactoryBean();
factory.setServiceInterface(UserService.class);
factory.setServiceUrl("rmi://localhost:1099/UserService");
factory.afterPropertiesSet();
UserService service = (UserService) factory.getObject();
service.create("test", "password");
System.out.println(service.login("test", "password"));
try {
service.login("test", "bad-password");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
如果客户端也在Spring容器中启动,完全可以在XML配置文件中定义UserService并直接使用
<bean id="userServiceRmi" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://localhost:1099/UserService" />
<property name="serviceInterface" value="example.rmi.UserService" />
</bean>
RMI虽然是Java标准的远程调用模式,但是它使用特定的Java Remote Method Protocol二进制协议,很难穿透防火墙,如果要跨防火墙,应该使用HTTP协议为基础的远程调用。
Hessian; Burlap; Spring HTTP Invoker。使用私有协议的Http远程调用没有成为标准,实际应用较少,只能用于Java应用程序之间的远程调用。如果希望和异构平台实现远程调用,就必须使用标准的Web服务(Web Services)