使用 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);
另請參閱
使用事件數據流實作 BAM 活動
活動接續
BAM 動態基礎結構
BAM API (BizTalk Server 範例)