前言:
先说说,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 "><bean id="helloWorld" class="com.guop.service.impl.HelloWorldImpl" /> <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="service" ref="helloWorld" /> <!-- 定义服务名 --> <property name="serviceName" value="hello" /> <property name="serviceInterface" value="com.guop.service.IHelloWorld" /> <property name="registryPort" value="8088" /> </bean>
</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 "><bean id="helloWorld" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://localhost:8088/hello" /> <property name="serviceInterface" value="com.guop.service.IHelloWorld" /> </bean>
</beans>
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于