jdk官网上有详细介绍:https://docs.oracle.com/javase/tutorial/rmi/overview.html,有什么不明白的地方或者一些概念可参见官网,下面主要是我写的一个例子,分为Server和Client两个部分,实现的功能是从Server读取一个文件,在Client端显示到Console。最后的代码我会上传到github,大家可以下载下来,有两个文件夹RMI_Server和RMI_Client,里面分别放了源码和打好的jar,可以直接运行jar包来显示效果。文章最后给出使用方法
GitHub地址:https://github.com/sunny123123/RMI.git
一、RMI工作流程
二、RMI实现要满足下面两个条件
1、继承java.rmi.Remote
2、接口的方法要抛出java.rmi.RemoteException
三、RMI实现4个步骤
- Designing and implementing the components of your distributed application.
- Compiling sources.
- Making classes network accessible.
- Starting the application.
四、示例:
4.1 server端,read函数一次读取一行
//ReadDataI.java
public interface ReadDataI extends Remote{
/*
* Must throw execption,else will throw below exception remote object implements illegal remote interface; nested exception is:
*/
public String read() throws RemoteException;
public void setFileName(String fileName) throws RemoteException;
public int init() throws RemoteException;
}
<pre name="code" class="html" style="font-size: 12.8px; line-height: 19.2px;">//ReadDataImpl.java
<pre name="code" class="html">public class ReadDataImpl implements ReadDataI {
FileInputStream input = null;
BufferedReader read = null;
InputStreamReader inputStreamReader = null;
String fileName = null;
public int init(){
if(fileName==null)
return -1;
File file = new File(fileName);
try {
input = new FileInputStream(file);
inputStreamReader = new InputStreamReader(input, "UTF-8");
read = new BufferedReader(inputStreamReader);
} catch (FileNotFoundException e) {
//e.printStackTrace();
System.out.println("can not find file "+ fileName);
return -1;
} catch (UnsupportedEncodingException e) {
//e.printStackTrace();
System.out.println("can not encode");
return -2;
}
return 1;
}
@Override
public String read() {
String buf = null;
try {
buf = read.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return buf;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
/* @Test
public void test(){
setFileName("src/DNS_6.6.6");
init();
String line = null;
do{
line = read();
System.out.println(line);
}while(line!=null);
} */
</pre><pre code_snippet_id="1682661" snippet_file_name="blog_20160513_5_3556137" name="code" class="html">//<pre name="code" class="html" style="font-size: 12.8px; line-height: 19.2px;">RMI_Server.java
public class RMI_Server{
public static void main(String[] args) {
ReadDataI read = new ReadDataImpl();
try {
ReadDataI stub = (ReadDataI)UnicastRemoteObject.exportObject(read, 0);
//Registry registery = LocateRegistry.getRegistry();
Registry registery = LocateRegistry.createRegistry(2001);
registery.rebind("read", stub);
System.out.println("object bound");
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
</pre>4.2 Client端,调用远程对象<pre>
<pre name="code" class="html">public class RMI_Client {
public static void main(String[] args) {
try {
if(args.length<2){
System.out.println("please input destination ip and file name");
return;
}
Registry registery = LocateRegistry.getRegistry(args[0],2001);
//System.out.println(System.getProperty("user.dir"));
ReadDataI read = (ReadDataI)registery.lookup("read");
read.setFileName(args[1]);
int flag = read.init();
if(flag==-1){
System.out.println("can not find file "+args[1]);
return;
}
if(flag==-2){
System.out.println("error encode");
return;
}
String line = null;
line = read.read();
while(line!=null){
System.out.println(line);
line = read.read();
}
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
五、运行示例:
将RMI_Server下的RMI_Server.jar放到一台服务器上,我这里是10.124.22.213
将RMI_Client 下的RMI_Clienr.jar 放到另外一台机器上,我这里是10.75.7.64
在10.124.22.213上:
在10.75.7.64上:
ip地址和文件名称需要自已指定