您的位置:首页 > 编程语言 > Java开发

JAVA反射示例四——结合代理模式的远程方法调用事例

2009-12-10 17:18 1121 查看
package proxy1;
import java.io.*;
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 proxy1;
import java.util.Date;
public interface HelloService{
public String echo(String msg)throws RemoteException;
public Date getTime()throws RemoteException;
}

package proxy1;
import java.util.Date;
public class HelloServiceImpl implements HelloService{
public String echo(String msg){
return "echo:"+msg;
}
public Date getTime(){
return new Date();
}
}


Connector类进行连接读写对象

package proxy1;
import java.io.*;
import java.net.*;
import java.util.*;
public class Connector {
private String host;
private int port;
private Socket skt;
private InputStream is;
private ObjectInputStream ois;
private OutputStream os;
private ObjectOutputStream oos;
public Connector(String host,int port)throws Exception{
this.host=host;
this.port=port;
connect(host,port);
}
public void send(Object obj)throws Exception{
oos.writeObject(obj);
}
public Object receive() throws Exception{
return ois.readObject();
}
public void connect()throws Exception{
connect(host,port);
}
public void connect(String host,int port)throws Exception{
skt=new Socket(host,port);
os=skt.getOutputStream();
oos=new ObjectOutputStream(os);
is=skt.getInputStream();
ois=new ObjectInputStream(is);
}
public void close(){
try{
}finally{
try{
ois.close();
oos.close();
skt.close();
}catch(Exception e){
System.out.println("Connector.close: "+e);
}
}
}
}


静态代理类

package proxy1;
import java.util.Date;
public class HelloServiceProxy implements HelloService{
private String host;
private int port;

public HelloServiceProxy(String host,int port){
this.host=host;
this.port=port;
}
public String echo(String msg)throws RemoteException{
Connector connector=null;
try{
connector=new Connector(host,port);
Call call=new Call("proxy1.HelloService","echo",new Class[]{String.class},new Object[]{msg});
connector.send(call);
call=(Call)connector.receive();
Object result=call.getResult();
if(result instanceof Throwable)
throw new RemoteException((Throwable)result);
else
return (String)result;
}catch(Exception e){
throw new RemoteException(e);
}finally{if(connector!=null)connector.close();}
}
public Date getTime()throws RemoteException{
Connector connector=null;
try{
connector=new Connector(host,port);
Call call=new Call("proxy1.HelloService","getTime",new Class[]{},new Object[]{});
connector.send(call);
call=(Call)connector.receive();
Object result=call.getResult();
if(result instanceof Throwable)
throw new RemoteException((Throwable)result);
else
return (Date)result;
}catch(Exception e){
throw new RemoteException(e);
}finally{if(connector!=null)connector.close();}
}
}


动态代理类:

package proxy1;
import java.lang.reflect.*;
public class ProxyFactory {
public static Object getProxy(final Class classType,final String host,final int port){
InvocationHandler handler=new InvocationHandler(){
public Object invoke(Object proxy,Method method,Object args[])
throws Exception{
Connector connector=null;
try{
connector=new Connector(host,port);
Call call=new Call(classType.getName(),
method.getName(),method.getParameterTypes(),args);
connector.send(call);
call=(Call)connector.receive();
Object result=call.getResult();
if(result instanceof Throwable)
throw new RemoteException((Throwable)result);
else
return result;
}finally{if(connector!=null)connector.close();}
}
};
return Proxy.newProxyInstance(classType.getClassLoader(),
new Class[]{classType},
handler);
}
}


自定义异常

package proxy1;
public class RemoteException extends Exception{
public RemoteException(Throwable e){
super(e);
}
}


客户端类

package proxy1;
import java.io.*;
import java.net.*;
import java.util.*;
public class SimpleClient {
public static void main(String args[])throws Exception {
//创建静态代理类实例
HelloService helloService1=new HelloServiceProxy("localhost",8000);
System.out.println(helloService1.echo("hello"));
System.out.println(helloService1.getTime());
//创建动态代理类实例
HelloService helloService2=
(HelloService)ProxyFactory.getProxy(HelloService.class,"localhost",8000);
System.out.println(helloService2.echo("hello"));
System.out.println(helloService2.getTime());
}
}


服务器端类

package proxy1;
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.reflect.*;
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(8000);
System.out.println("服务器启动.");
while(true){
Socket socket=serverSocket.accept();
InputStream in=socket.getInputStream();
ObjectInputStream ois=new ObjectInputStream(in);
OutputStream out=socket.getOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(out);
Call call=(Call)ois.readObject();
System.out.println(call);
call=invoke(call);
oos.writeObject(call);

ois.close();
oos.close();
socket.close();
}
}
public Call invoke(Call call){
Object result=null;
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("proxy1.HelloService",new HelloServiceImpl());
server.service();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: