Spring2.5整合RMI技术
来源:互联网 发布:c语言结束符 编辑:程序博客网 时间:2024/06/05 20:44
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);
}
/**
* 定义一个远程接口
*
* @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 + "!";
}
}
/**
* 远程的接口的实现
*
* @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>
<!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的启动而启动了.....");
}
}
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>
<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;
}
}
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;
}
}
运行结果:
0 0
- Spring2.5整合RMI技术
- Spring2.5整合JPA
- spring2.5整合ibatis
- 利用Spring2.0技术实现RMI的成功简单示例
- spring2.5同velocity整合
- spring2.5整合osworkflow2.8
- spring2.5整合struts1.2
- spring2.5整合struts2.1.8
- Spring2.5 + Struts1 的整合
- 整合struts2+hibernate3+spring2.5
- spring2.5整合osworkflow2.8 .
- jbpm4.4整合spring2.5
- spring2.5整合hibernate3.0
- spring2.5整合hibernate3.0整合Struts
- 《Spring2.0技术手册》读书笔记八-Spring与Hibernate整合
- spring2.5+struts2.18+hibernate3.5整合
- 整合Struts2+Spring2.5+Hibernate3.5
- Java : Hibernate3.2 与 Spring2.5整合
- 后缀数组(重复次数最多的最长重复子串)—— POJ 3693
- PHP JQUERY JSON 实例
- 表单校验组件ValidForm(2)
- web Deploy发布wcf
- POJ 3123 Ticket to Ride 状压dp+树形dp 斯坦纳树
- Spring2.5整合RMI技术
- Xcode6的路径变化
- LayoutParams的用法
- tez0.6.0 编译报错
- hihocoder 1015
- java中String的相等判断(==和equals())详解
- 软件生命周期流程
- 自动化功能测试-Athrun—IOS学习之旅文档
- 表单校验组件ValidForm(1)