java rmi (3)Spring对RMI的支持

1.使用RMI暴露服务

使用SpringRMI支持,你可以通过RMI基础设施透明的暴露你的服务。设置好SpringRMI支持后,你会看到一个和远程EJB接口类似的配置,只是没有对安全上下文传递和远程事务传递的标准支持。当使用RMI调用器时,Spring对这些额外的调用上下文提供了钩子,你可以在此插入安全框架或者定制的安全证书。


2. 使用 RmiServiceExporter 暴露服务

使用 RmiServiceExporter,我们可以把AccountService对象的接口暴露成RMI对象。可以使用 RmiProxyFactoryBean 或者在传统RMI服务中使用普通RMI来访问该接口。RmiServiceExporter 显式地支持使用RMI调用器暴露任何非RMI的服务。 

当然,我们首先需要在Spring BeanFactory中设置我们的服务: 

< bean  id ="accountService"  class ="example.AccountServiceImpl" >

    
<!--  any additional properties, maybe a DAO?  -->

</ bean >

然后,我们将使用 RmiServiceExporter 来暴露我们的服务: 

< 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 ="AccountService" />

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

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

<!--  defaults to 1099  -->

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

</ bean >

正如你所见,我们覆盖了RMI注册的端口号。通常,你的应用服务也会维护RMI注册,最好不要和它冲突。更进一步来说,服务名是用来绑定下面的服务的。所以本例中,服务绑定在 rmi://HOST:1199/AccountService。在客户端我们将使用这个URL来链接到服务。 

注意:我们省略了一个属性,就是 servicePort 属性,它的默认值为0。 这表示在服务通信时使用匿名端口。当然如果你愿意的话,也可以指定一个不同的端口。 


3. 在客户端链接服务

我们的客户端是一个使用AccountService来管理account的简单对象: 

public   class  SimpleObject {

  
private  AccountService accountService;

  
public   void  setAccountService(AccountService accountService) {

    
this .accountService  =  accountService;

  }

}

为了把服务连接到客户端上,我们将创建另一个单独的bean工厂,它包含这个简单对象和服务链接配置位: 

< bean  class ="example.SimpleObject" >

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

</ bean >

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

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

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

</ bean >

这就是我们在客户端为支持远程account服务所需要做的。Spring将透明的创建一个调用器并且通过RmiServiceExporter使得account服务支持远程服务。在客户端,我们用RmiProxyFactoryBean连接它。


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;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值