级别: 初级 Boris Lublinsky (boris.lublinsky@cna.com), 企业构架师, CNA Insurance
2005 年 2 月 01 日 本文描述了能够极大简化 WebSphere Business Integration Message Broker V5 代码的维护和提升的脚本化部署代码。脚本化的部署代码可以使您自动操作并修改 BAR 文件的参数,减少人工参与及错误的总量。利用部署脚本还可以保证一致性并提高操作质量。
引言
在 WebSphere® Business Integration Message Broker V5 (以下简称 WebSphere Message Broker) 问世之前,每个消息集和消息流必须通过将自身导出到适当环境中的控制中心来进行部署。通过引入部署单元,即可配置的代理归档文件 (configurable broker archive,BAR),WebSphere Message Broker 改变了这种状况。BAR 文件是包含大量消息集、流和描述部署参数清单的部署单元。WebSphere Message Broker 提供了支持部署 BAR 文件的命令,但却没有提供对 BAR 文件清单脚本化(修改参数) 的支持。修改 BAR 文件参数的推荐方法是利用需要对参数进行手动修改的基于 Eclipse 的工具。尽管该方法通常是有效的,但易出错,尤其是随着参数的增加。本文描述了一种基于一组参数的、脚本化的部署方法,可以在此 下载。本文从描述 BAR 文件的内容开始,然后引入可以修改参数的代码。
剖析 BAR 文件
代理归档是对部署或动态安装可执行内容到 WebSphere Message Broker 域中的封装。代理归档是以 “bar”为扩展名的标准压缩格式文件,可以利用压缩工具对其内容进行添加、删除或修改操作。BAR 文件包含部署描述符、已编译的消息流和消息集定义。
部署描述符是存储在代理档案中 META-INF/broker.xml 文件中的 XML 文件。部署描述符指定了 BAR 档案中所有消息流文件中的可配置属性的重载。部署描述符提供了简单的容易对可配置参数及其值进行访问的文本编辑器。部署参数指定为由名称和值组成元组的格式。名称以统一资源标识符 (universal resource identifier,URI) 的格式定义,并在消息流中指定参数的位置。遵从部署描述符模式的部署描述符如图 1。
图 1. 部署描述符模式
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Broker">
<xsd:annotation><xsd:documentation>
The document element of the broker.xml file is always a Broker element.
The structure of the broker.xml file may not be extended or modified
by users.
</xsd:documentation></xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="CompiledMessageFlow" minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="CompiledMessageFlow">
<xsd:annotation><xsd:documentation>
A CompiledMessageFlow element defines all the property overrides
for a single compiled msgflow (CMF) file in the root folder of
the bar archive.
</xsd:documentation></xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="ConfigurableProperty" minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ConfigurableProperty">
<xsd:annotation><xsd:documentation>
A ConfigurableProperty optionally specifies a deploy-time value
for a single configurable property.
The URI attribute specifies the broker-schema qualified flow name,
the node name (if needed), and the property name. URI is required.
The override attribute specifies the value to assign to the property
during installation. Override is optional. If override is not set, the
configurable property value at install defaults to the value in the flow.
If override is set (even to ""), the value of the configurable
property at install is set to the value of override.
</xsd:documentation></xsd:annotation>
<xsd:complexType>
<xsd:attribute ref="uri" use="required"/>
<xsd:attribute ref="override" use="optional"/>
</xsd:complexType>
</xsd:element>
<xsd:attribute name="uri" type="xsd:string">
<xsd:annotation><xsd:documentation>
The following specifies the syntax for uri attribute values in
the broker.xml ConfigurableProperty elements.
URI = (schemaName.)flowName#(nodeName.)propertyName,
where:
"schemaName" is the name of the Broker schema
the msgflow was defined in, or nothing if the msgflow is defined
in the unnamed schema. schemaName is a dot separated list of
path segments.
"flowName" is the name of the msgflow, such as the
name of the file without the .msgflow or .msgnode extension.
"nodeName" is the label of the node, if the property is
defined on a node. The nodeName parameter is optional, and does
not appear if the property is defined on a flow.
"propertyName" is the name of the property, or the label
key if the node is a primitive node. The descriptor tab of the BAR
editor displays the NL enabled name if the label key matches a
NL enabled name, else it displays the key itself.
</xsd:documentation></xsd:annotation>
</xsd:attribute>
<xsd:attribute name="override" type="xsd:string">
<xsd:annotation><xsd:documentation>
Override is an optional attribute. If the attribute is absent,
the default value set in the msgflow tool will be applied at
install time. If a value is provided for override, then that
value will be applied at install time. If override="",
then the value at install time is the empty string.
</xsd:documentation></xsd:annotation>
</xsd:attribute>
</xsd:schema>
|

 |

|
应用程序总体结构
基于 BAR 文件的内容以及给出的内容描述符,WebSphere Message Broker 的脚本化的部署的总体体系结构如图 2 所示。
图 2. 应用程序总体结构
如图 2 中看到的,BAR 脚本应用程序依靠原始的 BAR 文件和参数文件来生成结果 BAR 文件。该程序首先读取配置参数并创建包含连接了参数的 URI 的参数值的重载表。然后,BAR 脚本化程序读取原始的 BAR 文件,并从中得到 broker.xml 文件。对于原始的 broker.xml 文件中的每个可配置属性条目,URI 的值被提取并与重载表中的值进行核对。如果带有已知 URI 的参数存在于重载表中,那么就将表中的值分配给该参数。否则,将使用原始配置文件中的值。创建的由 URI 和值组成的元组存储在新的配置文件中。最后,将来自于原始 BAR 文件的剩余部分拷贝到结果 BAR 文件中。图 3 中显示了 BAR 脚本应用程序的处理流程。
图 3. BAR 文件处理算法
配置参数
可以利用配置参数来配置位于单独配置文件中的 BAR 文件。图 4 显示了配置文件实例。
图 4. 配置参数的布局
broker.bar.0.flowName=mmsmscenario21
broker.bar.0.propertyName=additionalInstances
broker.bar.0.overrideValue=2
broker.bar.1.schemaName=
broker.bar.1.flowName=mmsmscenario21
broker.bar.1.nodeName=Mapping
broker.bar.1.propertyName=dataSource
broker.bar.1.overrideValue=abcd
broker.bar.2.schemaName=
broker.bar.2.flowName=mmsmscenario21
broker.bar.2.nodeName=insert Flow Parameters
broker.bar.2.propertyName=environment
broker.bar.2.overrideValue=local
broker.bar.3.flowName=mmsmscenario21
broker.bar.3.nodeName=insert Flow Parameters
broker.bar.3.propertyName=value
broker.bar.3.overrideValue=<a><d>test1</d><c>test2</c></a>
|
如图 4 中见到的,每个 BAR 文件的可配置属性由一组配置参数定义而成。每组通过索引进行识别并且包含多达五个属性。这些属性都符合 URI 的定义并且重载部署描述符模式中定义的属性 (图 1):
-
broker.bar.(index).schemaName: 定义代理模式的可选属性。如果省略该属性或将其定义为空串,那么模式将不能用来构造参数的 URI。
-
broker.bar.(index).flowName: 定义参数所属流的必要属性。如果省略该属性,那么将忽略参数文件的余下部分。
-
broker.bar.(index).nodeName: 定义现有参数所属节点的可选属性。如果省略该属性或将其定义为空串,那么模式将不能用来构造参数的 URI。
-
broker.bar.(index).propertyName:该必要属性定义了属性名。如果省略该属性,那么该组属性将被忽略。
-
broker.bar.(index).overrideValue:定义参数值的可选属性。如果省略该属性,那么所有对应该索引的属性将被忽略。如果定义了该属性,但未提供属性值,那么属性值将被空串替换。
配置参数文件中的属性应具有从 0 开始的连续索引值。如果省略了其中一个索引,那么将不能处理更大的索引值。
实现
图 5 显示了 BAR 脚本应用程序的类图。
图 5. BAR 脚本类图
整个实现包括五个类:
- BarScripter:协调整个执行的主类。
- Override properties:定义为配置参数,用作属性的容器类。
- ZipReader:读取原始 BAR 文件并创建结果文件的类。
- DescriptorProcessor:修改部署描述符的类。
- Errors:支持类(XML 分析器的错误容器类)。
下面描述了每个类的具体实现。
BarScripter 类
该类协调整体运行。它读取定义定义对象的命令参数:
- 原始 BAR 文件的全限定名。
- 结果 BAR 文件的全限定名。
- 配置参数文件的全限定名。
如果参数少于 3 个,那么将调用 printUsage 方法并退出应用程序。否则,将生成 OverrideProperties 和 ZipReader 类。将调用处理 ZipReader 类的方法(处理所有的工作)。
OverrideProperties 类
该类负责分析配置参数文件并控制运行过程中的重载属性。它有两个方法,构造函数和 GetParameter 方法。构造函数读取配置参数文件作为属性并将这些属性转换成 URI/值对,这些值对是由部署描述符模式定义的(图 1) 。这些元组存储在内部哈希表中,该哈希表是由 URI 作为键码。构造函数方法使用标准化的方法来确保重载值中不包含 XML 解析器所使用的符号。GetParameter 方法获取基于 URI 值的重载参数的值。如果内部哈希表中没有 URI,将返回空值。
ZipReader 类
由于 BAR 文件本质上是压缩文件,所以将利用 java.util.zip.* 包来处理它。类中的主要方法就是打开原始的 BAR 文件并循环执行该文件内容的处理方法(图 3)。对于 BAR 文件中的每个文件,该方法获取文件名并建立一个流读取文件内容。如果当前的文件是部署描述符,那么通过 descriptor processor 类对其进行修改,并将其写到结果 BAR 文件中。或者将文件直接拷贝到输出 BAR 文件中。类中的另外两个方法,writeByteEntry 和 writeStreamEntry,将表示成流或字节数组的文件存储到结果 BAR 文件中。
DescriptorProcessor 类
部署描述符是由模式定义的 xml 文件(在图 1 中定义)。DescriptorProcessor 类中的 ProcessContent 方法 (图 3) 获得描述符文件的流并利用内部的 getDocument 方法将其转换成 DOM 树。然后遍历原始的文档树并复制到输出文档树中。对于每个原始文档中的 URI 属性,都考虑是否将 OverrideProperties 设为重载值。如果找到了该值,那么将其存储在结果的文档中,替换原始的值。图 6 显示了写入标准输出的运行结果。可以利用这些输出作为日志来验证结果参数。
图 6. BAR 脚本程序运行结果
Converting bar file C:/IBM/WSAD/WBI/barProcessor/M220.BAR.bar
Into bar file C:/IBM/WSAD/WBI/barProcessor/broker.bar
Using property file C:/IBM/WSAD/WBI/barProcessor/brokerbar.properties
Configured parameters are ....
Flow: mmsmscenario21
uri: mmsmscenario21#additionalInstances value:2
uri: mmsmscenario21#commitCount value:null
uri: mmsmscenario21#commitInterval value:null
uri: mmsmscenario21#coordinatedTransaction value:null
uri: mmsmscenario21#MQInput.queueName value:null
uri: mmsmscenario21#Compute1.dataSource value:null
uri: mmsmscenario21#MQOutput.replyToQ value:null
uri: mmsmscenario21#WSDLTODestination.timeToLive value:null
uri: mmsmscenario21#WSDLTODestination.startURL value:null
uri: mmsmscenario21#CandleMonitor.activateNode value:null
uri: mmsmscenario21#Mapping.dataSource value:abcd
uri: mmsmscenario21#WSDLTODestination.nodeTraceSetting value:null
uri: mmsmscenario21#Compute.dataSource value:null
uri: mmsmscenario21#MQOutput.queueName value:null
uri: mmsmscenario21#insert Flow Parameters.path value:blah/blah1
uri: mmsmscenario21#MQOutput.queueManagerName value:null
uri: mmsmscenario21#Compute2.dataSource value:null
uri: mmsmscenario21#MQInput.topicProperty value:null
uri: mmsmscenario21#insert Flow Parameters.environment value:local
uri: mmsmscenario21#insert Flow Parameters.nodeTraceSetting value:null
uri: mmsmscenario21#insert Flow Parameters.value value:<a><d>test1</d><c>test2</c></a>
uri: mmsmscenario21#JMSOutput2.queueName value:null
uri: mmsmscenario21#MQOutput.replyToQMgr value:null
|
将新建的描述符文档转换成 ByteArrayOutputStream,并返回到 ZipReader 类中。
Errors 类
errors 类实现了 org.xml.sax.ErrorHandler 接口。它必须支持由 DescriptorProcessor 类使用的 Xerces XML 分析器来分析原始的描述信息。
您可以下载 barscripter.zip 文件以获取 Eclipse 项目。该文件中包含了本文描述的代码、示例属性文件、brokerbar.properties 文件和若干用作测试的 BAR 文件(M220.BAR.bar、 M660.bar 和 xsltBar.bar)。
结束语
一种新的部署结构的引入,即 WebSphere Message Broker V5 中的 BAR 文件,使您能封装 WebSphere Message Broker 实现过程中的相关构件(消息集、流等等) 。这个新的部署结构,以及依据部署环境具体化参数的能力,极大地简化了 WebSphere Message Broker 代码的维护和提升。BAR 脚本代码使您能自动操作并修改 BAR 参数,从而减少人工参与及错误的总量。通过部署脚本可以保证一致性和操作质量的提升。
致谢
作者感谢 CNA 的同事,特别是 Alagiriswamy Venkiduswamy 和 James Bouquet,帮助作者将文中用到的代码整理出来。
下载 | 描述 | 名字 | 大小 | 下载方法 |
|---|
| Sample WebSphere Message Broker BAR 文件 | barscripter.zip | 33KB |
FTP | HTTP |
|---|
参考资料
关于作者  | 
|  |
Boris Lublinsky 是 CNA Insurance 的一位企业构架师。在 CNA Insuranc,他参与了 CNA 整合策略的设计和实现、应用程序框架的建立以及面向服务体系结构的实现。在这之前,他是一名 Inventa Technologies 的技术指导。他曾在那里监督并参与 EAI 和 B2B 整合的实现工作以及大规模 Web 应用程序开发的工作。他还曾在 Platinum Technology 和 SSA 中担任过技术构架师。在那里他参与了基于组件的系统的设计与开发。Boris 在软件工程和技术体系结构方面具有 20 多年的经验。 |
对本文的评价
|