Spring学习 RMI
来源:互联网 发布:雅马哈授权网络经销商 编辑:程序博客网 时间:2024/06/12 00:42
使用Spring对RMI的支持,可以非常容易地构建你的分布式应用。在服务端,可以通过Spring的org.springframework.remoting.rmi.RmiServiceExporter可以暴露你的服务;在客户端,通过org.springframework.remoting.rmi.RmiProxyFactoryBean可以使用服务端暴露的服务,非常方便。这种C/S模型的访问方式,可以屏蔽掉RMI本身的复杂性,如服务端Skeleton和客户端Stub等的处理细节,这些对于服务开发和服务使用的人员来说,都是透明的,无需过度关注,而集中精力开发你的商业逻辑。
下面通过一个例子,说明如何通过Spring集成RMI。
服务端发布服务
我们定义了服务接口,服务端实现该服务接口来完成其复杂的逻辑,客户端可以通过该接口调用服务端暴露的服务
package springapp.rmi.rmi;public interface IHelloWorld {public String helloWorld();public String sayHelloToSomeBody(String someBodyName);}
package springapp.rmi.rmi;public class HelloWorld implements IHelloWorld {@Overridepublic String helloWorld() {return "Hello World!";}@Overridepublic String sayHelloToSomeBody(String someBodyName) {return "Hello World!" + someBodyName;}}
<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean id="helloWorld" class="springapp.rmi.rmi.HelloWorld" /><bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"><property name="service" ref="helloWorld" /><!-- 定义服务名 --><property name="serviceName" value="hello" /><property name="serviceInterface" value="springapp.rmi.rmi.IHelloWorld" /><property name="registryPort" value="8088" /></bean></beans>
public class RmiServer {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:rmi_server_context.xml");System.out.println("RMI服务伴随Spring的启动而启动了....."); Object lock = new Object(); synchronized (lock) { try {lock.wait();} catch (InterruptedException e) {e.printStackTrace();} } }}
Client
<?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-3.0.xsd"> <bean id="helloWorld" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://192.168.1.34:8088/hello" /> <property name="serviceInterface" value="springapp.rmi.rmi.IHelloWorld" /> </bean> </beans>
package springapp.rmi.rmi;public interface IHelloWorld {public String helloWorld();public String sayHelloToSomeBody(String someBodyName);}
public class RMIClientTest {public static void main(String[] args){ApplicationContext ctx = new ClassPathXmlApplicationContext( "classpath:client.xml"); IHelloWorld hs = (IHelloWorld) ctx.getBean("helloWorld"); System.out.println(hs); System.out.println(hs.helloWorld()); System.out.println(hs.sayHelloToSomeBody("Lavasoft")); }}
0 0
- Spring RMI 学习
- spring rmi学习示例
- Spring学习 RMI
- Spring RMI学习
- Spring RMI使用学习
- RMI学习笔记(2)-Spring集成RMI
- spring rmi http invoker学习笔记
- spring RMI
- Spring rmi
- rmi spring
- Spring RMI
- spring rmi
- Spring RMI
- Spring - RMI
- spring rmi
- 《pro Spring》学习笔记之Spring集成RMI实例
- Spring框架学习【Spring RMI实现远程调用】
- RMI学习
- 【转发】关于Java性能的9个谬论
- VC++ data type: wchar_t v.s. unsigned short and compiler option /Zc:wchar_t
- 内存文件映射
- Git push 总结
- 区间第K大数
- Spring学习 RMI
- 【JSP开发】JSP常用标签介绍
- 图---邻接表(建立,深度遍历,广度遍历)
- R语言选择导入文本数据部分字段
- 把TextView中的文字添加阴影效果及Style的使用
- mysql 搜索当天数据
- html5 canvas 详细使用教程
- bool? 什么意思 bool? isTrue;表示 bool isTrue = null ; 定义并给变量赋值为null;
- SVN应用