歡迎您光臨本站 註冊首頁

Spring2.5整合RMI技術

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

Java的RMI技術使用起來比較麻煩,有兩點:服務發布和調用服務.

通過Spring的RMI支持,可以非常容易的暴露任何的服務.

下面是之前一篇《Java RMI之HelloWorld篇》文章的基礎上,加入了Spring的框架來實現的例子.

環境:jdk1.5

spring-framework-2.5.6.SEC01

所用的第三方包優先從Spring的lib包中獲取,以獲取最佳的兼容性.

所依賴的jar包:

一、服務端實現

第一服務介面,和以前不一樣了,不用實現遠程介面了.

package lavasoft.sturmi;

/**
* 定義一個遠程介面
*
* @author leizhimin 2009-8-17 13:53:38
*/


public interface HelloService {

/**
* 簡單的返回「Hello World!"字樣
*
* @return 返回「Hello World!"字樣
*/

public String helloWorld();

/**
* 一個簡單的業務方法,根據傳入的人名返回相應的問候語
*
* @param someBodyName 人名
* @return 返回相應的問候語
*/

public String sayHelloToSomeBody(String someBodyName);
}

服務實現類

package lavasoft.sturmi;

/**
* 遠程的介面的實現
*
* @author leizhimin 2009-8-17 13:54:38
*/

public class HelloServiceImpl implements HelloService {
public HelloServiceImpl() {
}

/**
* 簡單的返回「Hello World!"字樣
*
* @return 返回「Hello World!"字樣
*/

public String helloWorld() {
return "Hello World!";
}


/**
* 一個簡單的業務方法,根據傳入的人名返回相應的問候語
*
* @param someBodyName 人名
* @return 返回相應的問候語
*/

public String sayHelloToSomeBody(String someBodyName) {
return "你好," someBodyName "!";
}
}

Spring配置rmi服務

<?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="helloService" class="lavasoft.sturmi.HelloServiceImpl"/>

<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="helloService"/>
<!-- 定義服務名 -->
<property name="serviceName" value="hello"/>
<property name="serviceInterface" value="lavasoft.sturmi.HelloService"/>
<property name="registryPort" value="8088"/>
</bean>

</beans>

服務端測試:

package lavasoft.sturmi;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* 通過Spring發布RMI服務
*
* @author leizhimin 2009-8-17 14:22:06
*/

public class HelloHost {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext_rmi_server.xml");
System.out.println("RMI服務伴隨Spring的啟動而啟動了.....");
}
}

啟動后如圖所示:

二、客戶端調用測試

客戶端調用有兩種方式,一種是使用Spring,一種不使用,這裡僅介紹使用Spring的情況.

在Spring中配置客戶端要調用服務:

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="helloService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://192.168.14.117:8088/hello"/>
<property name="serviceInterface" value="lavasoft.sturmi.HelloService"/>
</bean>


<bean id="helloServiceClient" class="lavasoft.sturmi.HelloClient">
<property name="helloService" ref="helloService"/>
</bean>
</beans>

客戶端測試代碼:

package lavasoft.sturmi;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.rmi.RemoteException;

/**
* 通過Spring來調用RMI服務
*
* @author leizhimin 2009-8-17 14:12:46
*/

public class HelloClient {
private HelloService helloService;

public static void main(String[] args) throws RemoteException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext_rmi_client.xml");
HelloService hs = (HelloService) ctx.getBean("helloService");
System.out.println(hs.helloWorld());
System.out.println(hs.sayHelloToSomeBody("Lavasoft"));
}

public void setHelloService(HelloService helloService) {
this.helloService = helloService;
}
}

運行結果:


[火星人 ] Spring2.5整合RMI技術已經有480次圍觀

http://coctec.com/docs/java/show-post-61106.html