Connector:
public class Connector {
/**
* 主机地址
*/
private String host;
/**
* 端口号
*/
private int port;
/**
* 套接字
*/
private Socket skt;
private InputStream in;
private ObjectInputStream ois;
private OutputStream out;
private ObjectOutputStream oos;
/**
* 有参构造方法
*
* @param host
* @param port
*/
public Connector(String host, int port) {
this.host = host;
this.port = port;
}
/**
* 发送对象
*
* @param obj
* @throws IOException
*/
public void send(Object obj) throws IOException {
this.connect();
oos.writeObject(obj);
}
/**
* 接受对象
*
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public Object receive() throws IOException, ClassNotFoundException {
return ois.readObject();
}
/**
* 连接远程服务器
*
* @throws UnknownHostException
* @throws IOException
*/
public void connect() throws UnknownHostException, IOException {
connect(host, port);
}
/**
* 建立于远程服务器的连接
*
* @param host
* @param port
* @throws UnknownHostException
* @throws IOException
*/
public void connect(String host, int port) throws UnknownHostException,
IOException {
skt = new Socket(host, port);
out = skt.getOutputStream();
oos = new ObjectOutputStream(out);
in = skt.getInputStream();
ois = new ObjectInputStream(in);
}
public void close() {
try {
System.out.println("关闭与远程的连接");
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
ois.close();
oos.close();
skt.close();
} catch (IOException e) {
System.out.println("关闭远程连接过程中出现错误了" + e);
e.printStackTrace();
}
}
}
}
静态代理类HelloServiceProxy:
/**
* 静态代理类
*
*/
public class HelloServiceProxy implements HelloService {
private String host;
private int port;
public HelloServiceProxy(String host, int port) {
this.host = host;
this.port = port;
}
/**
* 远程调用echo方法
*/
public String echo(String msg) {
Object result = null;
Connector conn = null;
try {
conn = new Connector(host, port);
Call call = new Call("com.hou.service.impl.HelloServiceImpl", "echo",
new Class[] { String.class }, new Object[] { msg });
// 发送数据
conn.send(call);
// 接受数据
call = (Call) conn.receive();
result = call.getResult();
if (result instanceof Throwable)
System.out.println("出现异常消息了" + result);
} catch (Exception e) {
System.out.println("出现异常消息了" + e);
} finally {
if (conn != null) {
conn.close();
}
}
return (String) result;
}
/**
* 远程调用getTime方法
*/
public Date getTime() {
Connector conn = null;
Object result = null;
try {
conn = new Connector(host, port);
Call call = new Call("com.hou.service.impl.HelloServiceImpl", "getTime",
new Class[] {}, new Object[] {});
conn.send(call);
call = (Call) conn.receive();
result = call.getResult();
if (result instanceof Throwable) {
System.out.println("出现异常消息了" + result);
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (conn != null) {
conn.close();
}
}
return (Date) result;
}
}
Call发转工具类:
/**
* 反转工具类
*
*
*/
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;
}
}
Dynamic Class:
/**
* 动态代理类
*
*/
public class ProxyFactory {
public static Object getProxy(final Class classType, final String host,
final int port) {
/**
* 匿名内部类
*/
InvocationHandler handler = new InvocationHandler() {
Object result = null;
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Connector conn = null;
try {
conn = new Connector(host, port);
Call call = new Call(classType.getName(), method.getName(),
method.getParameterTypes(), args);
conn.send(call);
call = (Call) conn.receive();
result = call.getResult();
if (result instanceof Throwable) {
System.out.println("出现异常了" + result);
}
} catch (Exception e) {
// TODO: handle exception
} finally {
if (conn != null) {
conn.close();
}
}
return result;
}
};
return Proxy.newProxyInstance(classType.getClassLoader(),
new Class[] { classType }, handler);
}
}
Client:
/**
* 客户端
*
*
*/
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
// 静态代理测试
/*HelloService helloService1 = new HelloServiceProxy("localhost", 8000);
System.out.println("测试静态代理--->" + helloService1.echo("猴哥"));
System.out.println("测试静态代理--->" + helloService1.getTime());*/
// 动态代理
HelloService helloService2 = (HelloService) ProxyFactory.getProxy(
HelloService.class, "localhost", 8000);
System.out.println("测试动态代理--->" + helloService2.echo("猴哥哥"));
System.out.println("测试动态代理--->" + helloService2.getTime());
}
}
RMI远程调用原理就是利用java soket+Proxy来实现远程调用的。
分享到:
相关推荐
在Java中,动态代理主要通过两个类来实现:`java.lang.reflect.Proxy` 和 `java.lang.reflect.InvocationHandler`。`Proxy` 类用于创建动态代理实例,而 `InvocationHandler` 接口则定义了当调用代理对象的方法时应...
2. 定义一个接口`HelloClientInterface`,它扩展`java.rmi.Remote`并声明`getName`方法。 3. 在客户端代码`TestHelloClient.java`中,使用`javax.xml.rpc.ServiceFactory`从WSDL URL创建服务代理,然后通过调用接口...
描述中提到,BunnyX Dynamic XML Proxy基于一种名为RMA(可能是指"Remote Method Access"或某种特定的算法)的机制,这一机制在某篇研究生论文中有详细论述。RMA可能是一种优化的数据访问和远程调用的方法,旨在提高...
JDK动态代理,全称为Java Dynamic Proxy,是Java标准库提供的一种强大且灵活的机制,允许我们在运行时创建代理类来实现指定的接口。这种机制主要用于实现AOP(面向切面编程)或为已有接口提供额外的功能,如日志、...
- 创建服务接口,并确保它继承自 `java.rmi.Remote`。 - 使用 Axis 提供的工具或API创建该接口的代理实例。 - 通过代理对象调用远程服务的方法。 在上述示例中,没有给出使用动态代理的完整代码,但基本流程是先...
- 考虑使用`UnicastRemoteObject`替代`DynamicProxy`,以提高性能。 通过这个"rmi-code"示例,你可以学习如何设置和运行RMI应用程序,理解服务器和客户端之间的通信机制。同时,也可以进一步探索RMI的高级特性,如...
System.out.println("Dynamic proxy: " + hello.sayHello("abc", "123")); } } ``` 在这个示例代码中,我们首先使用 `ServiceFactory` 创建了一个 `Service` 对象,然后使用该对象来访问 Net WebService 的 `...
Axis支持三种Web服务的部署和开发方式:Dynamic Invocation Interface (DII)、Stubs方式和Dynamic Proxy方式。下面以DII和Dynamic Proxy为例进行说明。 #### 1. DII (Dynamic Invocation Interface) **服务端**: ...
public interface HelloClientInterface extends java.rmi.Remote { public String getName(String name) throws java.rmi.RemoteException; } ``` - **创建客户端代码** 创建客户端代码以调用服务。 ```java ...
2. **生成代理接口**:定义一个接口`HelloClientInterface`,它继承自`java.rmi.Remote`,并包含与`HelloClient`类相同的`getName`方法。 3. **生成代理类**:使用Axis工具生成与WSDL文件匹配的Java客户端代码。这...
public interface HelloClientInterface extends java.rmi.Remote { public String getName(String name) throws java.rmi.RemoteException; } ``` 2. **创建客户端:** 创建 `TestHelloClient` 类,使用 `...
**定义代理接口**:编写一个名为`HelloClientInterface`的接口,该接口继承自`java.rmi.Remote`,并声明一个`getName`方法。 2. **创建代理对象**:在客户端程序`TestHelloClient`中,使用`javax.xml.rpc....
5. 动态代理(Dynamic Proxy):提供了一种创建动态实现指定接口的对象的机制,常用于AOP(面向切面编程)。 四、其他重要组件 1. Swing GUI:提供了丰富的组件和布局管理器,用于构建桌面应用程序。 2. Applet:...
- **动态代理(Dynamic Proxy)**:Java的`java.lang.reflect.Proxy`类提供了创建动态代理对象的能力,使得我们可以在运行时创建一个新的类,该类实现了指定接口。在RPC中,动态代理用于创建客户端的接口代理,调用...
本文将详细介绍如何使用Apache Axis创建Web Service,并将重点介绍三种不同的方法:动态调用接口(Dynamic Invocation Interface,DII)、Stubs方式和动态代理(Dynamic Proxy)方式。 #### 二、环境搭建与配置 在...
要新建 BlazeDS 工程,需要创建 Flex 工程,勾选 Use remote object access service。然后,需要配置 J2EE 服务器,使用 Tomcat 就可以了。设定上下文路径和 blazeds.war 的位置。 三、同域访问 访问 HTTP 服务...
public interface HelloClientInterface extends java.rmi.Remote { public String getName(String name) throws java.rmi.RemoteException; } ``` 2. **客户端代码**: ```java import javax.xml.rpc....
远程方法调用(Remote Method Invocation, RMI)是一种Java技术,允许一个Java虚拟机(JVM)上的对象调用另一个JVM中的对象的方法。 **工作流程:** 1. **参数序列化:** 调用方将参数“打包”后发送给远程JVM。 2. **...
proxy_set_header X-Real-IP $remote_addr; } } ``` #### 动态资源配置 ```nginx server { listen 80; server_name www.example.com; # 转发动态请求 location / { proxy_pass http://dynamic_server; ...
Spring AOP部分会介绍AOP的基本概念、Spring中的AOP实现以及与动态代理(Dynamic Proxy)和CGLib等技术的结合使用。 DAO(Data Access Object)支持是Spring提供的数据访问抽象,它封装了数据访问的细节,使得...