Java开发者可以利用的远程技术有以下几种:
1.远程方法调用(RMI)
2.Caucho的Hessian和Burlap
3.Spring自己的HTTP invoker
4.EJB
5.Web Services
一.RMI
它由5个包及3个应用工具组成
java.rmi 组织客户端的RMI类、接口和异常
java.rmi.server 组织服务器端的RMI类 接口和异常
java.rmi.registry 组织用于管理RMI命名服务的类
java.rmi.dgc 组织用于管理分布式垃圾收集的类
java.rmi.activation 组织用于实现按需激活的RMI服务的类
rmic 编译器,它产生RMI使用的存根和框架以完成分布式通信
rmiregistry 一个为RMI提供命名服务的服务器,这项服务把名字和对象关联在一起
rmid 一个支持RMI激活框架的服务器
如果具体还不知道什么是RMI,可以参考这篇非常不错的文章《 JavaRMI入门实战》
注意:
1)要把PerfectTime 和PerfectTimeI类中的
System.setSecurityManager(new RMISecurityManager()); 注释掉(因为这需要配安全策略,比较麻烦)
2)还有在JDK5下,用rmic -d命令的话只会输出一个文件,因为JDK5对RMI进行了优化
接下来看看用spring对《 JavaRMI入门实战》例子的简化
1.客户端的配置及代码
PerfectTimeI接口(不用继承java.rmi.Remote接口了,普通的POJO)
PerfectTime类(爽吧,简简单单的POJO)
TestServer.java
这就是spring的魅力啊
1.远程方法调用(RMI)
2.Caucho的Hessian和Burlap
3.Spring自己的HTTP invoker
4.EJB
5.Web Services
一.RMI
它由5个包及3个应用工具组成
java.rmi 组织客户端的RMI类、接口和异常
java.rmi.server 组织服务器端的RMI类 接口和异常
java.rmi.registry 组织用于管理RMI命名服务的类
java.rmi.dgc 组织用于管理分布式垃圾收集的类
java.rmi.activation 组织用于实现按需激活的RMI服务的类
rmic 编译器,它产生RMI使用的存根和框架以完成分布式通信
rmiregistry 一个为RMI提供命名服务的服务器,这项服务把名字和对象关联在一起
rmid 一个支持RMI激活框架的服务器
如果具体还不知道什么是RMI,可以参考这篇非常不错的文章《 JavaRMI入门实战》
注意:
1)要把PerfectTime 和PerfectTimeI类中的
System.setSecurityManager(new RMISecurityManager()); 注释掉(因为这需要配安全策略,比较麻烦)
2)还有在JDK5下,用rmic -d命令的话只会输出一个文件,因为JDK5对RMI进行了优化
接下来看看用spring对《 JavaRMI入门实战》例子的简化
1.客户端的配置及代码
PerfectTimeI接口(不用继承java.rmi.Remote接口了,普通的POJO)
package
com.open.rmi.ex2;
public interface PerfectTimeI {
long getPerfectTime();
}
DisplayPerfectTime类(也是一个普通的类)
public interface PerfectTimeI {
long getPerfectTime();
}
package
com.open.rmi.ex2;
public class DisplayPerfectTime {
private PerfectTimeI pf;
public void display() {
for (int i = 0; i < 10; i++) {
System.out.println("PerfectTime:" + pf.getPerfectTime());
}
}
//省略了pf的get,set方法
}
客户端配置bean_client.xml
public class DisplayPerfectTime {
private PerfectTimeI pf;
public void display() {
for (int i = 0; i < 10; i++) {
System.out.println("PerfectTime:" + pf.getPerfectTime());
}
}
//省略了pf的get,set方法
}
<
beans
>
<!-- 客户端 -->
< bean id ="pf" class ="org.springframework.remoting.rmi.RmiProxyFactoryBean" >
< property name ="serviceUrl" value ="rmi://192.168.1.8:1099/perfect_time" />
< property name ="serviceInterface" value ="com.open.rmi.ex2.PerfectTimeI" />
</ bean >
< bean id ="test" class ="com.open.rmi.ex2.DisplayPerfectTime" >
< property name ="pf" ref ="pf" />
</ bean >
</ beans >
2.服务端的配置及代码
<!-- 客户端 -->
< bean id ="pf" class ="org.springframework.remoting.rmi.RmiProxyFactoryBean" >
< property name ="serviceUrl" value ="rmi://192.168.1.8:1099/perfect_time" />
< property name ="serviceInterface" value ="com.open.rmi.ex2.PerfectTimeI" />
</ bean >
< bean id ="test" class ="com.open.rmi.ex2.DisplayPerfectTime" >
< property name ="pf" ref ="pf" />
</ bean >
</ beans >
PerfectTime类(爽吧,简简单单的POJO)
package
com.open.rmi.ex2;
public class PerfectTime implements PerfectTimeI {
public long getPerfectTime() {
return System.currentTimeMillis();
}
}
配置bean_server.xml
public class PerfectTime implements PerfectTimeI {
public long getPerfectTime() {
return System.currentTimeMillis();
}
}
<
beans
>
<!-- 服务端:Spring读取完定义文件,在生成serviceExporter实例后,RMI服务就会启动 -->
< bean id = " serviceExporter "
class = " org.springframework.remoting.rmi.RmiServiceExporter " >
< property name = " service " ref = " pt " />
< property name = " serviceName " value = " perfect_time " />
< property name = " serviceInterface " value = " com.open.rmi.ex2.PerfectTimeI " />
</ bean >
< bean id = " pt " class = " com.open.rmi.ex2.PerfectTime " />
</ beans >
3.测试代码
<!-- 服务端:Spring读取完定义文件,在生成serviceExporter实例后,RMI服务就会启动 -->
< bean id = " serviceExporter "
class = " org.springframework.remoting.rmi.RmiServiceExporter " >
< property name = " service " ref = " pt " />
< property name = " serviceName " value = " perfect_time " />
< property name = " serviceInterface " value = " com.open.rmi.ex2.PerfectTimeI " />
</ bean >
< bean id = " pt " class = " com.open.rmi.ex2.PerfectTime " />
</ beans >
TestServer.java
package
com.open.rmi.ex2;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestServer {
public static void main(String[] args) {
BeanFactory bf=new ClassPathXmlApplicationContext("bean_server.xml");
bf.getBean("serviceExporter");
}
}
TestClient.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestServer {
public static void main(String[] args) {
BeanFactory bf=new ClassPathXmlApplicationContext("bean_server.xml");
bf.getBean("serviceExporter");
}
}
package
com.open.rmi.ex2;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestClient {
public static void main(String[] args) {
BeanFactory bf = new ClassPathXmlApplicationContext("bean_client.xml");
DisplayPerfectTime test = (DisplayPerfectTime)bf.getBean("test");
test.display();
}
}
先运行TestServer,输出
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestClient {
public static void main(String[] args) {
BeanFactory bf = new ClassPathXmlApplicationContext("bean_client.xml");
DisplayPerfectTime test = (DisplayPerfectTime)bf.getBean("test");
test.display();
}
}
RmiServiceExporter
-
Could not detect RMI registry
-
creating
new
one
在运行TestClient就可以看到结果了
PerfectTime:
1150125107468
PerfectTime: 1150125107484
PerfectTime: 1150125107484
看到没,你根本不用运行rmic -d G:\RMI test.PerfectTime,start rmiregistry 。
PerfectTime: 1150125107484
PerfectTime: 1150125107484
这就是spring的魅力啊