Spring RMI应用_转载
[仅作收藏]
?
RMI是从JDK 1.1开始就出现的API功能,它让客户端在使用远程对象所提供的服务时,就如何使用本地对象一样,然而RMI在使用时必须一连串繁复的手续,像是服务介 面在定义时必须继承java.rmi.Remote接口、服务Server在实作时必须继承java.rmi.UnicastRemoteObject类 别、必须使用rmic产生stub与skeleton等等。
?
透过org.springframework.remoting.rmi.RmiServiceExporter,Spring 简化了这些手续,来实际看看例子,了解Spring在RMI上的使用与简化,首先定义服务对象的接口:
?
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" name="code"><?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" name="code">... ApplicationContext context = new FileSystemXmlApplicationContext("rmi-client.xml"); ISomeService service = (ISomeService) context.getBean("someServiceProxy"); String result = service.doSomeService("Some request"); System.out.println(result);...?