zoukankan      html  css  js  c++  java
  • 通过java反射调用远程方法

    整体通信过程如下图:

    package search;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.lang.reflect.Method;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.HashMap;
    import java.util.Map;
    
    
    public class SimpleServer 
    {
    	private Map remoteObjects=new HashMap();
    	public void register(String className,Object remoteObject)
    	{
    		remoteObjects.put(className, remoteObject);
    	}
    	public void service()throws Exception
    	{
    		ServerSocket serverSocket=new ServerSocket(8003);
    		System.out.println("server has started");
    		while(true)
    		{
    			Socket socket=serverSocket.accept();
    			InputStream in=socket.getInputStream();
    			ObjectInputStream oin=new ObjectInputStream(in);
    			Call call=(Call) oin.readObject();
    			System.out.println(call);
    			OutputStream out=socket.getOutputStream();
    			ObjectOutputStream oout=new ObjectOutputStream(out);
    			call=invoke(call);
    			oout.writeObject(call);
    			
    			oin.close();
    			oout.close();
    			socket.close();
    		}
    		
    	}
    	private Call invoke(Call call)
    	{
    		Object result;
    		try
    		{
    			String className=call.getClassName();
    			String methodName=call.getMethodName();
    			Object[]params=call.getParams();
    			Class classType=Class.forName(className);
    			Class[]paramTypes=call.getParamTypes();
    			Method method=classType.getMethod(methodName,paramTypes);
    			Object remoteObject=remoteObjects.get(className);
    			if(remoteObject==null)
    			{
    				throw new Exception(className+"的远程对象不存在");
    			}
    			else
    			{
    				result=method.invoke(remoteObject,params);
    			}
    		}catch(Exception e)
    		{
    			result=e;
    		}
    		call.setResult(result);
    		return call;
    	}
    	public static void main(String[]args) throws Exception
    	{
    		
    			SimpleServer server=new SimpleServer();
    			server.register("search.HelloService", new HelloServiceImpl());
    			server.service();
    		
    	}
    }
    
    package search;
    
    import java.util.Date;
    
    public class HelloServiceImpl implements HelloService
    {
    
    	public String echo(String msg) 
    	{
    		
    		return "echo "+msg;
    	}
    
    	
    	public Date getTime() 
    	{
    		return new Date();
    	}
    
    }
    
    package search;
    
    import java.util.Date;
    
    public interface HelloService 
    {
    	public String echo(String msg);
    	public Date getTime();
    }
    
    package search;
    
    import java.io.Serializable;
    
    public class Call implements Serializable
    {
    	private String className;//表示类名或接口名
    	private String methodName;//表示方法名
    	private Class[]paramTypes;//表示方法参数类型
    	private Object[]params;//表示方法参数值
    	//表示方法的执行结果
    	private Object result;
    	public Call(){}
    	public Call(String className,String methodName,Class[]paramTypes,Object[]params)
    	{
    		this.className=className;
    		this.methodName=methodName;
    		this.paramTypes=paramTypes;
    		this.params=params;
    	}
    	public String getClassName()
    	{
    		return className;
    	}
    	public void setClassName(String className) {
    		this.className = className;
    	}
    	public String getMethodName() {
    		return methodName;
    	}
    	public void setMethodName(String methodName) 
    	{
    		this.methodName = methodName;
    	}
    	public Class[] getParamTypes() {
    		return paramTypes;
    	}
    	public void setParamTypes(Class[] paramTypes) {
    		this.paramTypes = paramTypes;
    	}
    	public Object[] getParams() {
    		return params;
    	}
    	public void setParams(Object[] params) {
    		this.params = params;
    	}
    	public Object getResult() {
    		return result;
    	}
    	public void setResult(Object result) {
    		this.result = result;
    	}
    	public String toString()
    	{
    		return "classname= "+className+" methodname= "+methodName;
    	}
    	
    }
    
    package search;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class SimpleClient 
    {
    	public void invoke()
    	{
    		try
    		{
    			Socket socket=new Socket("localhost",8003);
    			InputStream in=socket.getInputStream();
    			OutputStream out=socket.getOutputStream();
    			ObjectOutputStream oout=new ObjectOutputStream(out);
    			Call call=new Call("search.HelloService","echo",new Class[]{String.class},new Object[]{"hello"});
    			System.out.println(call);
    			oout.writeObject(call);
    			ObjectInputStream oin=new ObjectInputStream(in);
    			call=(Call)oin.readObject();
    			System.out.println(call.getResult());
    			oin.close();
    			oout.close();
    			socket.close();
    		} catch (UnknownHostException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    	}
    	public static void main(String[]args)
    	{
    		new SimpleClient().invoke();
    	}
    }
    


  • 相关阅读:
    基于RSA securID的Radius二次验证java实现(PAP验证方式)
    一些指令 & 一些知识 (Linux Spring log4j...)
    RSA, ACS5.X 集成配置
    Python中的动态属性与描述符
    设计模式(一):单例模式
    JavaWeb
    动态规划_背包问题
    动态规划_最长上升子序列
    MySQL复习
    动态规划_数字三角形
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3074182.html
Copyright © 2011-2022 走看看