spring搭建Java RMI
来源:互联网 发布:php工程师的工作内容 编辑:程序博客网 时间:2024/06/05 17:21
服务器端:
1.创建接口
package service;public interface IRmiService {public String sayHello();public void pringHello();}
2.创建接口实现类
package serviceimp;import service.IRmiService;public class RmiServiceImp implements IRmiService {public String sayHello() {return "sayHello...";}public void pringHello() {System.out.println("printHello....");}}
3.配置spring配置文件
<bean id="iRmiService" class="serviceimp.RmiServiceImp"></bean> <!-- 利用RmiServiceExporter发布iRmiService为RMI --> <bean class="org.springframework.remoting.rmi.RmiServiceExporter"> <!-- RMI服务 --> <property name="service" ref="iRmiService"></property> <!-- RMI服务名称 --> <property name="serviceName" value="test"></property> <!-- RMI服务实现的接口 --> <property name="serviceInterface" value="service.IRmiService"></property> <!-- 绑定端口 --> <property name="registryPort" value="8888"></property> </bean>
RmiServiceExporter可以将任意一个spring管理的bean发布为RMI服务。查看RmiServiceExporter源码,在默认情况下会尝试将RMI服务绑定到本机的1099端口。
如下源码所示:
RmiServiceExporter
public class RmiServiceExporter extends RmiBasedExporter implements InitializingBean, DisposableBean {private String serviceName;private int servicePort = 0; // anonymous portprivate RMIClientSocketFactory clientSocketFactory;private RMIServerSocketFactory serverSocketFactory;private Registry registry;private String registryHost;<span style="color:#ff0000;">private int registryPort = Registry.REGISTRY_PORT;</span>private RMIClientSocketFactory registryClientSocketFactory;...}
private int registryPort = Registry.REGISTRY_PORT;即RmiServiceExporter默认设置服务端口
Registry
public interface Registry extends Remote { /** Well known port for registry. */ public static final int REGISTRY_PORT = 1099;...}
如果希望将RMI注册表绑定到指定端口或主机,可以通过registryHost和registryPort属性来绑定。
4.启动服务端
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");System.out.println("service start...");}}
客户端:
1.配置xml文件
<bean id="iRmiService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"><property name="serviceUrl" value="rmi://192.168.1.146:8888/test"></property><property name="serviceInterface" value="service.IRmiService"></property></bean>
spring的RmiRegistryFactoryBean是一个工厂Bean,通过该Bean可以为RMI服务创建代理。
RmiRegistryFactoryBean
public class RmiRegistryFactoryBean implements FactoryBean<Registry>, InitializingBean, DisposableBean {protected final Log logger = LogFactory.getLog(getClass());private String host;private int port = Registry.REGISTRY_PORT;private RMIClientSocketFactory clientSocketFactory;private RMIServerSocketFactory serverSocketFactory;private Registry registry;private boolean alwaysCreate = false;private boolean created = false;....}
通过源码可以观察到,可以指定RMI服务端口与主机等。
在这里可以直接使用serviceUrl来指定RMI服务。注意serviceUrl的value,rmi://192.168.1.146:8888/test这里的路径为rmi://hostIP:port/serviceName
2.运行客户端连接
public class Test {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");IRmiService is = (IRmiService) ctx.getBean("iRmiService");is.pringHello();System.out.println(is.sayHello());}}
测试结果:
服务器端:
2015-1-8 15:49:04 org.springframework.remoting.rmi.RmiServiceExporter getRegistry信息: Looking for RMI registry at port '8888'2015-1-8 15:49:05 org.springframework.remoting.rmi.RmiServiceExporter getRegistry信息: Could not detect RMI registry - creating new one2015-1-8 15:49:05 org.springframework.remoting.rmi.RmiServiceExporter prepare信息: Binding service 'test' to RMI registry: RegistryImpl[UnicastServerRef [liveRef: [endpoint:[192.168.1.146:8888](local),objID:[0:0:0, 0]]]]service start...
客户端:
2015-1-8 15:50:20 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@bf32c: startup date [Thu Jan 08 15:50:20 CST 2015]; root of context hierarchy2015-1-8 15:50:21 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext.xml]sayHello...
0 0
- spring搭建Java RMI
- 【Java】RMI框架搭建
- JAVA Spring RMI(1)--Spring整合Rmi总结
- java rmi 代理包开发 ,spring rmi 设置超时
- Java RMI技术以及Spring封装了的RMI技术
- Java RMI技术以及Spring封装了的RMI技术
- Java RMI技术以及Spring封装了的RMI技术
- Java RMI技术以及Spring封装了的RMI技术
- java rmi (3)Spring对RMI的支持
- 【Distribution】Java RMI - 3 - Spring对RMI的支持
- Java RMI实现以及Spring封装RMI实现小结
- Java RMI实现以及Spring封装RMI实现小结
- Java RMI实现以及Spring封装RMI实现小结
- Java RMI实现以及Spring封装RMI实现小结
- Spring RMI java.rmi.NoSuchObjectException: no such object in table
- spring整合RMI - Java远程方法调用
- spring 对java RMI 的支持
- spring 对java RMI 的支持
- YCbCr
- nodejs学习
- windows和linux环境下memcached的安装
- 微信营销,到底需不需要做呢?
- VC++ 2010: /SUBSYSTEM (Specify Subsystem) - Console application or windows GUI application?
- spring搭建Java RMI
- mex, matlab中调用C 编程
- Windows下安装Redis
- Android 4.4 Kitkat 使能有线网络 Ethernet
- 揭秘淘宝286亿海量图片存储与处理架构
- Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable
- winform打包 InstallShield Limited Edition 下载不了
- Mac系统中QQ记录的存储路径
- c++ 学习笔记