在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特有的,基于流的协议。
下面给出简单例子
服务端程序:
新建接口:
-
1 /** 2 * IRmiServer.java 3 * 版权所有(C) 2012 4 * 创建:cuiran 2012-08-08 11:12:40 5 */ 6 package com.cayden.rmi; 7 8 /** 9 * TODO 10 * @author cuiran 11 * @version TODO 12 */ 13 public interface IRmiServer { 14 15 public boolean test(); 16 17 } 18
-
1 /** 2 * RmiServerImpl.java 3 * 版权所有(C) 2012 4 * 创建:cuiran 2012-08-08 11:13:24 5 */ 6 package com.cayden.rmi.impl; 7 8 import com.cayden.rmi.IRmiServer; 9 10 /** 11 * TODO 12 * @author cuiran 13 * @version TODO 14 */ 15 public class RmiServerImpl implements IRmiServer { 16 17 /* (non-Javadoc) 18 * @see com.cayden.rmi.IRmiServer#test() 19 */ 20 @Override 21 public boolean test() { 22 23 System.out.println("调用了我--服务端 O(∩_∩)O哈!"); 24 25 return true; 26 } 27 28 } 29
在src下新建applicationContext.xml 配置文件
-
1 2 3
4 5 6 7 8 910 2411 13remoteService 1214 15 17com.cayden.rmi.IRmiServer 1618 209400 1921 239401 22
-
1 package com.cayden.rmi; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 /** 6 * MainServer.java 7 * 版权所有(C) 2012 8 * 创建:cuiran 2012-08-08 11:44:07 9 */ 10 11 /** 12 * TODO 13 * @author cuiran 14 * @version TODO 15 */ 16 public class MainServer { 17 18 /** 19 * TODO 20 * @param args 21 */ 22 public static void main(String[] args) { 23 // TODO Auto-generated method stub 24 System.out.println("rmi服务端启动"); 25 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 26 27 System.out.println("rmi服务端启动完成。。。"); 28 } 29 30 } 31
客户端代码:
在客户端使用服务端的接口文件:
-
1 /** 2 * IRmiServer.java 3 * 版权所有(C) 2012 4 * 创建:cuiran 2012-08-08 11:12:40 5 */ 6 package com.cayden.rmi; 7 8 /** 9 * TODO 10 * @author cuiran 11 * @version TODO 12 */ 13 public interface IRmiServer { 14 15 public boolean test(); 16 17 } 18
然后在src下新建【applicationContext.xml】
-
1 2 3
4 5 6 137 9rmi://127.0.0.1:9400/remoteService 810 12com.cayden.rmi.IRmiServer 11
最新新建客户端的测试类【TestRmi.java】
-
1 /** 2 * TestRmi.java 3 * 版权所有(C) 2012 4 * 创建:cuiran 2012-08-08 11:38:06 5 */ 6 package com.cayden.rmi.client; 7 8 import org.springframework.context.ApplicationContext; 9 import org.springframework.context.support.ClassPathXmlApplicationContext; 10 11 import com.cayden.rmi.IRmiServer; 12 13 /** 14 * TODO 15 * @author cuiran 16 * @version TODO 17 */ 18 public class TestRmi { 19 public static void main(String[] arg) { 20 System.out.println("rmi客户端开始调用"); 21 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 22 IRmiServer rmi=(IRmiServer)ctx.getBean("testRmiService"); 23 rmi.test(); 24 System.out.println("rmi客户端调用结束"); 25 } 26 27 } 28
最后控制台输出
服务端:
-
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哈!
客户端:
-
-
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客户端调用结束
-