private void SubmitApplication(LoanApplicationData loanApp) { LoanApplicationProcess loanRequestActivity = new LoanApplicationProcess(loanApp.ApplicationID) { ApplicantID = loanApp.Ssn, LoanApplicationSubmitted = DateTime.UtcNow }; // Ensure the availability of required extensions, throw an exception if not available. Global.WebRoleSingleton.Extensions.Demand <IActivityTrackingEventStreamExtension>(); Global.WebRoleSingleton.Extensions.Demand <IRulesEngineServiceClientExtension>(); IActivityTrackingEventStreamExtension trackingEventStream = Global.WebRoleSingleton.Extensions.Find <IActivityTrackingEventStreamExtension>(); IRulesEngineServiceClientExtension ruleEngineClient = Global.WebRoleSingleton.Extensions.Find <IRulesEngineServiceClientExtension>(); IRoleConfigurationSettingsExtension roleConfig = Global.WebRoleSingleton.Extensions.Find <IRoleConfigurationSettingsExtension>(); // Tells the event stream to invoke the BeginActivity operation. trackingEventStream.BeginActivity(loanRequestActivity); // Call BRE rule to obtain a pre-approval. SimpleValueFact result = ruleEngineClient.ExecutePolicy <SimpleValueFact>("Contoso.Cloud.Integration.Demo.LoanPreapproval", loanApp); // Update the LoanPreapprovalReceived milestone. loanRequestActivity.LoanPreapprovalDecision = Convert.ToString(result.Value); loanRequestActivity.LoanPreapprovalReceived = DateTime.UtcNow; // Display pre-approval decision. SetResultDisplay(loanRequestActivity.LoanPreapprovalDecision); // Tells the event stream to invoke the UpdateActivity. trackingEventStream.UpdateActivity(loanRequestActivity); // Submit pre-approved loan for final processing. if (String.Compare(loanRequestActivity.LoanPreapprovalDecision, "Preapproved", true) == 0) { // Submit the loan application for processing (this will update LoanApplicationStarted, LoanFinalDecision and LoanFinalDecisionMade milestones. using (var loanProcessor = new ReliableServiceBusClient <ILoanApplicationProcessingServiceChannel>(roleConfig.GetServiceBusEndpoint("LoanApplication"), roleConfig.CommunicationRetryPolicy)) { loanProcessor.RetryPolicy.ExecuteAction(() => { loanProcessor.Client.Submit(loanApp); }); } } // Update the LoanFinalDecisionReceived milestone. loanRequestActivity.LoanFinalDecisionReceived = DateTime.UtcNow; // Tells the event stream to invoke the UpdateActivity and EndActivity operations. trackingEventStream.CompleteActivity(loanRequestActivity); }
private void HandlePersistenceQueueItem(PersistenceQueueItemInfo itemInfo) { Guard.ArgumentNotNull(itemInfo, "itemInfo"); this.owner.Extensions.Demand <IRulesEngineServiceClientExtension>(); this.owner.Extensions.Demand <IWorkItemSchedulerConfigurationExtension>(); this.owner.Extensions.Demand <IRoleConfigurationSettingsExtension>(); this.owner.Extensions.Demand <ICloudStorageProviderExtension>(); this.owner.Extensions.Demand <IInterRoleCommunicationExtension>(); IRulesEngineServiceClientExtension rulesEngineExtension = this.owner.Extensions.Find <IRulesEngineServiceClientExtension>(); IWorkItemSchedulerConfigurationExtension configSettingsExtension = this.owner.Extensions.Find <IWorkItemSchedulerConfigurationExtension>(); IRoleConfigurationSettingsExtension roleConfigExtension = this.owner.Extensions.Find <IRoleConfigurationSettingsExtension>(); ICloudStorageProviderExtension storageProviderExtension = this.owner.Extensions.Find <ICloudStorageProviderExtension>(); IActivityTrackingEventStreamExtension trackingEventStreamExtension = this.owner.Extensions.Find <IActivityTrackingEventStreamExtension>(); IInterRoleCommunicationExtension interCommExtension = this.owner.Extensions.Find <IInterRoleCommunicationExtension>(); // Update BAM activity to indicate when we started the dequeue operation. if (trackingEventStreamExtension != null) { InventoryDataTrackingActivity activity = new InventoryDataTrackingActivity(itemInfo.QueueItemId.ToString()) { DequeueOperationStarted = DateTime.UtcNow }; trackingEventStreamExtension.UpdateActivity(activity); } var xPathLib = roleConfigExtension.GetSection <XPathQueryLibrary>(XPathQueryLibrary.SectionName); var batchInfo = rulesEngineExtension.ExecutePolicy <PersistenceQueueItemBatchInfo>(configSettingsExtension.Settings.HandlingPolicyName, itemInfo, new MessageTypeFact(itemInfo.QueueItemType)); // Verify the batch metadata to ensure we have everything we need to be able to perform de-batching. ValidateBatchMetadata(itemInfo, batchInfo); // Replace the XPath query references with actual expressions taken from the XPath Library. batchInfo.BodyItemXPath = xPathLib.Queries.Contains(batchInfo.BodyItemXPath) ? xPathLib.GetXPathQuery(batchInfo.BodyItemXPath) : batchInfo.BodyItemXPath; batchInfo.BodyItemCountXPath = xPathLib.Queries.Contains(batchInfo.BodyItemCountXPath) ? xPathLib.GetXPathQuery(batchInfo.BodyItemCountXPath) : batchInfo.BodyItemCountXPath; var headerXPathList = from item in batchInfo.HeaderSegments where xPathLib.Queries.Contains(item) select new { Segment = item, XPath = xPathLib.GetXPathQuery(item) }; var bodyXPathList = from item in batchInfo.BodySegments where xPathLib.Queries.Contains(item) select new { Segment = item, XPath = xPathLib.GetXPathQuery(item) }; var footerXPathList = from item in batchInfo.FooterSegments where xPathLib.Queries.Contains(item) select new { Segment = item, XPath = xPathLib.GetXPathQuery(item) }; foreach (var item in headerXPathList.ToList()) { batchInfo.HeaderSegments.Remove(item.Segment); batchInfo.HeaderSegments.Add(item.XPath); } foreach (var item in bodyXPathList.ToList()) { batchInfo.BodySegments.Remove(item.Segment); batchInfo.BodySegments.Add(item.XPath); } foreach (var item in footerXPathList.ToList()) { batchInfo.FooterSegments.Remove(item.Segment); batchInfo.FooterSegments.Add(item.XPath); } int fromItem = 1, toItem = fromItem, maxItems = configSettingsExtension.Settings.XmlBatchSize; var taskParameters = new List <DequeueXmlDataTaskState>(); using (SqlAzurePersistenceQueue persistenceQueue = new SqlAzurePersistenceQueue()) { persistenceQueue.Open(WellKnownDatabaseName.PersistenceQueue); using (XmlReader resultReader = persistenceQueue.QueryXmlData(itemInfo.QueueItemId, new string[] { batchInfo.BodyItemCountXPath }, xPathLib.Namespaces.NamespaceManager)) { maxItems = resultReader.ReadContentAsInt(); } } do { toItem = fromItem + configSettingsExtension.Settings.XmlBatchSize - 1; taskParameters.Add(new DequeueXmlDataTaskState() { QueueItemInfo = itemInfo, HeaderSegments = new List <string>(batchInfo.HeaderSegments), BodySegments = new List <string>(from query in batchInfo.BodySegments select String.Format(query, fromItem, toItem)), FooterSegments = new List <string>(batchInfo.FooterSegments), StartItemIndex = fromItem, EndItemIndex = toItem, ItemDetectionXPath = batchInfo.BodyItemXPath, Settings = configSettingsExtension.Settings, StorageProvider = storageProviderExtension, NamespaceManager = xPathLib.Namespaces.NamespaceManager }); fromItem += configSettingsExtension.Settings.XmlBatchSize; }while (toItem < maxItems); // Before we start putting work items on queue, notify the respective queue listeners that they should expect work to arrive. CloudQueueWorkDetectedTriggerEvent trigger = new CloudQueueWorkDetectedTriggerEvent(configSettingsExtension.Settings.CloudStorageAccount, configSettingsExtension.Settings.DestinationQueue, maxItems, PayloadSizeKind.MessageCount); // Package the trigger into an inter-role communication event. var interRoleEvent = new InterRoleCommunicationEvent(trigger); // Publish inter-role communication event via the Service Bus one-way multicast. interCommExtension.Publish(interRoleEvent); var stateCollection = from x in taskParameters.AsParallel <DequeueXmlDataTaskState>() orderby x.StartItemIndex select x; foreach (var state in stateCollection) { try { DequeueXmlDataTaskMain(state); } catch (Exception ex) { TraceManager.WorkerRoleComponent.TraceError(ex); } } // Update BAM activity to indicate when we completed the dequeue operation. if (trackingEventStreamExtension != null) { InventoryDataTrackingActivity activity = new InventoryDataTrackingActivity(itemInfo.QueueItemId.ToString()) { DequeueOperationCompleted = DateTime.UtcNow }; trackingEventStreamExtension.UpdateActivity(activity); } }