首页 > 代码库 > RMI spring
RMI spring
1.业务接口类及其实现
/** * 定义一个远程接口 */ public interface HelloService { /** * 需要远程调用的方法 * @param msg * @return */ String sayHello(String msg); }
public class HelloServiceImpl implements HelloService { public String sayHello(String msg) { return "server received the msg : " + msg; } }
2.RmiServiceExporter(服务端)
<?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-2.5.xsd"> <!--RMI的服务实现类--> <bean id="helloService" class="com.rmi.spring.HelloServiceImpl" /> <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <!--配置RMI的服务实现类--> <property name="service" ref="helloService" /> <!--配置RMI的服务接口--> <property name="serviceInterface" value="http://www.mamicode.com/com.rmi.spring.HelloService"/> <!--暴露的对外服务名--> <property name="serviceName" value="http://www.mamicode.com/hello" /> <!--服务本地注册端口--> <property name="registryPort" value="http://www.mamicode.com/9123" /> <!--服务对外暴露端口--> <property name="servicePort" value="http://www.mamicode.com/9123" /> <!--注册服务--> <property name="alwaysCreateRegistry" value="http://www.mamicode.com/true" /> </bean> </beans>
3.RmiProxyFactoryBean(客户端)
<?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-2.5.xsd"> <bean id="helloService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://localhost:9123/hello" /> <property name="serviceInterface" value="http://www.mamicode.com/com.rmi.spring.HelloService"/> </bean> </beans>
4.测试
public class HelloServer { public static void main(String[] args) { new ClassPathXmlApplicationContext("spring-rmi-server.xml"); System.err.println("rmi 服务启动!"); } }
public class HelloClient { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-rmi-client.xml"); HelloService helloService = (HelloService)applicationContext.getBean("helloService"); System.err.println(helloService.sayHello("测试测试!")); } }
本文出自 “旅行者” 博客,请务必保留此出处http://881206524.blog.51cto.com/10315134/1922774
RMI spring
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。