你的位置:首页 > 软件开发 > Java > 使用RMI进行分布式交互

使用RMI进行分布式交互

发布时间:2015-12-07 14:00:11
内容概要:RMI 示例RMI 架构RMI APIRemote    RemoteExceptionRegistry, LocateRegistryNaming RMI 示例 在了解RMI之前,先来看一个例子:// 服务接口package com.fjn.java.r ...

使用RMI进行分布式交互

内容概要

  1. Remote    
  2. RemoteException
  3. Registry, LocateRegistry
  4. Naming

 

 

RMI 示例

 在了解RMI之前,先来看一个例子:

// 服务接口package com.fjn.java.rmi.quickstart.server;import java.rmi.Remote;import java.rmi.RemoteException;public interface Hello extends Remote{  public String sayHello(String str) throws RemoteException;}// 服务实现package com.fjn.java.rmi.quickstart.server;import java.io.Serializable;import java.rmi.RemoteException;public class HelloImpl implements Hello, Serializable {  protected HelloImpl() throws RemoteException {    super();  }  private static final long serialVersionUID = 3556503295294925414L;  @Override  public String sayHello(String str) {    return "Hello, "+str;  }} // Server端发布服务:package com.fjn.java.rmi.quickstart.server;import java.net.MalformedURLException;import java.rmi.AlreadyBoundException;import java.rmi.NotBoundException;import java.rmi.RemoteException;import java.rmi.registry.LocateRegistry;import java.rmi.registry.Registry;import java.rmi.server.UnicastRemoteObject; public class ServerTest {  public static void main(String[] args) throws RemoteException, AlreadyBoundException, InterruptedException, MalformedURLException, NotBoundException {    // 创建Registry    Registry registry=LocateRegistry.createRegistry(9998);      Hello stub=(Hello) UnicastRemoteObject.exportObject(new HelloImpl(),0);    // 将ref绑定到registry中    registry.rebind("Hello", stub);    Hello stub2=(Hello)registry.lookup("Hello");    System.out.println(stub2);    Thread.sleep(1000*60*60);  }}

原标题:使用RMI进行分布式交互

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录