示例#1
0
        private void DequeueProcess
        (
            Action <long, T> onOnceDequeueProcessAction = null
            , int sleepInMilliseconds = 100
            , Action <long, List <Tuple <long, T> > > onBatchDequeuesProcessAction = null
            , int waitOneBatchTimeOutInMilliseconds = 1000
            , int waitOneBatchMaxDequeuedTimes      = 100
            , Func <Exception, Exception, string, bool> onDequeueProcessCaughtExceptionProcessFunc       = null
            , Action <bool, Exception, Exception, string> onDequeueProcessFinallyProcessAction           = null
            , Func <Exception, Exception, string, bool> onDequeuesBatchProcessCaughtExceptionProcessFunc = null
            , Action <bool, Exception, Exception, string> onDequeuesBatchProcessFinallyProcessAction     = null
        )
        {
            if (_isStartedDequeueProcess)
            {
                return;
            }
            List <Tuple <long, T> > list = null;
            long      i         = 0;
            Stopwatch stopwatch = null;



            if (onBatchDequeuesProcessAction != null)
            {
                list      = new List <Tuple <long, T> >();
                stopwatch = new Stopwatch();
                stopwatch.Start();
            }
            while (true)
            {
                TryCatchFinallyProcessHelper
                .TryProcessCatchFinally
                (
                    true
                    , () =>
                {
                    if (!_queue.IsEmpty)
                    {
                        Tuple <Stopwatch, T> element = null;
                        if (_queue.TryDequeue(out element))
                        {
                            var enabledCountPerformance = true;
                            {
                                if (OnGetEnabledCountPerformanceProcessFunc != null)
                                {
                                    enabledCountPerformance = OnGetEnabledCountPerformanceProcessFunc();
                                }
                            }

                            var qpcc = _queuePerformanceCountersContainer;
                            Stopwatch stopwatchDequeue = null;
                            var stopwatchEnqueue       = element.Item1;
                            if (enabledCountPerformance)
                            {
                                stopwatchDequeue = _stopwatchsPool.Get();
                            }

                            _timerCounters[0].Item2 = stopwatchEnqueue;
                            _timerCounters[1].Item2 = stopwatchDequeue;

                            #region while queue.IsEmpty loop
                            var reThrowException = false;
                            PerformanceCountersHelper
                            .TryCountPerformance
                            (
                                () =>
                            {
                                return(enabledCountPerformance);
                            }
                                , reThrowException
                                , qpcc.IncrementCountersBeforeCountPerformanceForDequeue
                                , qpcc.DecrementCountersBeforeCountPerformanceForDequeue
                                , _timerCounters
                                , () =>                                     //try
                            {
                                if (onOnceDequeueProcessAction != null)
                                {
                                    var item = element.Item2;
                                    onOnceDequeueProcessAction
                                        (i, item);
                                }
                            }
                                , (x, y, z) =>                                  //catch
                            {
                                qpcc
                                .CaughtExceptionsPerformanceCounter
                                .Increment();

                                z = "OnDequeueProcessCaughtExceptionProcessFunc\r\n" + z;
                                if (onDequeueProcessCaughtExceptionProcessFunc != null)
                                {
                                    reThrowException = onDequeueProcessCaughtExceptionProcessFunc
                                                       (
                                        x
                                        , y
                                        , z
                                                       );
                                }
                                else if (OnCaughtException != null)
                                {
                                    reThrowException = OnCaughtException(this, x, y, z);
                                }
                                return(reThrowException);
                            }
                                , onDequeueProcessFinallyProcessAction                                      //finally
                                , null
                                , qpcc.IncrementCountersAfterCountPerformanceForDequeue
                            );
                            #endregion while queue.IsEmpty loop

                            //池化

                            if (stopwatchEnqueue != null)
                            {
                                stopwatchEnqueue.Reset();
                                var r = _stopwatchsPool.Put(stopwatchEnqueue);
                                if (!r)
                                {
                                    stopwatchEnqueue.Stop();
                                    stopwatchEnqueue = null;
                                }
                            }
                            if (stopwatchDequeue != null)
                            {
                                stopwatchDequeue.Reset();

                                var r = _stopwatchsPool.Put(stopwatchDequeue);
                                if (!r)
                                {
                                    stopwatchDequeue.Stop();
                                    stopwatchDequeue = null;
                                }
                            }
                            if (onBatchDequeuesProcessAction != null)
                            {
                                i++;
                                var item = element.Item2;
                                var tuple
                                    = Tuple
                                      .Create <long, T>
                                      (
                                          i
                                          , item
                                      );
                                list.Add(tuple);
                            }
                        }
                    }
                    else
                    {
                        if (sleepInMilliseconds > 0)
                        {
                            Thread.Sleep(sleepInMilliseconds);
                        }
                    }
                    if (onBatchDequeuesProcessAction != null)
                    {
                        if
                        (
                            i >= waitOneBatchMaxDequeuedTimes
                            ||
                            stopwatch.ElapsedMilliseconds > waitOneBatchTimeOutInMilliseconds
                        )
                        {
                            if (i > 0)
                            {
                                if (stopwatch != null)
                                {
                                    stopwatch.Stop();
                                }
                                EasyPerformanceCountersHelper <CommonPerformanceCountersContainer>
                                .TryCountPerformance
                                (
                                    PerformanceCounterProcessingFlagsType.All
                                    , _performanceCountersCategoryNameForBatchProcess
                                    , _performanceCountersCategoryInstanceNameForBatchProcess
                                    , () =>
                                {
                                    return(OnGetEnabledCountPerformanceProcessFunc());
                                }
                                    , () =>
                                {
                                    onBatchDequeuesProcessAction
                                    (
                                        i
                                        , list
                                    );
                                }
                                    , null
                                    , (xx, yy, zz) =>
                                {
                                    var rrr = false;
                                    zz      = "OnDequeueProcessCaughtExceptionProcessFunc\r\n" + zz;
                                    if (onDequeuesBatchProcessCaughtExceptionProcessFunc != null)
                                    {
                                        rrr = onDequeuesBatchProcessCaughtExceptionProcessFunc
                                              (
                                            xx, yy, zz
                                              );
                                    }
                                    else if (OnCaughtException != null)
                                    {
                                        rrr = OnCaughtException(this, xx, yy, zz);
                                    }
                                    return(rrr);
                                }
                                    , (xx, yy, zz, ww) =>
                                {
                                    i = 0;
                                    list.Clear();
                                    if (stopwatch != null)
                                    {
                                        stopwatch.Restart();
                                    }
                                    onDequeuesBatchProcessFinallyProcessAction?
                                    .Invoke(xx, yy, zz, ww);
                                }
                                );
                            }
                        }
                    }
                }
                    , false
                    , (x, y, z) =>
                {
                    var rr = false;
                    if (OnCaughtException != null)
                    {
                        rr = OnCaughtException
                             (
                            this, x, y, z
                             );
                    }
                    return(rr);
                }
                );
            }
        }
示例#2
0
        private void DequeueProcess <TBatchGroupByKey>
        (
            Func <long, JToken, bool> onOnceDequeueProcessFunc
            , int sleepInMilliseconds = 100
            , Func <JToken, TBatchGroupByKey> keySelector = null
            , Action <long, bool, TBatchGroupByKey, IEnumerable <JToken> > onBatchDequeuesProcessAction = null
            , int waitOneBatchTimeOutInMilliseconds = 1000
            , int waitOneBatchMaxDequeuedTimes      = 100
            , Func <Exception, Exception, string, bool> onDequeueProcessCaughtExceptionProcessFunc       = null
            , Action <bool, Exception, Exception, string> onDequeueProcessFinallyProcessAction           = null
            , Func <Exception, Exception, string, bool> onDequeuesBatchProcessCaughtExceptionProcessFunc = null
            , Action <bool, Exception, Exception, string> onDequeuesBatchProcessFinallyProcessAction     = null
        )
        {
            if (_isStartedDequeueProcess)
            {
                return;
            }
            List <JToken> list             = null;
            var           needBatchGroupBy = false;

            if (keySelector != null)
            {
                needBatchGroupBy = true;
            }
            long      i         = 0;
            Stopwatch stopwatch = null;

            if (onBatchDequeuesProcessAction != null)
            {
                list      = new List <JToken>();
                stopwatch = new Stopwatch();
                stopwatch.Start();
            }
            while (true)
            {
                TryCatchFinallyProcessHelper
                .TryProcessCatchFinally
                (
                    true
                    , () =>
                {
                    if (!_queue.IsEmpty)
                    {
                        Tuple <Stopwatch, JToken> element = null;
                        if (_queue.TryDequeue(out element))
                        {
                            var enabledCountPerformance = true;
                            {
                                if (OnGetEnabledCountPerformanceProcessFunc != null)
                                {
                                    enabledCountPerformance = OnGetEnabledCountPerformanceProcessFunc();
                                }
                            }

                            var qpcc = _queuePerformanceCountersContainer;
                            Stopwatch stopwatchDequeue = null;
                            var stopwatchEnqueue       = element.Item1;
                            if (enabledCountPerformance)
                            {
                                _stopwatchsPool.TryGet(out stopwatchDequeue);
                            }

                            _timerCounters[0].Item2 = stopwatchEnqueue;
                            _timerCounters[1].Item2 = stopwatchDequeue;

                            #region while queue.IsEmpty loop
                            var reThrowException = false;
                            JToken item          = null;
                            bool needAdd         = false;
                            PerformanceCountersHelper
                            .TryCountPerformance
                            (
                                () =>
                            {
                                return(enabledCountPerformance);
                            }
                                , reThrowException
                                , qpcc.IncrementCountersBeforeCountPerformanceForDequeue
                                , qpcc.DecrementCountersBeforeCountPerformanceForDequeue
                                , _timerCounters
                                , () =>                                     //try
                            {
                                if (onOnceDequeueProcessFunc != null)
                                {
                                    item    = element.Item2;
                                    needAdd = onOnceDequeueProcessFunc
                                                  (i, item);
                                }
                            }
                                , (x, y, z) =>                                  //catch
                            {
                                qpcc
                                .CaughtExceptionsPerformanceCounter
                                .Increment();

                                z = "OnDequeueProcessCaughtExceptionProcessFunc\r\n" + z;
                                if (onDequeueProcessCaughtExceptionProcessFunc != null)
                                {
                                    reThrowException = onDequeueProcessCaughtExceptionProcessFunc
                                                       (
                                        x
                                        , y
                                        , z
                                                       );
                                }
                                else if (OnCaughtException != null)
                                {
                                    reThrowException = OnCaughtException(this, x, y, z);
                                }
                                return(reThrowException);
                            }
                                , onDequeueProcessFinallyProcessAction                                      //finally
                                , null
                                , qpcc.IncrementCountersAfterCountPerformanceForDequeue
                            );
                            #endregion while queue.IsEmpty loop

                            //池化

                            if (stopwatchEnqueue != null)
                            {
                                stopwatchEnqueue.Reset();
                                var r = _stopwatchsPool.TryPut(stopwatchEnqueue);
                                if (!r)
                                {
                                    stopwatchEnqueue.Stop();
                                    stopwatchEnqueue = null;
                                }
                            }
                            if (stopwatchDequeue != null)
                            {
                                stopwatchDequeue.Reset();

                                var r = _stopwatchsPool.TryPut(stopwatchDequeue);
                                if (!r)
                                {
                                    stopwatchDequeue.Stop();
                                    stopwatchDequeue = null;
                                }
                            }
                            if (onBatchDequeuesProcessAction != null)
                            {
                                //var item = element.Item2;
                                if (needAdd)
                                {
                                    i++;
                                    list.Add(item);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (sleepInMilliseconds > 0)
                        {
                            Thread.Sleep(sleepInMilliseconds);
                        }
                    }
                    if (onBatchDequeuesProcessAction != null)
                    {
                        if
                        (
                            i >= waitOneBatchMaxDequeuedTimes
                            ||
                            stopwatch.ElapsedMilliseconds > waitOneBatchTimeOutInMilliseconds
                        )
                        {
                            if (i > 0)
                            {
                                if (stopwatch != null)
                                {
                                    stopwatch.Stop();
                                }
                                EasyPerformanceCountersHelper <CommonPerformanceCountersContainer>
                                .TryCountPerformance
                                (
                                    PerformanceCounterProcessingFlagsType.All
                                    , _performanceCountersCategoryNameForBatchProcess
                                    , _performanceCountersCategoryInstanceNameForBatchProcess
                                    , () =>
                                {
                                    return(OnGetEnabledCountPerformanceProcessFunc());
                                }
                                    , () =>
                                {
                                    if (needBatchGroupBy)
                                    {
                                        var groups = list
                                                     .GroupBy
                                                     (
                                            (x) =>
                                        {
                                            return(keySelector(x));
                                        }
                                                     );

                                        groups
                                        .AsParallel()
                                        .ForAll
                                        (
                                            (group) =>
                                        {
                                            onBatchDequeuesProcessAction
                                            (
                                                i
                                                , needBatchGroupBy
                                                , group.Key
                                                , group.AsEnumerable()
                                            );
                                        }
                                        );
                                        //foreach (var group in groups)
                                        //{

                                        //}
                                    }
                                    else
                                    {
                                        onBatchDequeuesProcessAction
                                        (
                                            i
                                            , needBatchGroupBy
                                            , default(TBatchGroupByKey)
                                            , list
                                        );
                                    }
                                }
                                    , null
                                    , (xx, yy, zz) =>
                                {
                                    var rrr = false;
                                    zz      = "OnDequeueProcessCaughtExceptionProcessFunc\r\n" + zz;
                                    if (onDequeuesBatchProcessCaughtExceptionProcessFunc != null)
                                    {
                                        rrr = onDequeuesBatchProcessCaughtExceptionProcessFunc
                                              (
                                            xx, yy, zz
                                              );
                                    }
                                    else if (OnCaughtException != null)
                                    {
                                        rrr = OnCaughtException(this, xx, yy, zz);
                                    }
                                    return(rrr);
                                }
                                    , (xx, yy, zz, ww) =>
                                {
                                    i = 0;
                                    list.Clear();
                                    if (stopwatch != null)
                                    {
                                        stopwatch.Restart();
                                    }
                                    onDequeuesBatchProcessFinallyProcessAction?
                                    .Invoke(xx, yy, zz, ww);
                                }
                                );
                            }
                        }
                    }
                }
                    , false
                    , (x, y, z) =>
                {
                    var rr = false;
                    if (OnCaughtException != null)
                    {
                        rr = OnCaughtException
                             (
                            this, x, y, z
                             );
                    }
                    return(rr);
                }
                );
            }
        }