您的位置:首页 > 编程语言 > Java开发

spring中实现rmi的一个例子及说明

2009-03-06 14:29 330 查看
rmi是java体系中实现分布式计算的重要手段.spring也提供了rmi的远程服务调用,本文主要讨论如何用spring的IOC实现rmi的远程服务调用

首先定义需要使用的域对象(Account), 这是由远端计算以后产生Account的对象,并将该对象传递给客户端:

package rmi;

import java.io.Serializable;

public class Account implements Serializable{

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}



定义远程提供的模拟的服务insertAccount, getAccounts
定义域服务
package rmi;

import java.util.*;

public interface AccountService {

public void insertAccount(Account account);

public List getAccounts(String name);

}


实现远程的服务服务(通常情况下需要访问数据库而构造出域对象)
package rmi;

import java.util.List;

import java.util.*;

public class AccountServiceImpl implements AccountService {

public void insertAccount(Account account) {

System.out.println(account.getName());

}

public List getAccounts(String name) {

List accountList = new ArrayList();

Account a = new Account();

a.setName("kevin");

accountList.add(a);

return accountList;

}

}


定义客户端:
客户端是一个使用AccountService管理账户的简单对象
package rmi;

public class SimpleObject {

private AccountService accountService;

public void setAccountService(AccountService accountService) {

this.accountService = accountService;

}

}


实现客户端
package rmi;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.*;

public class AccountTest {

public static void main(String[] args) {

ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(

new String[] { "rmi/rmi.xml" });

SimpleObject so = (SimpleObject)appContext.getBean("simpleService");

AccountService as = so.getAccountService();

List accounts = as.getAccounts("kevin");

Iterator iter = accounts.iterator();

while(iter.hasNext()){

Account account = (Account)iter.next();

System.out.println(account.getName());

}

}

}


配置bean之间的关系
配置文件:rmi.xml
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<!--域服务-->

<bean id="accountService" class="rmi.AccountServiceImpl"/>

<!--注册域服务-->

<bean class="org.springframework.remoting.rmi.RmiServiceExporter">

<property name="serviceName"><value>AccountService</value></property>

<property name="service"><ref bean="accountService"/></property>

<property name="serviceInterface">

<value>rmi.AccountService</value>

</property>

<property name="registryPort"><value>1199</value></property>

</bean>

<!--客户端查找域服务的stub-->

<bean id="simpleService" class="rmi.SimpleObject">

<property name="accountService"><ref bean="accountServiceClient"/></property>

</bean>

<bean id="accountServiceClient" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">

<property name="serviceUrl"><value>rmi://localhost:1199/AccountService</value></property>

<property name="serviceInterface"><value>rmi.AccountService</value></property>

</bean>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐