Esempio n. 1
0
        private void DequeueXmlDataTaskMain(DequeueXmlDataTaskState state)
        {
            Guard.ArgumentNotNull(state, "state");

            using (ICloudQueueStorage workItemQueue = state.StorageProvider.GetQueueStorage(state.Settings.CloudStorageAccount))
                using (SqlAzurePersistenceQueue dbQueue = new SqlAzurePersistenceQueue())
                {
                    dbQueue.Open(WellKnownDatabaseName.PersistenceQueue);

                    using (XmlReader xmlReader = dbQueue.DequeueXmlData(state.QueueItemInfo.QueueItemId, state.HeaderSegments, state.BodySegments, state.FooterSegments, state.NamespaceManager))
                    {
                        if (xmlReader != null)
                        {
                            XDocument batch = XDocument.Load(xmlReader);

                            // Check for presence of any items in the batch.
                            bool batchFound = (batch.XPathSelectElement(state.ItemDetectionXPath) != null);

                            if (batchFound)
                            {
                                workItemQueue.Put <XDocument>(state.Settings.DestinationQueue, batch);
                            }
                        }
                    }
                }
        }
Esempio n. 2
0
        public void TestCoreOperations()
        {
            string testFileName = Path.Combine(testMessageFolder, "InventoryReport_0.xml");
            PersistenceQueueItemInfo queueItemInfo = null;

            try
            {
                using (FileStream fileStream = new FileStream(testFileName, FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    using (SqlAzurePersistenceQueue persistenceQueue = new SqlAzurePersistenceQueue())
                    {
                        persistenceQueue.Open(WellKnownDatabaseName.PersistenceQueue);

                        queueItemInfo = persistenceQueue.Enqueue(fileStream);
                    }
                }

                Assert.IsTrue(queueItemInfo.QueueItemSize > 0, "QueueItemSize is zero");
                Assert.IsTrue(!String.IsNullOrEmpty(queueItemInfo.QueueItemType), "QueueItemType is zero");

                XPathQueryLibrary xPathLib       = ApplicationConfiguration.Current.GetConfigurationSection <XPathQueryLibrary>(XPathQueryLibrary.SectionName);
                string            itemCountXPath = xPathLib.GetXPathQuery("InventoryItemCount");

                using (SqlAzurePersistenceQueue persistenceQueue = new SqlAzurePersistenceQueue())
                {
                    persistenceQueue.Open(WellKnownDatabaseName.PersistenceQueue);

                    using (XmlReader resultReader = persistenceQueue.QueryXmlData(queueItemInfo.QueueItemId, new string[] { itemCountXPath }, xPathLib.Namespaces.NamespaceManager))
                    {
                        int itemCount = resultReader.ReadContentAsInt();

                        Assert.IsTrue(itemCount > 0, "Item count is zero");
                        Assert.AreEqual <int>(6, itemCount, "Wrong item count");
                    }
                }
            }
            finally
            {
                if (queueItemInfo != null)
                {
                    using (SqlAzurePersistenceQueue persistenceQueue = new SqlAzurePersistenceQueue())
                    {
                        persistenceQueue.Open(WellKnownDatabaseName.PersistenceQueue);
                        bool removed = persistenceQueue.Remove(queueItemInfo.QueueItemId);

                        Assert.IsTrue(removed, "Item was not removed");
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Persists the specified stream of data into the queue and returns an unique identify that corresponds to the persisted queue item.
        /// </summary>
        /// <param name="data">The stream containing the data to be persisted.</param>
        /// <returns>The unique identity of the queue item.</returns>
        public Guid PersistDataStream(Stream data)
        {
            Guard.ArgumentNotNull(data, "data");

            var callToken = TraceManager.WorkerRoleComponent.TraceIn(data.GetType().FullName);

            PersistenceQueueItemInfo queueItemInfo = null;

            using (SqlAzurePersistenceQueue dbQueue = new SqlAzurePersistenceQueue(StreamingMode.FixedBuffer))
            {
                dbQueue.Open(WellKnownDatabaseName.PersistenceQueue);
                queueItemInfo = dbQueue.Enqueue(data);
            }

            if (PersistDataStreamCompleted != null)
            {
                PersistDataStreamCompleted(queueItemInfo);
            }

            TraceManager.WorkerRoleComponent.TraceOut(callToken, queueItemInfo.QueueItemId);
            return(queueItemInfo.QueueItemId);
        }
Esempio n. 4
0
        public void TestDequeueXmlData()
        {
            XPathQueryLibrary xPathLib = ApplicationConfiguration.Current.XPathQueries;

            var headerSegments = new List <string>();
            var bodySegments   = new List <string>();
            var footerSegments = new List <string>();

            headerSegments.Add(xPathLib.GetXPathQuery("InventoryHeader"));
            footerSegments.Add(xPathLib.GetXPathQuery("InventorySummary"));
            footerSegments.Add(xPathLib.GetXPathQuery("InventoryIRRSegment"));
            string itemXPath = xPathLib.GetXPathQuery("InventoryItems");

            int       fromItem     = 1;
            int       xmlBatchSize = 50;
            XDocument batch        = null;
            Guid      queueItemId  = Guid.Parse("6DA1B27D-714D-465D-93B8-234B085CF6E4");

            using (SqlAzurePersistenceQueue dbQueue = new SqlAzurePersistenceQueue())
            {
                dbQueue.Open(WellKnownDatabaseName.PersistenceQueue);

                int toItem = fromItem + xmlBatchSize - 1;

                bodySegments.Clear();
                bodySegments.Add(String.Format(xPathLib.GetXPathQuery("InventoryItemBatchTemplate"), fromItem, toItem));

                using (XmlReader xmlReader = dbQueue.DequeueXmlData(queueItemId, headerSegments, bodySegments, footerSegments, xPathLib.Namespaces.NamespaceManager))
                {
                    if (xmlReader != null)
                    {
                        batch = XDocument.Load(xmlReader);
                    }
                }
            }
        }
Esempio n. 5
0
        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);
            }
        }