spring整合rmi远程调用

本贴最后更新于 3776 天前,其中的信息可能已经时移世改

前言:

      先说说,webservice和rmi的最主要的区别, rmi的客户端和服务端都必须是java,webservice没有这个限制,webservice是在http协议上传递xml文本文件。与语言和平台无关,rmi是在tcp协议上传递可序列化的java对象,只能用在java虚拟机上,绑定语言。 RMI是EJB远程调用的基础,仅用RMI技术就可以实现远程调用,使用EJB是为了实现组件,事物,资源池,集群等功能。
  WebService是通过XML来传输数据,可用http等协议因此可在异构系统间传递,并且可以穿过防火墙,可在公网上远程调用。更详细内容参看这篇文字http://blog.csdn.net/shan9liang/article/details/8995023

代码:首先为rmiservice端

定义一个接口

package com.guop.service;

public interface IHelloWorld {
public String helloWorld();

public String sayHelloToSomeBody(String name);

}

接口的实现类

package com.guop.service.impl;

import com.guop.service.IHelloWorld;

public class HelloWorldImpl implements IHelloWorld{

@Override
public String helloWorld() {
	return "hello world";
}

@Override
public String sayHelloToSomeBody(String name) {
	  return "Hello World!" + name;  
}

}

服务器启动类,这个启动类可以和tomcat关联在启动应用的时候,启动该服务就可以了

package com.guop.service;

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

public class HelloHost {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
System.out.println("rmi 服务启动了");
}
}

spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
                   	&lt;bean id=&quot;helloWorld&quot; class=&quot;com.guop.service.impl.HelloWorldImpl&quot; /&gt;

&lt;bean id=&quot;serviceExporter&quot; class=&quot;org.springframework.remoting.rmi.RmiServiceExporter&quot;&gt;
	&lt;property name=&quot;service&quot; ref=&quot;helloWorld&quot; /&gt;
	&lt;!-- 定义服务名 --&gt;
	&lt;property name=&quot;serviceName&quot; value=&quot;hello&quot; /&gt;
	&lt;property name=&quot;serviceInterface&quot; value=&quot;com.guop.service.IHelloWorld&quot; /&gt;
	&lt;property name=&quot;registryPort&quot; value=&quot;8088&quot; /&gt;
&lt;/bean&gt;

</beans>

rmiclient:client访问类

package com.guop.client;

import java.rmi.RemoteException;

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

import com.guop.service.IHelloWorld;

public class HelloClient {
public static void main(String[] args) throws RemoteException {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
IHelloWorld hs = (IHelloWorld) ctx.getBean("helloWorld");
System.out.println(hs.helloWorld());
System.out.println(hs.sayHelloToSomeBody("captain.cc"));
}
}

spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
&lt;bean id=&quot;helloWorld&quot; class=&quot;org.springframework.remoting.rmi.RmiProxyFactoryBean&quot;&gt;
	&lt;property name=&quot;serviceUrl&quot; value=&quot;rmi://localhost:8088/hello&quot; /&gt;
	&lt;property name=&quot;serviceInterface&quot; value=&quot;com.guop.service.IHelloWorld&quot; /&gt;
&lt;/bean&gt;

</beans>

 

 

  • rmi
    1 引用
  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3165 引用 • 8206 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...