spring对RMI支持

今天在网上看到一篇关于RMI介绍的文章,感觉不错,连我这个没用过RMI的人都看懂了,因此摘抄过来笔记,分享

RMI全称是Remote Method Invocation-远程方法调用。支持存储于不同地址空间的程序级对象之间彼此进行通信,实现远程对象之间的无缝远程调用。

一、RMI(远程方法调用)

一个正常工作的RMI系统由下面几个部分组成: 

·远程服务的接口定义 

·远程服务接口的具体实现 

·桩(Stub)和框架(Skeleton)文件 

·一个运行远程服务的服务器 

·一个RMI命名服务,它允许客户端去发现这个远程服务 

·类文件的提供者(一个HTTP或者FTP服务器) 

·一个需要这个远程服务的客户端程序

二、RMI(远程方法调用)原理示意图



方法调用从客户对象经占位程序(Stub)、远程引用层(Remote Reference Layer)和传输层(Transport Layer)向下,传递给主机,然后再次经传 输层,向上穿过远程调用层和骨干网(Skeleton),到达服务器对象。 占位程序扮演着远程服务器对象的代理的角色,使该对象可被客户激活。 远程引用层处理语义、管理单一或多重对象的通信,决定调用是应发往一个服务器还是多个。传输层管理实际的连接,并且追踪可以接受方法调用的远程对象。服务器端的骨干网完成对服务器对象实际的方法调用,并获取返回值。返回值向下经远程引用层、服务器端的传输层传递回客户端,再向上经传输层和远程调用层返回。最后,占位程序获得返回值。 

要完成以上步骤需要有以下几个步骤: 

1、 生成一个远程接口 

2、 实现远程对象(服务器端程序)

3、 生成占位程序和骨干网(服务器端程序)

4、 编写服务器程序 

5、 编写客户程序 

6、 注册远程对象 

7、 启动远程对象

三、spring与RMI整合

在OMAS系统中提供给业务系统的RMI客户反馈服务的实现服务暴露是通过Resource/modules/interfaces/spring-conf/serviceContext.xml配置文件实现的,而远程接口的实现类必须序列化(即实现Serializable接口)。

Resource/modules/interfaces/spring-conf/serviceContext.xml的内容如下:

<?xml version="1.0" encoding="UTF-8"?>

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

<beans default-autowire="byName" default-lazy-init="false">

<!-- service实现类的配置 -->

<bean id="fbWebService" class="com.ce.omas.interfaces.service.impl.FeedbackWebServiceImpl" />

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

<!-- does not necessarily have to be the same name as the bean to be exported -->

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

<property name="service" ref="fbWebService" />

<property name="serviceInterface" value="com.ce.omas.interfaces.service.IFeedbackWebService" />

<!-- <property name="registryHost" value="rmi://192.168.100.7"/> -->

<!-- defaults to 1099 -->

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

</bean>

</beans>
对应的暴露的服务接口如下:

public interface IFeedbackWebService {

/**

 * <b>方法用途和描述:</b> 客户反馈RMI服务端接口方法<br>

 * <b>方法的实现逻辑描述:</b> 通过RMI提供服务,Spring支持的RMI远程调用

 * @param systemID : 业务系统的唯一标识

 * @param feedbackType :用户反馈的类型(1-系统BUG、2-系统易用性、3-客服人员态度、4-运维人员态度、5-其他)

 * @param feedbackContent :用户反馈的正文内容

 * @return 反馈是否成功 true | false

 */

public boolean setFeedback(String systemID, FeedbackType feedbackType,

String feedbackContent) throws OMASServiceException ;

}

暴露的服务接口实现如下:


public class FeedbackWebServiceImpl implements IFeedbackWebService,  Serializable {

private static Log _log = LogFactory.getLog(FeedbackWebServiceImpl.class);

private static final long serialVersionUID = -5532505108644974594L;

/**

 * 客户反馈服务接口

 */

private IFeedbackOperationService feedbackService;

/**

* 方法用途和描述: 注入运营模块的添加客户反馈的服务

* @param feedbackWebService 运营模块服务

 */

public void setFeedbackService(IFeedbackOperationService feedbackWebService) {

_log.info("注入运营模块的添加客户反馈的服务");

this.feedbackService = feedbackWebService;

}

/**

* 方法用途和描述: 外部接口子系统中添加客户反馈的方法

* @param systemID :业务系统ID

* @param feedbackType :反馈类型

* @param feedbackContent :反馈内容

* @return 操作是否成功 ture or false

 * @throws ServiceException 

 */

public boolean setFeedback(String systemID, FeedbackType feedbackType, String feedbackContent) throws OMASServiceException {

_log.info("进入到外部接口的添加客户反馈的方法");

if (BlankUtil.isBlank(systemID) || BlankUtil.isBlank(feedbackType)

|| BlankUtil.isBlank(feedbackContent)) {

_log.error("添加客户反馈的接口参数为空!");

throw new OMASServiceException("omas.interfaces.001");//添加客户反馈的接口参数为空

}

WebServiceFeedbackVO vo = new WebServiceFeedbackVO();

vo.setFeedbackType(String.valueOf(feedbackType.getValue()));

vo.setFeedbackContent(feedbackContent);

vo.setSystemID(systemID);

_log.info("调用运营子系统的添加客户反馈的方法开始!");

try {

if (feedbackService == null) {

_log.error("运营模块服务为空");

throw new OMASServiceException("omas.interfaces.002");//运营模块服务为空

}

feedbackService.addFeedbackOperation(vo);

} catch (ServiceException e) {

_log.error("调用运营子系统的添加客户反馈出现异常:"+e.getMessage());

if(ExceptionConstants.EXCEPTION_OMAS_FEEDBACK_VO.equals(e.getMsgKey())){//客户调用接口的对像为空

throw new OMASServiceException("omas.interfaces.003");

} if(ExceptionConstants.EXCEPTION_OMAS_FEEDBACK_SYSTEMID.equals(e.getMsgKey())){//业务系统标识ID为空

throw new OMASServiceException("omas.omasservice.010");

} if(ExceptionConstants.EXCEPTION_OMAS_FEEDBACK_SIZE.equals(e.getMsgKey())){//非法的业务系统唯一标识

throw new OMASServiceException("omas.interfaces.004");

} if(ExceptionConstants.EXCEPTION_OMAS_FEEDBACK_BASE.equals(e.getMsgKey())){//数据库访问出了一点小问题!

throw new OMASServiceException("omas.interfaces.005");

}

throw new OMASServiceException("omas.omasservice.000");//未捕获到的异常信息!

}

return true;

}

}

接口方法setFeedbackString, FeedbackType, String)的实现大家不用关心,其与RMI并无关系,只是一些纯业务处理逻辑而已,要注意的是接口实现类必须实现 IfeedbackWebServiceSerializable接口。

在客户本地的omasservice.jar包中客户反馈的RMI客户端的配置如下:

Resource/config/omasrmi-client.xml

<?xml version="1.0" encoding="UTF-8"?>

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

<beans default-autowire="byName" default-lazy-init="true">

<bean id="fbWebServiceProxy"

class="org.springframework.remoting.rmi.RmiProxyFactoryBean">

<property name="serviceUrl">

<value>rmi://127.0.0.1:1199/FeedbackRMIService</value>

</property>

<property name="serviceInterface">

<value>com.ce.omas.interfaces.service.IFeedbackWebService</value>

</property>

</bean>

<bean class="com.ce.omas.omasservice.service.impl.FeedbackRMIClientImpl">

<property name="feedbackWebService" ref="fbWebServiceProxy" />

</bean>

</beans>

客户端调用RMI服务的方法如下所示:

/**

* 方法用途和描述: 客户反馈:通过RMI方法与OMAS通讯

* 方法的实现逻辑描述:

* @param feedbackType

* @param feedbackContent

* @return

* @throws OMASServiceException

*/

public static boolean setFeedback_RMIClient(String systemID, FeedbackType feedbackType, String feedbackContent) throws OMASServiceException {

if (systemID == null || "".equals(systemID)) {

_log.error("业务系统标识<SystemID>为空或不是对象");

throw new OMASServiceException("omas.omasservice.010");

}

String rmiClientConfigFilePath = PropertyReader .getValue(ConfigConstants.OMASSERVICE_CONFIG_PATH, ConfigConstants.RMI_CLIENT_CONFIG_FILEPATH);

if (rmiClientConfigFilePath == null || "".equals(rmiClientConfigFilePath)) {

_log.error("配置文件错误:Key<rmiClientConfigFile>为空或不存在");

throw new OMASServiceException("omas.omasservice.006");

}

_log.info("rmiClientConfigPath = " + rmiClientConfigFilePath);

ApplicationContext context = null;

try {

context = new ClassPathXmlApplicationContext(rmiClientConfigFilePath);

} catch (Exception e) {

_log.error("客户反馈:解析rmi-config.xml文件时出现异常:" + e);

_log.info("rmi-config.xml文件路径:"+rmiClientConfigFilePath);

throw new OMASServiceException("omas.omasservice.007");

}

IFeedbackWebService service = null;

try {

service = (IFeedbackWebService) context .getBean("fbWebServiceProxy");

} catch (Exception e) {

_log.error("从Spring的RMI客户端Bean配置文件获取服务对象时出现异常:" + e);

throw new OMASServiceException("omas.omasservice.009");

}

boolean bln = service.setFeedback(systemID, feedbackType, feedbackContent);

_log.info("反馈操作是否成功[true|false]:" + bln);

return bln;

}



更详细的可以查看原文http://www.blogjava.net/zhenyu33154/articles/320245.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值