java

位置:IT落伍者 >> java >> 浏览文章

Spring2.5整合RMI技术


发布日期:2018年11月23日
 
Spring2.5整合RMI技术

Java的RMI技术使用起来比较麻烦有两点服务发布和调用服务

通过Spring的RMI支持可以非常容易的暴露任何的服务

下面是之前一篇《Java RMI之HelloWorld篇》文章的基础上加入了Spring的框架来实现的例子

环境jdk

springframeworkSEC

所用的第三方包优先从Spring的lib包中获取以获取最佳的兼容性

所依赖的jar包

服务端实现

第一服务接口和以前不一样了不用实现远程接口了

package lavasoftsturmi;

/**

* 定义一个远程接口

*

* @author leizhimin ::

*/

public interface HelloService {

/**

* 简单的返回Hello World!字样

*

* @return 返回Hello World!字样

*/

public String helloWorld();

/**

* 一个简单的业务方法根据传入的人名返回相应的问候语

*

* @param someBodyName 人名

* @return 返回相应的问候语

*/

public String sayHelloToSomeBody(String someBodyName);

}

服务实现类

package lavasoftsturmi;

/**

* 远程的接口的实现

*

* @author leizhimin ::

*/

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= encoding=UTF?>

<!DOCTYPE beans PUBLIC //SPRING//DTD BEAN//EN

beansdtd>

<beans>

<bean id=helloService class=lavasoftsturmiHelloServiceImpl/>

<bean id=serviceExporter class=orgspringframeworkremotingrmiRmiServiceExporter>

<property name=service ref=helloService/>

<! 定义服务名 >

<property name=serviceName value=hello/>

<property name=serviceInterface value=lavasoftsturmiHelloService/>

<property name=registryPort value=/>

</bean>

</beans>

服务端测试

package lavasoftsturmi;

import orgntextApplicationContext;

import orgntextsupportClassPathXmlApplicationContext;

/**

* 通过Spring发布RMI服务

*

* @author leizhimin ::

*/

public class HelloHost {

public static void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext(/applicationContext_rmi_serverxml);

Systemoutprintln(RMI服务伴随Spring的启动而启动了);

}

}

启动后如图所示

客户端调用测试

客户端调用有两种方式一种是使用Spring一种不使用这里仅介绍使用Spring的情况

在Spring中配置客户端要调用服务

<?xml version= encoding=UTF?>

<beans

xmlns=

xmlns:xsi=instance

xsi:schemaLocation= beansxsd>

<bean id=helloService class=orgspringframeworkremotingrmiRmiProxyFactoryBean>

<property name=serviceUrl value=rmi://:/hello/>

<property name=serviceInterface value=lavasoftsturmiHelloService/>

</bean>

<bean id=helloServiceClient class=lavasoftsturmiHelloClient>

<property name=helloService ref=helloService/>

</bean>

</beans>

客户端测试代码

package lavasoftsturmi;

import orgntextApplicationContext;

import orgntextsupportClassPathXmlApplicationContext;

import javarmiRemoteException;

/**

* 通过Spring来调用RMI服务

*

* @author leizhimin ::

*/

public class HelloClient {

private HelloService helloService;

public static void main(String[] args) throws RemoteException {

ApplicationContext ctx = new ClassPathXmlApplicationContext(/applicationContext_rmi_clientxml);

HelloService hs = (HelloService) ctxgetBean(helloService);

Systemoutprintln(hshelloWorld());

Systemoutprintln(hssayHelloToSomeBody(Lavasoft));

}

public void setHelloService(HelloService helloService) {

thishelloService = helloService;

}

}

运行结果

               

上一篇:Eclipse经典开发教程插件安装

下一篇:学习比较Struts2和Struts1:Struts2完胜