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
原创粉丝点击