首页 > 代码库 > Spring整合Rmi总结
Spring整合Rmi总结
在Spring整合Rmi中:
服务端使用了org.springframework.remoting.rmi.RmiServiceExporter
RmiServiceExporter把任何Spring管理的Bean输出成一个RMI服务。通过把Bean包装在一个适配器类中工作。适配器类被绑定到RMI注册表中,并且将请求代理给服务类。
客户端使用了org.springframework.remoting.rmi.RmiProxyFactoryBean
客户端的核心是RmiProxyFactoryBean,包含serviceURL属性和serviceInterface属性。
通过JRMP访问服务。JRMP JRMP:java remote method protocol,Java特有的,基于流的协议。
下面给出简单例子
服务端程序:
新建接口:
[java] view plaincopy
- <span style="font-size:18px;color:#3333ff;">/**
- * IRmiServer.java
- * 版权所有(C) 2012
- * 创建:cuiran 2012-08-08 11:12:40
- */
- package com.cayden.rmi;
- /**
- * TODO
- * @author cuiran
- * @version TODO
- */
- public interface IRmiServer {
- public boolean test();
- }
- </span>
再实现该接口的方法:
[java] view plaincopy
- <span style="font-size:18px;color:#3333ff;">/**
- * RmiServerImpl.java
- * 版权所有(C) 2012
- * 创建:cuiran 2012-08-08 11:13:24
- */
- package com.cayden.rmi.impl;
- import com.cayden.rmi.IRmiServer;
- /**
- * TODO
- * @author cuiran
- * @version TODO
- */
- public class RmiServerImpl implements IRmiServer {
- /* (non-Javadoc)
- * @see com.cayden.rmi.IRmiServer#test()
- */
- @Override
- public boolean test() {
- System.out.println("调用了我--服务端 O(∩_∩)O哈!");
- return true;
- }
- }
- </span>
在src下新建applicationContext.xml 配置文件
[html] view plaincopy
- <span style="font-size:18px;color:#3333ff;"><?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.5//EN" "file:/usr/local/tomcat_report/lib/spring-beans-2.0.dtd">
- <beans>
- <!-- rmi -->
- <bean id="rmiService" class="com.cayden.rmi.impl.RmiServerImpl">
- </bean>
- <bean id="remoteRmiService" class="org.springframework.remoting.rmi.RmiServiceExporter">
- <property name="serviceName">
- <value>remoteService</value>
- </property>
- <property name="service" ref="rmiService" />
- <property name="serviceInterface">
- <value>com.cayden.rmi.IRmiServer</value>
- </property>
- <property name="registryPort">
- <value>9400</value>
- </property>
- <property name="servicePort">
- <value>9401</value>
- </property>
- </bean>
- </beans></span>
启动服务端的类【MainServer.java】
[java] view plaincopy
- <span style="font-size:18px;color:#3333ff;">package com.cayden.rmi;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * MainServer.java
- * 版权所有(C) 2012
- * 创建:cuiran 2012-08-08 11:44:07
- */
- /**
- * TODO
- * @author cuiran
- * @version TODO
- */
- public class MainServer {
- /**
- * TODO
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- System.out.println("rmi服务端启动");
- ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
- System.out.println("rmi服务端启动完成。。。");
- }
- }
- </span>
客户端代码:
在客户端使用服务端的接口文件:
[java] view plaincopy
- <span style="font-size:18px;color:#3333ff;">/**
- * IRmiServer.java
- * 版权所有(C) 2012
- * 创建:cuiran 2012-08-08 11:12:40
- */
- package com.cayden.rmi;
- /**
- * TODO
- * @author cuiran
- * @version TODO
- */
- public interface IRmiServer {
- public boolean test();
- }
- </span>
然后在src下新建【applicationContext.xml】
[html] view plaincopy
- <span style="font-size:18px;color:#3333ff;"><?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.5//EN" "file:/usr/local/tomcat_report/lib/spring-beans-2.0.dtd">
- <beans>
- <!-- rmi远程调用 -->
- <bean id="testRmiService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
- <property name="serviceUrl">
- <value>rmi://127.0.0.1:9400/remoteService</value>
- </property>
- <property name="serviceInterface">
- <value>com.cayden.rmi.IRmiServer</value>
- </property>
- </bean>
- </beans></span>
最新新建客户端的测试类【TestRmi.java】
[java] view plaincopy
- <span style="font-size:18px;color:#3333ff;">/**
- * TestRmi.java
- * 版权所有(C) 2012
- * 创建:cuiran 2012-08-08 11:38:06
- */
- package com.cayden.rmi.client;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.cayden.rmi.IRmiServer;
- /**
- * TODO
- * @author cuiran
- * @version TODO
- */
- public class TestRmi {
- public static void main(String[] arg) {
- System.out.println("rmi客户端开始调用");
- ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
- IRmiServer rmi=(IRmiServer)ctx.getBean("testRmiService");
- rmi.test();
- System.out.println("rmi客户端调用结束");
- }
- }
- </span>
最后控制台输出
服务端:
[java] view plaincopy
- <span style="font-size:18px;color:#3333ff;">rmi服务端启动
- log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
- log4j:WARN Please initialize the log4j system properly.
- rmi服务端启动完成。。。
- 调用了我--服务端 O(∩_∩)O哈!
- </span>
客户端:
[java] view plaincopy
- <span style="font-size:18px;color:#3333ff;">rmi客户端开始调用
- 2012-8-8 11:46:51 org.springframework.context.support.AbstractApplicationContext prepareRefresh
- 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c29ab2: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1c29ab2]; startup date [Wed Aug 08 11:46:51 CST 2012]; root of context hierarchy
- 2012-8-8 11:46:51 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
- 信息: Loading XML bean definitions from class path resource [applicationContext.xml]
- 2012-8-8 11:46:51 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
- 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1c29ab2]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1e0bc08
- 2012-8-8 11:46:51 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
- 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1e0bc08: defining beans [testRmiService]; root of factory hierarchy
- rmi客户端调用结束
- </span>
Spring整合Rmi总结
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。