通过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();
	}
}


 

 

posted @ 2013-05-12 16:31  javawebsoa  Views(401)  Comments(0Edit  收藏  举报