コード例 #1
0
        /// <summary>
        /// Begins the BAM activity.
        /// </summary>
        public void BeginMessagingStepActivity()
        {
            // Begin the Activity using the passed identifier
            EventStream es = _eventStream;

            es.BeginActivity(ActivityName, _activityId);
        }
        /// <summary>
        /// implements the IRelatedBAMActivitiesTracker Execute method
        /// </summary>
        /// <param name="context">pipeline context</param>
        /// <param name="msg">IBaseMessage</param>
        /// <param name="eventStream">the eventstream for writing BAM data</param>
        /// <param name="PrimaryActivityName">the primary BAM activity name</param>
        /// <param name="PrimaryActivityID">the primary BAM activity ID</param>
        /// <param name="RelatedActivityName">the related BAM activity name</param>
        /// <param name="BAMPropertyEventSources">a list of BAM activity event source</param>
        /// <param name="BAMPropertyEventSourceValues">a list of BAM activity event source values</param>
        /// <param name="PostprocessXPathEventSources">a list of xpath for post-processing</param>
        /// <param name="EnableContinuation">if related activity should be enabled</param>
        /// <param name="UseContinuation">if related activity should use continuation</param>
        public void Execute(IPipelineContext context
                            , IBaseMessage msg
                            , ref EventStream eventStream
                            , string PrimaryActivityName
                            , string PrimaryActivityID
                            , string RelatedActivityName
                            , List <BAMPropertyEventSource> BAMPropertyEventSources
                            , Dictionary <string, object> BAMPropertyEventSourceValues
                            , List <BAMPropertyEventSource> PostprocessXPathEventSources
                            , bool EnableContinuation
                            , bool UseContinuation
                            )
        {
            var callToken = TraceProvider.Logger.TraceIn(this.GetType().FullName);

            try
            {
                if (BAMPropertyEventSources != null && BAMPropertyEventSources.Count > 0)
                {
                    if (eventStream != null)
                    {
                        List <object> eventData = new List <object>();

                        var relatedXpathEventSources = from propertySource in BAMPropertyEventSources
                                                       where propertySource.Source.Equals(BAMPropertyEventSourceSource.XPath)
                                                       select propertySource;

                        if (relatedXpathEventSources.Count() == 1 &&
                            relatedXpathEventSources.First().ActivityItem.Equals(BAMTrackerFact.RELATED_ACTIVITY_XPATH_ROOT_SOURCE) &&
                            BAMPropertyEventSourceValues.ContainsKey(BAMTrackerFact.RELATED_ACTIVITY_XPATH_ROOT_SOURCE))
                        {
                            #region --------------- handle multiple related activities creation ---------------

                            var activitySource = (XPathNavigator)BAMPropertyEventSourceValues[BAMTrackerFact.RELATED_ACTIVITY_XPATH_ROOT_SOURCE];
                            Dictionary <string, string> xpathQueries = new Dictionary <string, string>();

                            PostprocessXPathEventSources.ToList().ForEach(x => xpathQueries.Add(x.ActivityItem, x.Value));
                            Dictionary <string, object> xpathValues = GetXPathValues(activitySource, xpathQueries);

                            for (int valueIndex = 0; valueIndex < ((List <object>)xpathValues.First().Value).Count; valueIndex++)
                            {
                                var           originalRelatedId = string.Concat(PrimaryActivityID, "/", valueIndex);
                                var           relatedId         = string.Concat(PrimaryActivityID, "/", valueIndex);
                                List <object> relatedEventData  = new List <object>();

                                relatedEventData.Add("PrimaryActivityId");
                                relatedEventData.Add(PrimaryActivityID);

                                foreach (var xpathQuery in xpathQueries)
                                {
                                    var itemValue = xpathValues.ContainsKey(xpathQuery.Key) && ((List <object>)xpathValues[xpathQuery.Key]).Count > 0
                                                        ? ((List <object>)xpathValues[xpathQuery.Key])[valueIndex]
                                                        : null;
                                    if (itemValue != null)
                                    {
                                        relatedEventData.Add(xpathQuery.Key);
                                        relatedEventData.Add(itemValue);
                                    }
                                }

                                if (BAMPropertyEventSources != null && BAMPropertyEventSources.Count() > 0)
                                {
                                    foreach (var eventsource in BAMPropertyEventSources)
                                    {
                                        if (!eventsource.Source.Equals(BAMPropertyEventSourceSource.XPath))
                                        {
                                            if (BAMPropertyEventSourceValues.ContainsKey(eventsource.ActivityItem))
                                            {
                                                relatedEventData.Add(eventsource.ActivityItem);
                                                relatedEventData.Add(BAMPropertyEventSourceValues[eventsource.ActivityItem]);
                                            }
                                        }
                                    }
                                }

                                #region start BAM Activity

                                // if using continuation, open activity with the continuation ID
                                if (UseContinuation)
                                {
                                    relatedId = CONTINUATION_PREFIX + originalRelatedId;
                                }
                                else
                                {
                                    TraceProvider.Logger.TraceInfo("Opening activity [{0}] with ID: {1}...", RelatedActivityName, relatedId);
                                    eventStream.BeginActivity(RelatedActivityName, relatedId);
                                }

                                #endregion start BAM Activity

                                #region update BAM Activity

                                TraceProvider.Logger.TraceInfo("Writing event data to activity [{0}] with ID: {1}...", RelatedActivityName, relatedId);
                                // write BAM data
                                eventStream.UpdateActivity(RelatedActivityName, relatedId, relatedEventData.ToArray());

                                #endregion update BAM Activity

                                #region BAM Activity Continuation

                                // check if we need to enable continuation
                                if (EnableContinuation == true && UseContinuation != true)
                                {
                                    string continuationToken = CONTINUATION_PREFIX + originalRelatedId;
                                    TraceProvider.Logger.TraceInfo("Enabling continuation on activity [{0}] with ID: {1} using continuation token: {2}...", RelatedActivityName, relatedId, continuationToken);
                                    eventStream.EnableContinuation(RelatedActivityName, relatedId, continuationToken);
                                }

                                #endregion BAM Activity Continuation

                                #region end related BAM Activity

                                // end updates to activity
                                if (EnableContinuation == false || UseContinuation == false)
                                {
                                    eventStream.EndActivity(RelatedActivityName, relatedId);
                                }
                                eventStream.Flush();

                                #endregion end related BAM Activity

                                #region add related activity

                                eventStream.AddRelatedActivity(PrimaryActivityName, PrimaryActivityID, RelatedActivityName, relatedId);

                                #endregion add related activity

                                TraceProvider.Logger.TraceInfo("Finished writing to activity [{0}] with ID: {1}...", RelatedActivityName, relatedId);
                            }

                            #endregion --------------- handle multiple related activities creation ---------------
                        }
                        else
                        {
                            #region --------------- handle single related activity creation ---------------

                            var           originalRelatedId = string.Concat(PrimaryActivityID, "/", 0);
                            var           relatedId         = string.Concat(PrimaryActivityID, "/", 0);
                            List <object> relatedEventData  = new List <object>();

                            relatedEventData.Add("PrimaryActivityId");
                            relatedEventData.Add(PrimaryActivityID);

                            if (BAMPropertyEventSources != null && BAMPropertyEventSources.Count() > 0)
                            {
                                foreach (var eventsource in BAMPropertyEventSources)
                                {
                                    var itemValue = BAMPropertyEventSourceValues.ContainsKey(eventsource.ActivityItem)
                                                        ? BAMPropertyEventSourceValues[eventsource.ActivityItem]
                                                        : null;
                                    if (itemValue != null)
                                    {
                                        relatedEventData.Add(eventsource.ActivityItem);
                                        relatedEventData.Add(itemValue);
                                    }
                                }
                            }

                            #region start BAM Activity

                            // if using continuation, open activity with the continuation ID
                            if (UseContinuation)
                            {
                                relatedId = CONTINUATION_PREFIX + originalRelatedId;
                            }
                            else
                            {
                                TraceProvider.Logger.TraceInfo("Opening activity [{0}] with ID: {1}...", RelatedActivityName, relatedId);
                                eventStream.BeginActivity(RelatedActivityName, relatedId);
                            }

                            #endregion start BAM Activity

                            #region update BAM Activity

                            TraceProvider.Logger.TraceInfo("Writing event data to activity [{0}] with ID: {1}...", RelatedActivityName, relatedId);
                            // write BAM data
                            eventStream.UpdateActivity(RelatedActivityName, relatedId, relatedEventData.ToArray());

                            #endregion update BAM Activity

                            #region BAM Activity Continuation

                            // check if we need to enable continuation
                            if (EnableContinuation == true && UseContinuation != true)
                            {
                                string continuationToken = CONTINUATION_PREFIX + originalRelatedId;
                                TraceProvider.Logger.TraceInfo("Enabling continuation on activity [{0}] with ID: {1} using continuation token: {2}...", RelatedActivityName, relatedId, continuationToken);
                                eventStream.EnableContinuation(RelatedActivityName, relatedId, continuationToken);
                            }

                            #endregion BAM Activity Continuation

                            #region end related BAM Activity

                            // end updates to activity
                            if (EnableContinuation == false || UseContinuation == false)
                            {
                                eventStream.EndActivity(RelatedActivityName, relatedId);
                            }
                            eventStream.Flush();

                            #endregion end related BAM Activity

                            #region add related activity

                            eventStream.AddRelatedActivity(PrimaryActivityName, PrimaryActivityID, RelatedActivityName, relatedId);

                            #endregion add related activity

                            TraceProvider.Logger.TraceInfo("Finished writing to activity [{0}] with ID: {1}...", RelatedActivityName, relatedId);

                            #endregion --------------- handle single related activity creation ---------------
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                // put component name as a source information in this exception,
                // so the event log in message could reflect this
                ex.Source = this.GetType().FullName;
                TraceProvider.Logger.TraceError(ex);
                throw ex;
            }
            finally
            {
                TraceProvider.Logger.TraceOut(callToken, this.GetType().FullName);
            }
        }
コード例 #3
0
        /// <summary>
        /// Performs the BAM activity tracking using the current message context and instance.
        /// </summary>
        /// <param name="pipelineContext">Pipeline context</param>
        /// <param name="inputMessage">Input message</param>
        /// <returns>Original input message</returns>
        protected override IBaseMessage Execute(IPipelineContext pipelineContext, IBaseMessage inputMessage)
        {
            // Note: this component, as a general rule, first attemps to read a property from the context and if
            // it cannot be found there, it uses the design property value
            string activityName = inputMessage.Context.Read <string>(BAMTrackerProperties.ActivityName.Name, BAMTrackerProperties.ActivityName.Namespace, this.ActivityName);
            List <BAMEventSource> eventSources = inputMessage.Context.Read <List <BAMEventSource> >(BAMTrackerProperties.EnableContinuation.Name, BAMTrackerProperties.EnableContinuation.Namespace, this.EventSources);

            if (eventSources != null && eventSources.Count > 0)
            {
                TraceProvider.Logger.TraceInfo("Retrieving Messaging Event Stream from pipeline context...");
                // get a messaging BAM event stream instance that participates in the pipeline transaction context
                EventStream eventStream = pipelineContext.GetEventStream();
                if (eventStream != null)
                {
                    // set default BAM activity ID
                    string activityID = Guid.NewGuid().ToString();

                    // read message InterchangeID to use it as the continuation token
                    TraceProvider.Logger.TraceInfo("Attempting to read InterchangeID from context...");
                    // attempt to read the activity ID from the context
                    string interchangeID = inputMessage.Context.Read <string>(BtsProperties.InterchangeID.Name, BtsProperties.InterchangeID.Namespace, null);
                    if (String.IsNullOrEmpty(interchangeID) == true)
                    {
                        throw new InvalidOperationException("InterchangeID not found in context.");
                    }

                    // if using continuation, open activity with the continuation ID
                    bool useContinuation = inputMessage.Context.Read <bool>(BAMTrackerProperties.UseContinuation.Name, BAMTrackerProperties.UseContinuation.Namespace, this.UseContinuation);
                    if (useContinuation == true)
                    {
                        activityID = CONTINUATION_PREFIX + interchangeID;
                    }

                    // open activity
                    TraceProvider.Logger.TraceInfo("Opening activity {0} with ID: {1}...", activityName, activityID);
                    eventStream.BeginActivity(activityName, activityID);

                    // check if we need to enable continuation
                    bool enableContinuation = inputMessage.Context.Read <bool>(BAMTrackerProperties.EnableContinuation.Name, BAMTrackerProperties.EnableContinuation.Namespace, this.EnableContinuation);
                    if (this.EnableContinuation == true)
                    {
                        string continuationToken = CONTINUATION_PREFIX + interchangeID;
                        TraceProvider.Logger.TraceInfo("Enabling continuation on activity {0} with ID: {1} using continuation token: {2}...", activityName, activityID, continuationToken);
                        eventStream.EnableContinuation(activityName, activityID, continuationToken);
                    }

                    TraceProvider.Logger.TraceInfo("Writing event data to activity {0} with ID: {1}...", activityName, activityID);
                    // write BAM data
                    List <object> eventData = new List <object>();
                    foreach (BAMEventSource eventSource in eventSources)
                    {
                        // add activity item key (this the BAM activity item name in the BAM definition)
                        eventData.Add(eventSource.ActivityItem);
                        // read from the context the corresponding value
                        object itemValue = inputMessage.Context.Read(eventSource.ContextPropertyName, eventSource.ContextPropertyNamespace);
                        eventData.Add(itemValue);
                    }

                    // update activity
                    eventStream.UpdateActivity(activityName, activityID, eventData.ToArray());
                    // end updates to activity
                    eventStream.EndActivity(activityName, activityID);
                    TraceProvider.Logger.TraceInfo("Finished writing to activity {0} with ID: {1}...", activityName, activityID);
                }
            }

            return(inputMessage);
        }