Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1119469
  • 博文数量: 284
  • 博客积分: 8223
  • 博客等级: 中将
  • 技术积分: 3188
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 13:26
文章分类

全部博文(284)

文章存档

2012年(18)

2011年(33)

2010年(83)

2009年(147)

2008年(3)

分类: Java

2010-09-02 17:03:57

概念:
Java远程方法调用,即Java RMI(Java Remote Method Invocation)是Java编程语言里,一种用于实现远程过程调用的应用程序编程接口。它使客户机上运行的程序可以调用远程服务器上的对象。远程方法调用特性使Java编程人员能够在网络环境中分布操作。RMI全部的宗旨就是尽可能简化远程接口对象的使用。

1.实现remote接口
RemoteSearcher.java

?[Copy to clipboard]View Code JAVAimport java.rmi.*;
public interface RemoteSearcher extends Remote {
Person search(String q) throws RemoteException;
}

DefaultRemoteSearcher.java

?[Copy to clipboard]View Code JAVAimport java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Map;
public class DefaultRemoteSearcher extends UnicastRemoteObject implements RemoteSearcher {
private static final long serialVersionUID = 1L;
private static final Map<String, Person> pool = new HashMap<String, Person>();
public DefaultRemoteSearcher() throws RemoteException {
super();
pool.put("xiaoli", new Person("xiaoli", 35));
pool.put("zhangsan", new Person("zhangsan", 52));
pool.put("abama", new Person("abama", 41));
}
public Person search(String q) throws RemoteException {
return pool.get(q);
}
}

2.编写服务端代码,绑定RMI对象
?[Copy to clipboard]View Code JAVA<strong>SearchServer.java</strong>
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
public class SearchServer {
private static final String RMI_URL = "rmi://172.16.102.6:10009/search";
public SearchServer() {
}
public static void main(String[] args) {
try {
// 创建远程对象

DefaultRemoteSearcher remoteSearcher = new DefaultRemoteSearcher();
// 启动注册表

LocateRegistry.createRegistry(10009);
// 奖名称绑定到对象

Naming.rebind(RMI_URL, remoteSearcher);
System.out.println("RMI started at " + RMI_URL);
} catch (Exception e) {
e.printStackTrace();
}
}
}

3.编写客户端代码,远程调用RMI对象
SearchClient.java

?[Copy to clipboard]View Code JAVAimport java.rmi.Naming;
public class SearchClient {
private static final String RMI_URL = "rmi://172.16.102.6:10009/search";
public SearchClient() {
}
public static void main(String[] args) {
try {
System.out.println(((RemoteSearcher) Naming.lookup(RMI_URL)).search("xiaoli"));
} catch (Exception e) {
e.printStackTrace();
}
System.exit(0);
}
}

person.java

?[Copy to clipboard]View Code JAVApublic class Person implements Serializable {
    private static final long serialVersionUID = 3069227031912694124L;
    private String name;
    private int age = 30;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person [age=" + age + ", name=" + name + "]";
    }
}

4.启动服务端
?[Copy to clipboard]View Code JAVAC:\Documents and Settings\Administrator&gt;java -classpath D:\workspace\RMI\bin SearchServer
RMI started at rmi://172.16.102.6:10009/search


5.执行客户端程序
?[Copy to clipboard]View Code JAVAC:\Documents and Settings\Administrator&gt;java -classpath D:\workspace\RMI\bin SearchClient
Person [age=35, name=xiaoli]

注意事项: 如果参数或者返回结果是自定义对象,那该对象必须要实现java.io.Serializable接口


阅读(968) | 评论(0) | 转发(0) |
0

上一篇:jni

下一篇:java-压缩

给主人留下些什么吧!~~