林信良(良葛格)的专栏

http://caterpillar.onlyfun.net/

原创 Spring RMI 支持收藏

新一篇: 扎根、搜寻、过滤、深读 | 旧一篇: 不要只读一本书

RMI是从JDK 1.1开始就出现的API功能,它让客户端在使用远程对象所提供的服务时,就如何使用本地对象一样,然而RMI在使用时必须一连串繁复的手续,像是服务介 面在定义时必须继承java.rmi.Remote接口、服务Server在实作时必须继承java.rmi.UnicastRemoteObject 别、必须使用rmic产生stubskeleton等等。

透过org.springframework.remoting.rmi.RmiServiceExporterSpring 简化了这些手续,来实际看看例子,了解SpringRMI上的使用与简化,首先定义服务对象的接口:

  •  ISomeService.java
package onlyfun.caterpillar;

public interface ISomeService {
    public String doSomeService(String some);
    public void doOtherService(int other);
}

可以看到服务对象的接口不用继承java.rmi.Remote界面,而在实作时也不用继承java.rmi.UnicastRemoteObject,例如:

  • SomeServiceImpl.java
package onlyfun.caterpillar;

public class SomeServiceImpl implements ISomeService {
    public String doSomeService(String some) {
        return some + " is processed";
    }
   
    public void doOtherService(int other) {
        // bla.. bla
    }
}

接下来在伺服端,您只要在Bean定义档中定义,让Spring管理、生成Bean,即可注册、启动RMI服务,例如:
  • rmi-server.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
  "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <bean id="someService" class="onlyfun.caterpillar.SomeServiceImpl"/>

    <bean id="serviceExporter"
class="org.springframework.remoting.rmi.RmiServiceExporter">
        <property name="service">
            <ref bean="someService"/>
        </property>
        <property name="serviceName">
            <value>SomeService</value>
        </property>
        <property name="serviceInterface">
            <value>onlyfun.caterpillar.ISomeService</value>
        </property>       
    </bean>
   
</beans>

很简单,只要告诉org.springframework.remoting.rmi.RmiServiceExporter服务对象、名称与要代理的接口,之后Spring读取完定义文件并生成Bean实例后,RMI服务就会启动。

接下来看看客户端要如何实作,只要透过org.springframework.remoting.rmi.RmiProxyFactoryBean,并告知服务的URL、代理的接口即可,就好像在使用本地端管理的服务一样,例如Bean定义档可以如下撰写:

  • rmi-client.xml
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="someServiceProxy"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl">
<value>rmi://localhost/SomeService</value>
</property>
<property name="serviceInterface">
<value>onlyfun.caterpillar.ISomeService</value>
</property>
</bean>
</beans>

以下是个简单的客户端呼叫远程服务的例子:

....
        ApplicationContext context =
                new FileSystemXmlApplicationContext("rmi-client.xml");

        ISomeService service = (ISomeService) context.getBean("someServiceProxy");

        String result = service.doSomeService("Some request");
        System.out.println(result);
....











发表于 @ 2006年06月10日 12:04:00|评论(loading...)|编辑

新一篇: 扎根、搜寻、过滤、深读 | 旧一篇: 不要只读一本书

评论

#javadao 发表于2006-06-10 14:44:00  IP: 59.81.135.*
实作?
您是台湾哪里人啊……,还是觉得这么说话很洋气?


#林信良 发表于2006-06-10 20:09:00  IP: 220.248.60.*
简繁用语本就不同,与洋气与否无关,我仅是使用Word的简繁互换功能转为简体而已...

顺便附带说明,Spring技术手册是由博文专门负责转译,应无用语上的困扰...:)
#henryli 发表于2006-06-12 08:57:00  IP: 10.16.26.*
javadao你肯定没有上过http://www.javaworld.com.tw更没有上过http://caterpillar.onlyfun.net,当然也没有看过他著的书了。不然你不会说这种话的!!这样不好!把时间多用在技术上!!
#笨笨狗 发表于2006-06-15 21:44:00  IP: 218.84.11.*
呵呵,他肯定不知道啦,莫怪:)很期待作者的书,等了好久都不见上市,可否催促一下出版社?呵呵
#林信良 发表于2006-06-16 09:03:00  IP: 220.248.62.*
应该是已经从印刷厂出来了,估计再几天可以在各大书局上架。。。:)
#JaneCrazy 发表于2006-07-04 16:10:00  IP: 219.136.51.*
rmi-server.xml 必须在web.xml中注册
界面-接口
实作-实现
伺服端-服务器端
定义档-配置文件
#fanxianshi 发表于2007-11-16 09:53:12  IP: 59.41.181.*
谢谢你的帮助!
#apple0668 发表于2008-01-06 17:30:18  IP: 218.15.22.*
林老师,我按照你上面去做啦,不过有异常发生:
异常如下:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someServiceProxy' defined in class path resource [rmi-client.xml]: Invocation of init method failed; nested exception is org.springframework.remoting.RemoteLookupFailureException: Lookup of RMI stub failed; nested exception is java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused: connect
Caused by: org.springframework.remoting.RemoteLookupFailureException: Lookup of RMI stub failed; nested exception is java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused: connect
发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © 良葛格