`
XinTeng2012
  • 浏览: 94469 次
社区版块
存档分类
最新评论

spring的rmi整合以及客户端和服务端搭建2.0

 
阅读更多

……继续上一篇的内容

原理搞清楚了那就准备写服务端和客户端吧!

首先把服务端搭建起来吧,由于项目是基于spring的rmi实现。因此服务端需要spring相关配置:

引入spring依赖的相关jar包:


然后开始代码编写,由于rmi需要接口来传递对象,需要先建立接口:

以下内容借鉴转载于:这位大牛的(由于我的工程涉及公司信息,就贴出别的大牛的吧,逻辑一样)

publicinterfaceHelloService {

/**
* 简单的返回“Hello World!"字样
*
* @return 返回“Hello World!"字样
*/

publicString helloWorld();

/**
* 一个简单的业务方法,根据传入的人名返回相应的问候语
*
* @param someBodyName 人名
* @return 返回相应的问候语
*/

publicString sayHelloToSomeBody(String someBodyName);
}

publicclassHelloServiceImplimplementsHelloService {
publicHelloServiceImpl() {
}

/**
* 简单的返回“Hello World!"字样
*
* @return 返回“Hello World!"字样
*/

publicString helloWorld() {
return"Hello World!";
}

/**
* 一个简单的业务方法,根据传入的人名返回相应的问候语
*
* @param someBodyName 人名
* @return 返回相应的问候语
*/

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

Spring配置rmi服务
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<beanid="helloService"class="lavasoft.sturmi.HelloServiceImpl"/>

<beanid="serviceExporter"class="org.springframework.remoting.rmi.RmiServiceExporter">
<propertyname="service"ref="helloService"/>
<!--定义服务名-->
<propertyname="serviceName"value="hello"/>
<propertyname="serviceInterface"value="lavasoft.sturmi.HelloService"/>
<propertyname="registryPort"value="8088"/>
</bean>
</beans>
服务端测试:

/**
* 通过Spring发布RMI服务
*
* @author leizhimin 2009-8-17 14:22:06
*/

publicclassHelloHost {
publicstaticvoidmain(String[] args) {
ApplicationContext ctx =newClassPathXmlApplicationContext("/applicationContext_rmi_server.xml");
System.out.println("RMI服务伴随Spring的启动而启动了.....");
}
}

客户端调用:

在Spring中配置客户端要调用服务:
<?xmlversion="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">
<beanid="helloService"class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<propertyname="serviceUrl"value="rmi://192.168.14.117:8088/hello"/>
<propertyname="serviceInterface"value="lavasoft.sturmi.HelloService"/>
</bean>

<beanid="helloServiceClient"class="lavasoft.sturmi.HelloClient">
<propertyname="helloService"ref="helloService"/>
</bean>
</beans>
客户端测试代码:

publicclassHelloClient {
privateHelloService helloService;

publicstaticvoidmain(String[] args)throwsRemoteException {
ApplicationContext ctx =newClassPathXmlApplicationContext("/applicationContext_rmi_client.xml");
HelloService hs = (HelloService) ctx.getBean("helloService");
System.out.println(hs.helloWorld());
System.out.println(hs.sayHelloToSomeBody("Lavasoft"));
}

publicvoidsetHelloService(HelloService helloService) {
this.helloService = helloService;
}
}

就这样spring的rmi调用整合就完成了!祝你好运!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics