Spring实现RMI调用

传统的实现RMI,需要
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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值