Spring--RMI

使用Spring對RMI的支持,可以非常容易地構建你的分佈式應用。在服務端,可以通過Spring的org.springframework.remoting.rmi.RmiServiceExporter可以暴露你的服務;在客戶端,通過org.springframework.remoting.rmi.RmiProxyFactoryBean可以使用服務端暴露的服務,非常方便。這種C/S模型的訪問方式,可以屏蔽掉RMI本身的複雜性,如服務端Skeleton和客戶端Stub等的處理細節,這些對於服務開發和服務使用的人員來說,都是透明的,無需過度關注,而集中精力開發你的商業邏輯。
下面通過一個例子,說明如何通過Spring集成RMI。
服務端發佈服務
我們定義了服務接口,服務端實現該服務接口來完成其複雜的邏輯,客戶端可以通過該接口調用服務端暴露的服務,如下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi;

public interface AccountService {
int queryBalance(String mobileNo);
String shoopingPayment(String mobileNo, byte protocol);
}

服務實現,示例如下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi;

import org.apache.log4j.Logger;

public class MobileAccountServiceImpl implements AccountService {

private static final Logger LOG = Logger.getLogger(MobileAccountServiceImpl.class);
public int queryBalance(String mobileNo) {
if (mobileNo != null)
return 100;
return 0;
}

public String shoopingPayment(String mobileNo, byte protocol) {
StringBuffer sb = new StringBuffer().append("Your mobile number is /"").append(
mobileNo).append("/", protocol type is /"").append(protocol)
.append("/".");
LOG.info("Message is: " + sb.toString());
return sb.toString();
}
}

服務端發佈服務,供客戶端進行(遠程方法)調用,Spring配置server.xml如下所示:
[xhtml] view plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="MobileAccountService" />
<property name="service" ref="accountService" />
<property name="serviceInterface"
value="org.shirdrn.spring.remote.rmi.AccountService" />
<property name="registryPort" value="8080" />
<property name="servicePort" value="8088" />
</bean>

<bean id="accountService" class="org.shirdrn.spring.remote.rmi.MobileAccountServiceImpl" />

</beans>

上面配置,指定了暴露的服務的名稱,通過serviceName屬性注入到RmiServiceExporter中,服務名稱爲MobileAccountService,客戶端通過該服務名稱就能夠進行調用。
下面啓動服務端,發佈服務,如下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RmiServer {

public static void main(String[] args) throws InterruptedException {
new ClassPathXmlApplicationContext("org/shirdrn/spring/remote/rmi/server.xml");

Object lock = new Object();
synchronized (lock) {
lock.wait();
}
}
}

客戶端調用服務
客戶端配置client.xml如下所示:
[xhtml] view plaincopy
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="mobileAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://192.168.1.103:8080/MobileAccountService" />
<property name="serviceInterface"
value="org.shirdrn.spring.remote.rmi.AccountService" />
</bean>

</beans>

配置中,將一個serviceUrl和serviceInterface注入給RmiProxyFactoryBean,即可進行遠程方法調用。調用示例如下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RmiClient {

private static final Logger LOG = Logger.getLogger(RmiClient.class);

public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"org/shirdrn/spring/remote/rmi/client.xml");
AccountService accountService = (AccountService) ctx
.getBean("mobileAccountService");
String result = accountService.shoopingPayment("13800138000", (byte) 5);
LOG.info(result);
}

}

可見,實現遠程訪問變得非常容易。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章