java RMI

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个步骤

  1. Designing and implementing the components of your distributed application.
  2. Compiling sources.
  3. Making classes network accessible.
  4. 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地址和文件名称需要自已指定

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值