Friday, May 23, 2014

BizTalk Atomic Update for Side-by-Side Orchestration Deployment

Deploying orchestration side-by-side, you have to unenlist the old version and start the new version. To minimize the down time, the process should be done in atomic way so that the old orchestration would not pick the new messages while the new orchestration instances will be activated by the new messages.

BizTalk exposes the .Net API to manage the orchestrations. The following script demonstrates how to use PowerShell to invoke the API so that unenlisting and starting the orchestrations could be done atomically.

To use the script, update $connectionString and the expressions in $unenlisting and $starting variables so that they could match the assembly qualified names of the orchestrations. The script also takes one argument "test" or "trial" so that you could test out whether the expressions are correct.
  1. ### Settings of the script ###  
  2. $test = $FALSE  
  3. If ($args.Count -gt 0) {  
  4.     $test = ($args[0] -eq "test") -or ($args[0] -eq "trial")  
  5. }  
  6. $connectionString = "SERVER=.;DATABASE=BizTalkMgmtDb;Integrated Security=SSPI"  
  7. $unenlisting = `  
  8.     @( `  
  9.       '^Test.*' `  
  10.     )  
  11. $starting = `  
  12.     @( `  
  13.       '.*SubOrchestration.*' `  
  14.     )  
  15.   
  16. ### Initialize BizTalk catalog explorer ###  
  17. add-type -assemblyName "Microsoft.BizTalk.ExplorerOM, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"  
  18. $catalog = New-Object -TypeName Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer  
  19. $catalog.ConnectionString = $connectionString  
  20.   
  21. ### Matching orchestration's assembly qualified name with the regular expressions in $unenlisting and $starting array ###  
  22. If ($test) {  
  23.     Write-Host "TEST MODE" -foregroundcolor white  
  24.     Write-Host  
  25. }  
  26. Try  
  27. {  
  28.     $catalog.Applications | ForEach-Object {  
  29.         $_.Orchestrations | ForEach-Object {  
  30.             $target = $_  
  31. ### -like is the wildcard operator  
  32. ### -match is the regular expression operator  
  33.   
  34. #            $unenlisting | Where-Object {($target.AssemblyQualifiedName -like $_) -and ($target.Status -ne [Microsoft.BizTalk.ExplorerOM.OrchestrationStatus]::Unenlisted)} | ForEach-Object {  
  35.             $unenlisting | Where-Object {($target.AssemblyQualifiedName -match $_) -and ($target.Status -ne [Microsoft.BizTalk.ExplorerOM.OrchestrationStatus]::Unenlisted)} | ForEach-Object {  
  36.                 Write-Host "Unenlisting" $target.FullName $target.BtsAssembly.Version "..." -foregroundcolor yellow  
  37.                 $target.Status = [Microsoft.BizTalk.ExplorerOM.OrchestrationStatus]::Unenlisted  
  38.             }  
  39. #            $starting | Where-Object {($target.AssemblyQualifiedName -like $_) -and ($target.Status -ne [Microsoft.BizTalk.ExplorerOM.OrchestrationStatus]::Started)} | ForEach-Object {  
  40.             $starting | Where-Object {($target.AssemblyQualifiedName -match $_) -and ($target.Status -ne [Microsoft.BizTalk.ExplorerOM.OrchestrationStatus]::Started)} | ForEach-Object {  
  41.                 Write-Host "Starting   " $target.FullName $target.BtsAssembly.Version "..." -foregroundcolor green  
  42.                 $target.Status = [Microsoft.BizTalk.ExplorerOM.OrchestrationStatus]::Started  
  43.             }  
  44.         }  
  45.     }  
  46.     Write-Host  
  47.     If ($test) {  
  48.         $catalog.DiscardChanges()  
  49.         Write-Host "Changes rolled-back for test mode" -foregroundcolor white  
  50.     }  
  51.     Else {  
  52.         $catalog.SaveChanges()  
  53.         Write-Host "Changes committed" -foregroundcolor white  
  54.     }  
  55. }  
  56. Catch {  
  57.     $catalog.DiscardChanges()  
  58.     Write-Host "Changes rolled-back" -foregroundcolor red  
  59.     Write-Host $Error cyan  
  60. }  
  61. Write-Host   

No comments: