使用 BAM 的最简单方法是使用 BAM API 发送显式里程碑或数据。 可以将 BAM 活动视为 SQL 表中与实际工作单元保持同步的记录。
为每个新的工作单元调用
BeginActivity
。当工作完成且预计不会再发生任何事件时,调用
EndActivity
。在实现的关键位置调用 Microsoft.BizTalk.Bam.EventObservation.EventStream.UpdateActivity ,以发送对信息工作者有用的数据和里程碑。
重要
在释放之前,必须清除事件流。 释放后,EventStream 对象不会自动刷新数据。 这意味着,通常在处理完活动后刷新流的代码通常会在调用刷新之前发生异常时导致数据丢失。
为避免数据丢失,建议将处理和刷新封装在 try/finally 块中,如下面的伪代码所示:
BufferedEventStream es = new BufferedEventStream(…)
Try
{
// Do the activity processing here.
}
Finally
{
if (es != null ) es.Flush();
}
以下示例代码演示如何在工作单位为采购订单时使用 BeginActivity、UpdateActivity 和 EndActivity。 在此示例中,我们假设字符串变量 poid 标识当前正在处理的采购订单:
using Microsoft.BizTalk.BAM.EventObservation;
...
EventStream es=new DirectEventStream(connectionString,1);
...
// At the beginning of the processing of a new work:
// Here poid is a string variable that has the current
// Purchase Order identifier (e.g. PO number)
es.BeginActivity("PurchaseOrder",poid);
es.UpdateActivity("PurchaseOrder",poid,
"POReceived",DateTime.UtcNow,
"POAmount",100,
"CustomerName",""Joe",
"CustomerCity","Seattle");
...
// few days later
es.UpdateActivity("PurchaseOrder",poid,
"POApproved",DateTime.UtcNow,
"ProductName","Widget");
...
// and another few days later
es. UpdateActivity("PurchaseOrder",poid,
"POShipped",DateTime.UtcNow);
...
// and finally
es. UpdateActivity("PurchaseOrder",poid,
"Delivered",DateTime.UtcNow);
es.EndActivity("PurchaseOrder",poid);