WebSphere WebSphere Enterprise Service Bus V6.0.1 操作系统: AIX, HP-UX, Linux, Solaris, Windows

创建和填充事件

获取事件工厂后,可创建事件对象并为其填充属性数据。

执行此任务的目的和时间

公共基本事件规范将多数事件属性定义为可选,但以下属性是必需的:
注: version 属性是由公共基本事件规范作为可选项定义的,但是,如果未指定该项,将采用缺省值 1.0。因为 Common Event Infrastructure 仅支持规范版本 1.0.1,所以必须指定此值。

如果您尝试发送的事件缺少这些属性中的任何一个,发射器将拒绝此事件并抛出 EventsException 异常。

以下代码段创建事件并对其填充所需的最少属性数据:

CommonBaseEvent event = eventFactory.createCommonBaseEvent();

event.setVersion("1.0.1");                     // set version

long currentTime = System.currentTimeMillis(); // get current time
event.setCreationTimeAsLong(currentTime);      // and set creationTime

// set sourceComponentId (a complex type) 
event.setSourceComponentId("Windows",          // application
                           "svchost.exe",      // component
                           "tlntsvr.exe",      // subcomponent
                           "http://www.ibm.com/namespaces/autonomic/Windows",
                                               // componentType
                           "win386_svc",       // componentIdType
                           "9.45.72.138",      // location
                           "IPV4"              // locationType
                           );

// create situation object
Situation situation = eventFactory.createSituation();

// set situationType to AvailableSituation (a complex type)
situation.setAvailableSituation("EXTERNAL",          // reasoningScope
                                "NOT AVAILABLE",     // availabilityDisposition
                                "STARTABLE",         // operationDisposition
                                "FUNCTION_PROCESS"); // processingDisposition

// set situation
event.setSituation(situation);

此示例先使用事件工厂来创建新的事件实例 event。首先,它将设置 version 属性;然后会检索当前系统时间并使用 setCreationTimeAsLong(long) 方法来设置 creationTime 属性的值。或者使用 setCreationTime(String) 方法,它使用 XML dateTime 格式(如 "2004-07-29T13:12:00-05:00")来设置创建时间。

下一个必需属性 sourceComponentId 是一个复合属性,以拥有自己属性的 ComponentIdentification 的实例表示。但是,您不必直接实例化此对象或与它进行交互(尽管可以这么做)。反而示例中的下一个语句会使用辅助控件方法 setSourceComponentId() 来指定嵌套属性;辅助控件方法使用这些值来创建 ComponentIdentification 的实例,随后使用它来设置事件的 sourceComponentId 属性的值。

存在用于设置其他复杂属性的类似辅助控件方法(如 setMsgDataElement()、addAssociatedEvent 和 addExtendedDataElement())。许多此类方法提供带有不同特征符的多种版本,使您能以不同的方式指定属性值。请参阅 Javadoc API 文档以获取这些方法的完整信息。

示例中的最后一个必需属性 Situation 是另一个复杂属性。在此情况下,situation 对象必须直接使用事件工厂实例化;然后示例使用辅助控件方法来设置 situationType 属性,它本身是复杂子元素。

在实际应用程序中,有用的事件包含的信息比此处显示的信息要多,但是,这些信息是公共基本事件规范和 Common Event Infrastructure 的最低要求。现在,事件是有效的并且可以提交给发射器。


任务主题

使用条款 | 评价此页

Timestamp icon上次更新时间: 3 Mar 2006
http://publib.boulder.ibm.com/infocenter/dmndhelp/v6rxmx/index.jsp?topic=/com.ibm.websphere.wesb.doc.nl1_6.0.1\doc\tcei_admin_populatingEvent.html

(C) Copyright IBM Corporation 2005, 2006. All Rights Reserved.
本信息中心基于 Eclipse 技术。(http://www.eclipse.org)