spring包装的rmi

来源:互联网 发布:网络最红张老师视频 编辑:程序博客网 时间:2024/05/09 21:11

rmi之远程接口的调用,简单点说 就是客户端能调用 服务器端的接口

值得注意的几个地方:

1.既然是远程接口调用,首先当然是要建立接口 以及要方法

2.其次重要的是 由于这里用的是spring包装的rmi,所以我们只须在spring配置文件里配置bean就ok了

3.服务器端的接口一定要拷贝一份到客户端,而且包名一定要相同

 

 

 

在MyEclipse中新建一个rmiServer的java project,然后导入下面几个包:spring.jar;spring-aspects.jar;spring-mock.jar;common-logging.jar;cglib-nodep-2.1_3.jar; 
第一步: 
建立test.spring.remote.rmi包,然后在此包下新建一接口MyService.java,代码如下: 

package test.spring.remote.rmi; 

public interface MyService extends java.rmi.Remote { 
public void doSomething() throws java.rmi.RemoteException; 
public String sayHello() throws java.rmi.RemoteException; 


然后实现这个接口MyServiceImpl.java,代码如下: 

package test.spring.remote.rmi; 

public class MyServiceImpl implements MyService { 

public void doSomething(){ 
// TODO Auto-generated method stub 



public String sayHello(){ 
// TODO Auto-generated method stub 
System.out.println("rmi client call rmi server..."); 
return "hello world!"; 



第二步: 
配置applicationContext.xml,代码如下: 

<?xml version="1.0" encoding="UTF-8"?> 
<beans 
xmlns="http://www.springframework.org/schema/beans" 
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-2.0.xsd"> 

<bean id="myService" class="test.spring.remote.rmi.MyServiceImpl" /> 
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> 
<property name="serviceName"> 
<value>myService</value> 
</property> 
<property name="service"> 
<ref bean="myService" /> 
</property> 
<property name="serviceInterface"> 
<value>test.spring.remote.rmi.MyService</value> 
</property> 
<property name="registryPort"> 
<value>1199</value> 
</property> 
</bean> 
</beans> 

第三步: 
在test.spring.remote.rmi包下建启动rmi server的RmiServer.java,代码如下: 

package test.spring.remote.rmi; 

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

public class RmiServer { 
public static void main(String[] args) { 
ApplicationContext context = new ClassPathXmlApplicationContext( 
"applicationContext.xml"); 




第四步: 
Run As--->Java Application,运行RmiServer,控制台打印出“Binding service 'myService' to RMI registry: RegistryImpl[UnicastServerRef [liveRef: [endpoint:[192.168.8.109:1199](local),objID:[0:0:0, 0]]]]”信息 

第五步: 
新建一个rmiClient的java project,导入相同的jar包,然后建一个相同的test.spring.remote.rmi包 

第六步: 
将rmiServer中的MyService.java拷贝到rmiClient的test.spring.remote.rmi包下 

第七步: 
配置rmiClient项目中的applicationContext.xml,代码如下: 

<?xml version="1.0" encoding="UTF-8"?> 
<beans 
xmlns="http://www.springframework.org/schema/beans" 
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-2.0.xsd"> 

<bean id="rmiService" 
class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> 
<property name="serviceInterface"> 
<value>test.spring.remote.rmi.MyService</value> 
</property> 
<property name="serviceUrl"> 
<value>rmi://localhost:1199/myService</value> 
</property> 
</bean> 
</beans> 

第八步: 
在test.spring.remote.rmi包下新建rmi的客户端TestSpringRmi.java,代码如下: 

package test.spring.remote.rmi; 

import java.rmi.RemoteException; 

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

public class TestSpringRmi { 
public static void main(String[] args) { 
ApplicationContext context = new ClassPathXmlApplicationContext( 
"applicationContext.xml"); 
MyService service=(MyService)context.getBean("rmiService"); 

try { 
System.out.println(service.sayHello()); 
} catch (RemoteException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 


原创粉丝点击