Util Class:
public class Call implements Serializable{
/**
* 表示类名或者接口名
*/
private String className;
/**
* 表示方法名
*/
private String methodName;
/**
* 表示方法参数类型
*/
private Class[] paramType;
/**
* 表示参数值
*/
private Object[] params;
/**
* 表示方法执行结果 如果方法正常执行,则返回正常值,如果抛出异常则返回该异常
*/
private Object result;
/**
* 无参构造方法
*/
public Call() {
}
/**
* 有参构造方法
*
* @param clasName
* @param methodName
* @param paramType
* @param params
*/
public Call(String className, String methodName, Class[] paramType,
Object[] params) {
this.className = className;
this.methodName = methodName;
this.paramType = paramType;
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[] getParamType() {
return paramType;
}
public void setParamType(Class[] paramType) {
this.paramType = paramType;
}
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;
}
@Override
public String toString() {
return "ClassName=" + className + ";methodName=" + methodName;
}
}
SocketServer:
package com.hou.server;
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;
import com.hou.service.impl.HelloServiceImpl;
import com.hou.util.Call;
public class SimplServer {
/**
* 存放远程对象的缓存
*/
private Map remoteObjecs = new HashMap();
/**
* 存放远程对象到缓存中
*
* @param className
* @param remoteObject
*/
public void register(String className, Object remoteObject) {
remoteObjecs.put(className, remoteObject);
}
public void service() throws Exception {
ServerSocket server = new ServerSocket(8000);
System.out.println("服务器启动...");
while (true) {
Socket socket = server.accept();
InputStream inputStream = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(inputStream);
OutputStream out = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
Call call = (Call) ois.readObject();
System.out.println("读取Obj=" + call);
call = this.invoke(call);
oos.writeObject(call);
ois.close();
oos.close();
socket.close();
}
}
/**
* Invoke execute method
*
* @param call
* @return
*/
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[] paramType = call.getParamType();
// 得到方法
Method method = classType.getMethod(methodName, paramType);
// 从缓存中取出相关的远程对象
// Object obj = remoteObjecs.get(className);
Object obj = classType.newInstance();
if (obj == null) {
throw new Exception(className + "远程对象不存在");
} else {
// 调用远程对象的方法
result = method.invoke(obj, params);
}
} catch (Exception e) {
System.out.println("出现错误了..." + e);
}
call.setResult(result);
return call;
}
public static void main(String args[]) throws Exception {
SimplServer server = new SimplServer();
server.register("com.hou.service.impl.HelloServiceImpl",
new HelloServiceImpl());
server.service();
}
}
SocketClient:
package com.hou.client;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
* 客户端
*/
import com.hou.util.Call;
public class SimpleClient {
public void invoke() throws Exception {
Socket socket = new Socket("localhost", 8000);
OutputStream out = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
InputStream in = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(in);
Call call = new Call("com.hou.service.impl.HelloServiceImpl", "echo",
new Class[] { String.class }, new Object[] { "猴哥" });
// 向服务器发送对象包
oos.writeObject(call);
// 接受服务器返回的对象包
call = (Call) ois.readObject();
System.out.println("返回结果:" + call.getResult());
ois.close();
oos.close();
socket.close();
}
public static void main(String args[]) throws Exception {
new SimpleClient().invoke();
}
}
Invoke Class:
public class HelloServiceImpl implements HelloService {
public String echo(String msg) {
return "Echo:"+msg;
}
public Date getTime() {
return new Date();
}
}
以上是利用java反射机制实现远程调用,RMI远程调用就是采用该机制。如果利用NIO远程调用时间效率更高。
public class Call implements Serializable{
/**
* 表示类名或者接口名
*/
private String className;
/**
* 表示方法名
*/
private String methodName;
/**
* 表示方法参数类型
*/
private Class[] paramType;
/**
* 表示参数值
*/
private Object[] params;
/**
* 表示方法执行结果 如果方法正常执行,则返回正常值,如果抛出异常则返回该异常
*/
private Object result;
/**
* 无参构造方法
*/
public Call() {
}
/**
* 有参构造方法
*
* @param clasName
* @param methodName
* @param paramType
* @param params
*/
public Call(String className, String methodName, Class[] paramType,
Object[] params) {
this.className = className;
this.methodName = methodName;
this.paramType = paramType;
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[] getParamType() {
return paramType;
}
public void setParamType(Class[] paramType) {
this.paramType = paramType;
}
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;
}
@Override
public String toString() {
return "ClassName=" + className + ";methodName=" + methodName;
}
}
SocketServer:
package com.hou.server;
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;
import com.hou.service.impl.HelloServiceImpl;
import com.hou.util.Call;
public class SimplServer {
/**
* 存放远程对象的缓存
*/
private Map remoteObjecs = new HashMap();
/**
* 存放远程对象到缓存中
*
* @param className
* @param remoteObject
*/
public void register(String className, Object remoteObject) {
remoteObjecs.put(className, remoteObject);
}
public void service() throws Exception {
ServerSocket server = new ServerSocket(8000);
System.out.println("服务器启动...");
while (true) {
Socket socket = server.accept();
InputStream inputStream = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(inputStream);
OutputStream out = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
Call call = (Call) ois.readObject();
System.out.println("读取Obj=" + call);
call = this.invoke(call);
oos.writeObject(call);
ois.close();
oos.close();
socket.close();
}
}
/**
* Invoke execute method
*
* @param call
* @return
*/
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[] paramType = call.getParamType();
// 得到方法
Method method = classType.getMethod(methodName, paramType);
// 从缓存中取出相关的远程对象
// Object obj = remoteObjecs.get(className);
Object obj = classType.newInstance();
if (obj == null) {
throw new Exception(className + "远程对象不存在");
} else {
// 调用远程对象的方法
result = method.invoke(obj, params);
}
} catch (Exception e) {
System.out.println("出现错误了..." + e);
}
call.setResult(result);
return call;
}
public static void main(String args[]) throws Exception {
SimplServer server = new SimplServer();
server.register("com.hou.service.impl.HelloServiceImpl",
new HelloServiceImpl());
server.service();
}
}
SocketClient:
package com.hou.client;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
* 客户端
*/
import com.hou.util.Call;
public class SimpleClient {
public void invoke() throws Exception {
Socket socket = new Socket("localhost", 8000);
OutputStream out = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
InputStream in = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(in);
Call call = new Call("com.hou.service.impl.HelloServiceImpl", "echo",
new Class[] { String.class }, new Object[] { "猴哥" });
// 向服务器发送对象包
oos.writeObject(call);
// 接受服务器返回的对象包
call = (Call) ois.readObject();
System.out.println("返回结果:" + call.getResult());
ois.close();
oos.close();
socket.close();
}
public static void main(String args[]) throws Exception {
new SimpleClient().invoke();
}
}
Invoke Class:
public class HelloServiceImpl implements HelloService {
public String echo(String msg) {
return "Echo:"+msg;
}
public Date getTime() {
return new Date();
}
}
以上是利用java反射机制实现远程调用,RMI远程调用就是采用该机制。如果利用NIO远程调用时间效率更高。