概念:
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>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>java -classpath D:\workspace\RMI\bin SearchClient
Person [age=35, name=xiaoli]
注意事项: 如果参数或者返回结果是自定义对象,那该对象必须要实现java.io.Serializable接口
|