java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。本站提供基于Java框架struts,spring,hibernate等的桌面应用、web交互及移动终端的开发技巧与资料
保持永久学习的心态,将成就一个优秀的你,来 继续搞起java知识。
转自:/content/6857296.html
Spring有多种调用远程的方式,今天学习了一下远程方法调用(RMI)。
RMI需要服务端和客户端
我们先从服务器开始
我的代码结构
</b> <br/>view plaincopyprint?<br/><img src="https://code.csdn.net/assets/CODE_ico.png" border="0" ><br/><br/><img src="https://code.csdn.net/assets/ico_fork.svg" border="0" ><br/><br/>package rmi; <br/>public interface ServerRmiI { <br/> public String sayHi(String name); <br/>} <br/>1package rmi;
public interface ServerRmiI {
public String sayHi(String name);
}
</b> <br/>view plaincopyprint?<br/><img src="https://code.csdn.net/assets/CODE_ico.png" border="0" ><br/><br/><img src="https://code.csdn.net/assets/ico_fork.svg" border="0" ><br/><br/>package rmi; <br/>public class ServerRmiImpl implements ServerRmiI{ <br/> public String sayHi(String name) { <br/> return "Hi,"+name; <br/> } <br/>} <br/>1package rmi;
public class ServerRmiImpl implements ServerRmiI{
public String sayHi(String name) {
return "Hi,"+name;
}
}
</b> <br/>view plaincopyprint?<br/><img src="https://code.csdn.net/assets/CODE_ico.png" border="0" ><br/><br/><img src="https://code.csdn.net/assets/ico_fork.svg" border="0" ><br/><br/>package rmi; <br/>import org.springframework.context.ApplicationContext; <br/>import org.springframework.context.support.ClassPathXmlApplicationContext; <br/>public class Testrun { <br/> /** <br/> * <p> <br/> * </p> <br/> * @author zhangjunshuai <br/> * @date 2014-5-28 下午5:06:06 <br/> * @param args <br/> */ <br/> public static void main(String[] args) { <br/> // TODO Auto-generated method stub <br/> ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-bean.xml"); <br/> context.getBean("serverTest"); <br/> } <br/>} <br/>1package rmi;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Testrun {
/**
* <p>
* </p>
* @author zhangjunshuai
* @date 2014-5-28 下午5:06:06
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-bean.xml");
context.getBean("serverTest");
}
}
配置:
view plaincopyprint?
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName" default-lazy-init="true">
<?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-2.5.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-autowire="byName" default-lazy-init="true">
<bean name="rmiserver" class="rmi.ServerRmiImpl"/>
<bean name="serverTest" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="rmiserver"/>
<property name="serviceName" value="serverRmiTest"/>
<property name="serviceInterface" value="rmi.ServerRmiI"/>
<property name="registryPort" value="1021"/>
</bean>
</beans>
说明:service本地实现,serviceName对外提供的名称对外提供的名称,registyPort开启端口
客户端:
代码结构
首先要定义对应接口
</b> <br/>view plaincopyprint?<br/><img src="https://code.csdn.net/assets/CODE_ico.png" border="0" ><br/><br/><img src="https://code.csdn.net/assets/ico_fork.svg" border="0" ><br/><br/>package client; <br/>public interface ServerRmiI { <br/> public String sayHi(String name); <br/>} <br/>1package client;
public interface ServerRmiI {
public String sayHi(String name);
}
测试方法
</b> <br/>view plaincopyprint?<br/><img src="https://code.csdn.net/assets/CODE_ico.png" border="0" ><br/><br/><img src="https://code.csdn.net/assets/ico_fork.svg" border="0" ><br/><br/>package client; <br/>import org.springframework.context.ApplicationContext; <br/>import org.springframework.context.support.ClassPathXmlApplicationContext; <br/>public class TestRun { <br/> public static void main(String[] args) { <br/> ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/config/applicationContext-rmi-client.xml"); <br/> ServerRmiI rmiI = (ServerRmiI) context.getBean("clentrmi"); <br/> System.out.println(rmiI.sayHi("rmi")); <br/> } <br/>} <br/>1package client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestRun {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/config/applicationContext-rmi-client.xml");
ServerRmiI rmiI = (ServerRmiI) context.getBean("clentrmi");
System.out.println(rmiI.sayHi("rmi"));
}
}
配置文件
view plaincopyprint?
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName" default-lazy-init="true">
<?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-2.5.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-autowire="byName" default-lazy-init="true">
<bean name="clentrmi" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://127.0.0.1:1021/serverRmiTest"/>
<property name="serviceInterface" value="client.ServerRmiI"/>
</bean>
</beans>
后台成功输出“hi”说明执行成功
需要注意:
1、RMI是很难穿越防火墙的;
2、RMI是基于Java的,这意味着客户端和服务端必须都是采用Java开发的;
3、因为RMI使用了Java的序列化机制,所以通过网络传输的对象类型必须保证在调用的两端是相同的版本。
-------------------------------------------------------------------------------------------------------------------------------------------------------------
参考:
《Spring事件(第3版)》
《Spring高级程序设计》
javaspringrmi
因为水平有限,难免有疏忽或者不准确的地方,希望大家能够直接指出来,我会及时改正。一切为了知识的分享。
后续会有更多的精彩的内容分享给大家。
支付宝扫一扫
微信扫一扫
