MOOC华东师范大学 Java核心技术(进阶)第八章作业 基于RMI技术,实现客户端和服务端功能

题目:

基于RMI技术,实现客户端和服务端功能。

服务端需要提供编译一个类和运行一个类的功能(采用Runtime的exec方法)。

客户端接受用户的输入,如输入javac HelloWorld,就通过RMI方法请求服务端的编译HelloWorld功能;

如输入java HelloWorld,就通过RMI方法请求服务端的执行HelloWorld功能。服务端的执行结果,返回给客户端,并打印出来。

服务端代码:

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

/**
 * 基于RMI技术,实现客户端和服务端功能。
 * 服务端需要提供编译一个类和运行一个类的功能(采用Runtime的exec方法)。
 * 客户端接受用户的输入,如输入javac HelloWorld,
 * 就通过RMI方法请求服务端的编译HelloWorld功能;
 * 如输入java HelloWorld,就通过RMI方法请求服务端的执行HelloWorld功能。
 * 服务端的执行结果,返回给客户端,并打印出来。
 * @author MSS
 *
 */
public class WarehouseServer {

	public static void main(String[] args) throws Exception {
		System.out.println("产生服务器对象");
		WarehouseImpl centralWarehouse=new WarehouseImpl();
		
		System.out.println("将服务器对象绑定在8001端口,对外提供服务");
		LocateRegistry.createRegistry(8001);//定义端口
		Naming.rebind("rmi://127.0.0.1:8001/warehouse1", centralWarehouse);
		System.out.println("等待客户端");
		
	}

}

Remote接口和实现类:

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;

public interface Warehouse extends Remote{
    List<String> cmdExec(String cmd)throws RemoteException;
}
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;

public class WarehouseImpl extends UnicastRemoteObject implements Warehouse{
    String cmd1="javac HelloWorld";
    String cmd2="java HelloWorld";
	protected WarehouseImpl() throws RemoteException {
		super();
		
	}

	@Override
	public List<String> cmdExec(String cmd) throws RemoteException {
		List<String> result=new ArrayList<String>();
		if(cmd1.equals(cmd)) {
			Process p;
			String[] cmds=new String[2];
			cmds[0]="javac";
			cmds[1]="D:/temp/HelloWord.java";
			
			try {
				//执行命令
				p=Runtime.getRuntime().exec(cmds);
				//取得命令结果的输出流
				InputStream fis=p.getInputStream();
				//用一个读输出流去读
				InputStreamReader isr=new InputStreamReader(fis);
				//用缓冲器读行
				BufferedReader br=new BufferedReader(isr);
				String line=null;
				//直到读完为止
				while((line=br.readLine())!=null) {
					result.add(line);
				}
				System.out.println("");
				int exitVal=p.waitFor(); //获取进程最后返回状态
				System.out.println("Process exitValue: "+exitVal);
			}catch(Exception e) {
				e.printStackTrace();
			}
			result.add("编译成功");
			return result;
		}else if(cmd2.equals(cmd)){
			Process p;
			String[] cmds=new String[2];
			cmds[0]="java";
			cmds[1]="HelloWorld";
			
			try {
				//执行命令
				p=Runtime.getRuntime().exec(cmds,null,new File("D:/temp"));
				//取得命令结果的输出流
				InputStream fis=p.getInputStream();
				//用一个读输出流去读
				InputStreamReader isr=new InputStreamReader(fis);
				//用缓冲器读行
				BufferedReader br=new BufferedReader(isr);
				String line=null;
				//直到读完为止
				while((line=br.readLine())!=null) {
					result.add(line);
					System.out.println(line);
				}
				System.out.println("");
				int exitVal=p.waitFor(); //获取进程最后返回状态
				System.out.println("Process exitValue: "+exitVal);
			}catch(Exception e) {
				e.printStackTrace();
			}
			result.add("执行成功");
			return result;

		}else {
			Process p;
			
			
			try {
				//执行命令
				p=Runtime.getRuntime().exec(cmd);
				//取得命令结果的输出流
				InputStream fis=p.getInputStream();
				//用一个读输出流去读
				InputStreamReader isr=new InputStreamReader(fis);
				//用缓冲器读行
				BufferedReader br=new BufferedReader(isr);
				String line=null;
				//直到读完为止
				while((line=br.readLine())!=null) {
					result.add(line);
				}
				System.out.println("");
				int exitVal=p.waitFor(); //获取进程最后返回状态
				System.out.println("Process exitValue: "+exitVal);
			}catch(Exception e) {
				e.printStackTrace();
			}
			return result;
		}
		
	}

}

客户端代码:

import java.rmi.RemoteException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.Scanner;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingException;

/**
 * 基于RMI技术,实现客户端和服务端功能。
 * 服务端需要提供编译一个类和运行一个类的功能(采用Runtime的exec方法)。
 * 客户端接受用户的输入,如输入javac HelloWorld,
 * 就通过RMI方法请求服务端的编译HelloWorld功能;
 * 如输入java HelloWorld,就通过RMI方法请求服务端的执行HelloWorld功能。
 * 服务端的执行结果,返回给客户端,并打印出来。
 * @author MSS
 *
 */
public class WarehouseClient {

	public static void main(String[] args)throws NamingException,RemoteException{
		Context namingContext=new InitialContext();
        
		//开始查找RMI注册表上有哪些绑定的服务
		System.out.print("RMI 注册表绑定列表:");
		Enumeration<NameClassPair>e=namingContext.list("rmi://127.0.0.1:8001/");
		while(e.hasMoreElements()) {
			System.out.println(e.nextElement().getName());
		}
		//获取某一地址上的服务类
		String url="rmi://127.0.0.1:8001/warehouse1";
		Warehouse centralWarehouse=(Warehouse) namingContext.lookup(url);
		
		Scanner in=new Scanner(System.in);
		System.out.println("Enter keywords:");
		String kewords=in.nextLine();
		List<String> result=centralWarehouse.cmdExec(kewords);
		for(String res:result) {
			System.out.println(res);
		}
		in.close();
		
	}

}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值