Spring rmi的简单demo

本文展示了如何使用Java RMI(Remote Method Invocation)进行远程方法调用,包括定义接口、接口实现、配置RMI服务和客户端连接过程。
(1)定义接口:
Java代码 复制代码 收藏代码
  1. packagecom.logcd.spring.rmi;
  2. publicinterfaceHelloService{
  3. publicStringdoHello(Stringname);
  4. }
package com.logcd.spring.rmi;

public interface HelloService {
	public String doHello(String name);
}

(2)接口实现:
Java代码 复制代码 收藏代码
  1. packagecom.logcd.spring.rmi;
  2. publicclassHelloServiceImplimplementsHelloService{
  3. publicStringdoHello(Stringname){
  4. return"Hello,"+name;
  5. }
  6. }
package com.logcd.spring.rmi;

public  class HelloServiceImpl implements HelloService{

	public String doHello(String name) {
		return "Hello , " + name;
	}

}

(3)rmi-server.xml
Java代码 复制代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEbeansPUBLIC"-//SPRING/DTDBEAN/EN"
  3. "http://www.springframework.org/dtd/spring-beans.dtd">
  4. <beans>
  5. <beanid="helloService"class="com.logcd.spring.rmi.HelloServiceImpl"/>
  6. <!--RmiServiceExporter显示地支持使用RMI调用器暴露任何非RMI服务-->
  7. <beanid="serviceExporter"
  8. class="org.springframework.remoting.rmi.RmiServiceExporter">
  9. <propertyname="service"ref="helloService"/>
  10. <propertyname="serviceInterface"
  11. value="com.logcd.spring.rmi.HelloService"/>
  12. <!--定义要暴露的服务名可以与输出的bean不同名,客户端通过这个名字来调用服务-->
  13. <propertyname="serviceName"value="HelloService"/>
  14. <!--覆盖RMI注册端口号(1099),通常应用服务器也会维护RMI注册,最好不要冲突-->
  15. <propertyname="registryPort"value="1199"/>
  16. </bean>
  17. </beans>
<?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="com.logcd.spring.rmi.HelloServiceImpl"/>

	<!--RmiServiceExporter显示地支持使用RMI调用器暴露任何非RMI服务-->
    <bean id="serviceExporter" 
class="org.springframework.remoting.rmi.RmiServiceExporter">

        <property name="service" ref="helloService"/>
        <property name="serviceInterface"
           value="com.logcd.spring.rmi.HelloService"/>
        <!--定义要暴露的服务名可以与输出的bean不同名,客户端通过这个名字来调用服务-->
        <property name="serviceName" value ="HelloService"/>
        <!--覆盖RMI注册端口号(1099),通常应用服务器也会维护RMI注册,最好不要冲突-->
        <property name="registryPort" value="1199"/>
    </bean>
    
</beans>

(4)rmi-client.xml
Java代码 复制代码 收藏代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEbeansPUBLIC"-//SPRING/DTDBEAN/EN"
  3. "http://www.springframework.org/dtd/spring-beans.dtd">
  4. <beans>
  5. <!--使用RmiProxyFactoryBean连接服务端-->
  6. <beanid="serviceProxy"
  7. class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
  8. <propertyname="serviceUrl"
  9. value="rmi://localhost:1199/HelloService"/>
  10. <propertyname="serviceInterface"
  11. value="com.logcd.spring.rmi.HelloService"/>
  12. </bean>
  13. </beans>
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
   "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<!--使用RmiProxyFactoryBean连接服务端-->
     <bean id="serviceProxy" 
               class="org.springframework.remoting.rmi.RmiProxyFactoryBean">

        <property name="serviceUrl"
	                value="rmi://localhost:1199/HelloService"/> 
        <property name="serviceInterface"
                  value="com.logcd.spring.rmi.HelloService"/>
     </bean>
</beans>

(5)测试
Java代码 复制代码 收藏代码
  1. packagecom.logcd.spring.rmi;
  2. importorg.springframework.context.ApplicationContext;
  3. importorg.springframework.context.support.ClassPathXmlApplicationContext;
  4. publicclassTestSpringRMI{
  5. publicstaticvoidmain(Stringargs[]){
  6. ApplicationContextcontext=newClassPathXmlApplicationContext(
  7. newString[]{"rmi-server.xml","rmi-client.xml"});
  8. HelloServiceservice=(HelloService)context.getBean("serviceProxy");
  9. System.out.println(service.doHello("logcd"));
  10. }
  11. }
package com.logcd.spring.rmi;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpringRMI {
	
	public static void main(String args[]){
	   ApplicationContext context= new ClassPathXmlApplicationContext(
				new String[]{"rmi-server.xml","rmi-client.xml"}); 
	   
	   HelloService service = (HelloService)context.getBean("serviceProxy");
	   
	   System.out.println(service.doHello("logcd"));
	}
	
}
转自:http://log-cd.iteye.com/blog/213908
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值