WEB开发网
开发学院软件开发Java Websphere Adapter 中错误处理的原理与应用 阅读

Websphere Adapter 中错误处理的原理与应用

 2009-12-24 00:00:00 来源:WEB开发网   
核心提示:错误处理机制简介什么是错误绑定在应用系统中,异常(Exception)是经常出现的,Websphere Adapter 中错误处理的原理与应用,这些异常通常分为 2 类,一类是运行时异常(Runtime Exception),通过本文的介绍,读者能够在实际的应用开发中理解错误绑定,这类异常通常是因为代码缺陷造成的;另一

错误处理机制简介

什么是错误绑定

在应用系统中,异常(Exception)是经常出现的。这些异常通常分为 2 类,一类是运行时异常(Runtime Exception),这类异常通常是因为代码缺陷造成的;另一类是业务逻辑异常 (Business Exception),这类异常通常是由于数据的不准确、不完善、应用系统无法处理造成的。

对于系统开发人员而言,运行时异常容易被捕获并进行相应的处理,处理的方式主要有忽略、包装后重新抛出、记录日志等。对于业务逻辑相关的异常,开发人员很难去分析和处理,最佳的处理方式是把这些异常信息提供给应用系统的调用者,由调用者对这些业务逻辑进行相关处理。应用系统传递给调用者的业务逻辑错误不应该是一个异常类,而是与业务相关的结构化异常数据,这样调用方不用关心服务的实现方式和编程语言,只需关注返回的数据(正确结果或者异常数据),并对这些数据进行相应的处理。另外,这些数据的格式通常是与编程语言无关的,而这正是 Websphere Adapter 的错误绑定(Fault Binding)特性所支持的。也就是说,错误绑定是 WebSphere Adapter 提供的一种与编程语言无关的处理业务逻辑异常的机制,通过此错误处理机制,能够将运行时异常和业务逻辑异常进行区分,并给应用系统的调用者提供更为有意义的错误数据。

注:错误绑定机制目前只能在 WebSphere Process Server 以及 WebSphere Application Server Feature Pack 环境中工作。

错误绑定实现原理

错误绑定是 Websphere Adapter 支持的一种错误处理机制,用在 outbound 应用场景中。错误绑定具有以下几个要素:

错误选择器(Fault selector)

错误名称(Fault name)

错误绑定类型(Fault binding type)

错误数据(Fault data)

当 WebSphere Adapter 遇到异常时,WebSphere Adapter 先调用错误选择器判断此异常是否为 WebSphere Adapter 支持的错误,如果不是,则不做处理,如果是,则调用错误选择器生成对应的错误名字。然后根据错误名字找到对应的错误绑定类型,最后调用错误绑定类型生成相应的错误数据,并将此错误数据返回给调用者。图 1 是整个错误绑定处理的流程图。


图 1. 错误绑定流程图
Websphere Adapter 中错误处理的原理与应用

查看原图(大图)

WebSphere Adapter 错误绑定的配置

WebSphere Adapter 6.2 的实现已经支持错误绑定了,如果用户要在自己的应用系统中使用错误绑定,就需要配置相应的文件— import 文件和 wsdl 文件。WebSphere Adapter 在默认情况下可以直接为用户生成预定义好的错误配置,如果用户需要使用自己定义的错误类型,则需要在 .import 文件与 .wsdl 文件中进行相应的配置。

在 import 文件中,首先需要配置一个错误选择器(faultSelector),然后在每个 method 对应的 methodBindng 中,配置支持的 faultBinding,包括错误名称和错误绑定类型,它的格式如下:

<faultBinding fault="fault name" faultBindingType=" fault binding type"/>

下面是 JDBC Adapter Outbound create 操作错误绑定的配置实例。


清单 1. 错误绑定 import 文件配置实例

 <?xml version="1.0" encoding="UTF-8"?> 
 <scdl:import name="Output" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:eis="http://www.ibm.com/xmlns/prod/websphere/scdl/eis/6.0.0" 
 xmlns:scdl="http://www.ibm.com/xmlns/prod/websphere/scdl/6.0.0"> 
 <interfaces>  ... </interfaces> 
 <esbBinding xsi:type="eis:EISImportBinding" 
 faultSelector="com.ibm.j2ca.extension.emd.runtime.WBIFaultSelectorImpl" 
 dataBindingType="com.ibm.j2ca.jdbc.emd.databinding.JDBCDataBindingGenerator"> 
  <resourceAdapter name="JDBCINConnectorApp.IBM WebSphere Adapter for JDBC" 
  type="com.ibm.j2ca.jdbc.JDBCResourceAdapter" version="6.2.0.2"/> 
  <connection type="com.ibm.j2ca.jdbc.JDBCManagedConnectionFactory" 
  interactionType="com.ibm.j2ca.jdbc.JDBCInteractionSpec"> 
   <properties> 
    ... 
   </properties> 
  </connection> 
  <methodBinding method="createCUSTOMER"> 
  <faultBinding fault="INTEGRITY_CONSTRAINT_VIOLATION"    
  faultBindingType="com.ibm.j2ca.extension.emd.runtime.WBIFaultDataBindingImpl"/> 
  <faultBinding fault="MISSING_DATA"    
 faultBindingType="com.ibm.j2ca.extension.emd.runtime.WBIFaultDataBindingImpl"/> 
 <faultBinding fault="OBJECT_NOTFOUND_EXCEPTION"    
 faultBindingType="com.ibm.j2ca.extension.emd.runtime.WBIFaultDataBindingImpl"/> 
 <faultBinding fault="UNIQUECONSTRAINT_VIOLATION"    
 faultBindingType="com.ibm.j2ca.extension.emd.runtime.WBIFaultDataBindingImpl"/> 
   <interaction> 
    <properties> 
     <functionName>Create</functionName> 
    </properties> 
   </interaction> 
  </methodBinding> 
  <!-- other methodBinding --> 
 </esbBinding> 
 </scdl:import> 

清单 1 中粗体部分分别是前文中提到的错误绑定的要素 -- 错误选择器和错误名字,错误绑定类型。

在配置 wsdl 文件前,用户首先需要准备错误数据。这些错误数据可以是用户自己定义的,也可以是 WebSphere Adapter 提供了的默认错误数据。图 2 是 WebSphere Adapter 默认提供的 RecordNotFound 错误业务对象定义。


图 2.RecordNotFound 错误数据定义
Websphere Adapter 中错误处理的原理与应用

注:WebSphere Adapter 提供了一个基础的错误数据 WBIFault,其他的所有错误数据都继承自此错误数据。

接下来,用户需在 wsdl 文件中配置相应的错误数据。用户只需根据 import 文件中 methodBinding 所支持的错误名称在 wsdl 文件对应的操作(operation)中配置相应的错误数据。清单 2 是 JDBC Adapter Outbound wsdl 文件中 create 操作的错误配置实例。


清单 2.WSDL 文件错误数据配置实例

 <?xml version="1.0" encoding="UTF-8"?> 
 <wsdl:definitions name="JDBCOutboundInterface1.wsdl" … > 
 <wsdl:types> 
 <xsd:schema elementFormDefault="qualified" 
 targetNamespace="http://com/ibm/j2ca/fault/afcfault" 
 xmlns:tns="http://com/ibm/j2ca/fault/afcfault"> 
   <xsd:include schemaLocation="UniqueConstraintFault.xsd"/> 
   <xsd:include schemaLocation="ObjectNotFoundFault.xsd"/> 
   <xsd:include schemaLocation="MissingDataFault.xsd"/> 
   <xsd:include schemaLocation="IntegrityConstraintFault.xsd"/> 
  </xsd:schema> 
  <xsd:schema elementFormDefault="qualified" 
  targetNamespace="http://ForDW/JDBCOutboundInterface1" 
  xmlns:bons5="http://com/ibm/j2ca/fault/afcfault"> 
   <xsd:import namespace="http://com/ibm/j2ca/fault/afcfault"/> 
   <xsd:import 
    namespace="http://www.ibm.com/xmlns/prod/websphere/j2ca/jdbc/customerbg" 
   schemaLocation="CustomerBG.xsd"/> 
   <xsd:element name="createCustomerBG"> ...  
   </xsd:element> 
   <xsd:element name="createCustomerBGResponse"> ...  
   </xsd:element> 
    
 <xsd:element name="MissingDataFault_element" nillable="true" 
 type="bons5:MissingDataFault"/> 
   <xsd:element name="UniqueConstraintFault_element" nillable="true" 
   type="bons5:UniqueConstraintFault"/> 
   <xsd:element name="IntegrityConstraintFault_element" nillable="true" 
   type="bons5:IntegrityConstraintFault"/> 
   <xsd:element name="ObjectNotFoundFault_element" nillable="true" 
  type="bons5:ObjectNotFoundFault"/> 
  </xsd:schema> 
 </wsdl:types> 
 <!-- wsdl:message 
 ... 
 </wsdl:message --> 
 <wsdl:portType name="JDBCOutboundInterface1"> 
  <wsdl:operation name="createCustomerBG"> 
 <wsdl:input message="tns:createCustomerBGRequestMsg" 
 name="createCustomerBGRequest"/> 
 <wsdl:output message="tns:createCustomerBGResponseMsg" 
 name="createCustomerBGResponse"/> 
 <wsdl:fault message="tns:createCustomerBGMissingDataFaultMsg" 
 name="MISSING_DATA"/> 
 <wsdl:fault message="tns:createCustomerBGUniqueConstraintFaultMsg" 
 name="UNIQUECONSTRAINT_VIOLATION"/> 
 <wsdl:fault message="tns:createCustomerBGIntegrityConstraintFaultMsg" 
 name="INTEGRITY_CONSTRAINT_VIOLATION"/> 
 <wsdl:fault message="tns:createCustomerBGObjectNotFoundFaultMsg" 
 name="OBJECT_NOTFOUND_EXCEPTION"/> 
 </wsdl:operation> 
 </wsdl:portType> 
 </wsdl:definitions> 

WebSphere Adapter 支持的错误绑定

WebSphere Adapter 作为一个与各种 EIS 系统进行连接的产品,支持两大类的产品,一类称作 Application Adapter,用来连接诸如 SAP,Siebel,Oracle EBS 等不同的应用系统,另外一类称为 Technology Adapter,用于连接 JDBC,Email,FTP 等标准的通用技术。各个不同的 Adapter 在其错误绑定上有某些相似之处。基于此种考虑,WebSphere Adapter 在其基础实现类中定义了 5 种通用的错误绑定,每个 Adapter 既可以直接使用这 5 种错误绑定,也可以很方便的定义自己特殊的错误绑定。下面的章节,我们先来看一下 WebSphere Adapter 定义的 5 中通用错误绑定和相应的错误数据。


表 1. 基本错误绑定类型

错误类型名称 错误类型实现类 错误类型业务对象
DUPLICATE_RECORD DuplicateRecordException DuplicateRecordFault
MATCHES_EXCEEDED_LIMIT MatchesExceededLimitException MatchesExceededLimitFault
MULTIPLE_MATCHING_RECORDS MultipleMatchingRecordsException MultipleMatchingRecordsFault
RECORD_NOT_FOUND RecordNotFoundException RecordNotFoundFault
MISSING_DATA MissingDataException MissingDataFault

下面是 IBM WebSphere Adapter for JDBC 所支持的错误绑定类型,其他 Adapter 所支持的错误绑定类型,本文不再一一列举,有兴趣的读者可以在附件中找到相应 Adapter 所支持的全部错误绑定清单:


表 2.IBM WebSphere Adapter for JDBC 错误绑定定义清单

错误类型名称 详细描述
RecordNotFoundFault 执行 Retrieve,RetrieveAll,Delete,Update 操作时,如果相应的数据库记录不存在,JDBC Adapter 会抛出该类错误,指示用户所存取的记录并不存在,用户可以根据相应的错误信息进行处理
MissingDataFault 执行 outbound 操作时,业务对象没有填写必须填写的的属性信息,JDBC Adapter 会抛出该类错误,例如在执行 Create 操作时,用户没有输入相应的主键键值,JDBC adapter 就会抛出该错误提示用户。默认情况下 Create, Delete, Update, Retrieve, ApplyChanges 和 Exists 操作时会产生该类错误。
IntegrityConstraintFault JDBC Adapter 单独定义的一种错误。当执行 Create 操作时,如果数据库抛出违背完整性限制的异常信息,JDBC Adapter 会抛出此类错误。例如,执行 Update 操作时对于子业务对象的外键键值和父业务对象对应的键值不能匹配时,JDBC adapter 就会抛出该错误
MatchesExceededLimitFault JDBC Adapter 执行 RetrieveAll 操作时,当返回的查询结果记录条目超过了 MaxRecords 属性所配置的条目时,JDBC Adapter 会抛出该错误
MultipleMatchingRecordsFault 执行 Retrieve 或者 Update 操作时,如果有超过一条记录符合查询条件,JDBC Adapter 会抛出该错误
ObjectNotFoundFault 此错误类型为 JDBC adapter 单独定义的一种错误。执行 Create 或者 Update 操作时,JDBC Adapter 处理 Single-cardinality 子业务对象,如果子业务对象对应的记录在数据库中不存在,JDBC Adapter 会抛出该错误
UniqueConstraintFault 此错误类型为 JDBC adapter 单独定义的一种错误。执行 Create 或 Update 操作时,如果 JDBC Adapter 接收到 SQLException,JDBC Adapter 会抛出该错误

除了本文列举了 IBM WebSphere Adapter for JDBC 预定好的错误类型外,用户还可以根据自己的需求定制自己的错误绑定,不过这不在本文的讨论范围。下面的章节我们将通过 IBM WebSphere Adapter for JDBC 来展示一个错误绑定的应用实例。

错误绑定应用示例

下面,我们将通过 WebSphere Adapter for JDBC Adapter 的一个示例来展示错误绑定的应用。对于 WebSphere Adapter 而言,每个操作支持的错误绑定是每个 Adapter 预先定义的,并在生成服务接口的时候自动生成相应的错误数据类型。下图所示的是一个 JDBC Adapter 的服务接口定义文件,可以看到,针对 Retrieve 操作,生成了 3 个不同的错误数据类型:


图 3. 服务接口定义示例
Websphere Adapter 中错误处理的原理与应用

查看原图(大图)

关于每个操作所支持的错误绑定的含义,请参考上文。

除了在 WSDL 服务定义文件中定义这些错误数据类型外,JDBC Adapter 还会在对应的 Import 文件定义相应的错误绑定,见清单 3。


清单 3.Import 文件示例
 <?xml version="1.0" encoding="UTF-8"?> 
 <scdl:import xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:EIS="http://www.ibm.com/xmlns/prod/websphere/scdl/eis/6.0.0" 
 xmlns:ns1="http://testJDBC1/JDBCOutboundInterface" 
 xmlns:scdl="http://www.ibm.com/xmlns/prod/websphere/scdl/6.0.0" 
 xmlns:wsdl="http://www.ibm.com/xmlns/prod/websphere/scdl/wsdl/6.0.0" 
 displayName="JDBCOutboundInterface" name="JDBCOutboundInterface"> 
 <interfaces> 
  <interface xsi:type="wsdl:WSDLPortType" preferredInteractionStyle="sync" 
  portType="ns1:JDBCOutboundInterface"> 
   <scdl:interfaceQualifier xsi:type="scdl:JoinTransaction" value="true"/> 
  </interface> 
 </interfaces> 
 <esbBinding xsi:type="EIS:EISImportBinding" 
 faultSelector="com.ibm.j2ca.extension.emd.runtime.WBIFaultSelectorImpl" 
 dataBindingType="com.ibm.j2ca.jdbc.emd.databinding.JDBCDataBindingGenerator"> 
  <resourceAdapter 
  name="testJDBC1App.IBM WebSphere Adapter for JDBC with local transaction support" 
  type="com.ibm.j2ca.jdbc.JDBCResourceAdapter" version="7.0.0.0"> 
   … 
  </resourceAdapter> 
  <connection type="com.ibm.j2ca.jdbc.JDBCManagedConnectionFactory" 
  interactionType="com.ibm.j2ca.jdbc.JDBCInteractionSpec"> 
   … 
  </connection> 
   
  <methodBinding method="retrieveallDb2adminCustomerBG"> 
 <faultBinding fault="MATCHES_EXCEEDED_LIMIT" 
 faultBindingType="com.ibm.j2ca.extension.emd.runtime.MatchingFaultDataBinding"/> 
  <faultBinding fault="RECORD_NOT_FOUND" 
  faultBindingType="com.ibm.j2ca.extension.emd.runtime.WBIFaultDataBindingImpl"/> 
 <interaction> 
 <properties> 
 <functionName>RetrieveAll</functionName> 
 </properties> 
 </interaction> 
 </methodBinding> 
… 
 <!-- other methodBinding --> 
 </esbBinding> 
 </scdl:import> 

对于应用程序的调用者而言,只需要知道 JDBC Adapter 的每个操作能够支持哪些错误,并且根据返回的错误数据进行相应处理即可。运行时,如果 JDBC Adapter 捕获的异常信息为一个业务逻辑异常,通过错误绑定机制的处理,将会返回对应的错误数据,调用者只需处理该错误数据对象即可。见下图显示的是返回的错误数据:


图 4. 业务逻辑异常处理结果
Websphere Adapter 中错误处理的原理与应用

查看原图(大图)

如果运行时抛出的异常信息不是业务逻辑错误的话, Adapter 会直接抛出相应的异常信息,而不是错误数据对象。见下图的处理结果:


图 5. 一般异常处理结果
Websphere Adapter 中错误处理的原理与应用

查看原图(大图)

结束语

本文首先介绍了错误绑定的原理,并介绍了 WebSphere Adapter 所支持的错误绑定及其使用场景。最后通过 2 个例子展现了错误绑定在处理业务逻辑异常与运行时异常时的区别。通过本文的介绍,读者能够在实际的应用开发中理解错误绑定,并能够灵活应用这些错误绑定。

Tags:Websphere Adapter 错误

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接