public void OnDequeue()
        {
            ModuleProc PROC = new ModuleProc(this.DYN_MODULE_NAME, "OnDequeue");

            try
            {
                IExecutorService exec = this.ExecutorService;
                while (!exec.WaitForShutdown(_queueTimeout))
                {
                    try
                    {
                        T item = _queue.Dequeue();
                        if (exec.IsShutdown)
                        {
                            break;
                        }

                        if (this.Dequeue != null)
                        {
                            this.Dequeue(item);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Exception(PROC, ex);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }
        }
        private void OnReceiveMessages()
        {
            ModuleProc PROC = new ModuleProc(this.DYN_MODULE_NAME, "OnReceiveMessages");

            try
            {
                while (!this.ExecutorService.WaitForShutdown())
                {
                    try
                    {
                        AppNotifyData data = _queue.Dequeue();
                        if (data != null && _callback != null)
                        {
                            _callback.NotifyData(data);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Exception(PROC, ex);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }
            finally
            {
                this.Shutdown();
            }
        }
Ejemplo n.º 3
0
        private void OnBroadcastMessages()
        {
            ModuleProc PROC = new ModuleProc(this.DYN_MODULE_NAME, "OnBroadcastMessages");

            try
            {
                while (!this.ExecutorService.WaitForShutdown())
                {
                    try
                    {
                        AppNotifyData data = _queue.Dequeue();
                        AppNotifyService.OnNotifyData(data);
                    }
                    catch (Exception ex)
                    {
                        Log.Exception(PROC, ex);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
                _host = null;
            }
            finally
            {
                this.Stop();
            }
        }
        private void DoWork()
        {
            ModuleProc PROC = new ModuleProc(DYN_MODULE_NAME, "DoWork");

            _doWorkInitialized = InitializeStatus.Completed;
            int threadId = Thread.CurrentThread.ManagedThreadId;

            _workerThreadId = threadId;

            try
            {
                while (!this.ExecutorService.WaitForShutdown())
                {
                    T item = default(T);

                    try
                    {
                        ThreadExecutorItem <T> executorItem = _queue.Dequeue();
                        if (this.ExecutorService.IsShutdown)
                        {
                            Log.Info(PROC, string.Format("Shutdown called. Thread : {0:D} is exiting now.", threadId));
                            break;
                        }
                        if (executorItem == null)
                        {
                            Log.Info(PROC, "Invalid executor item received.");
                            continue;
                        }

                        // actual item
                        Log.DescriptionV(PROC, "Thread : {0:D}, Queue Size : {1:D}", threadId, _queue.QueueCount);
                        item = executorItem.Item;

                        // if any preceeding item there, then wait for it to finish
                        if (executorItem.WaitHandle != null)
                        {
                            // Waiting for previous item to finish
                            WaitHandle wh = executorItem.WaitHandle;
                            Log.Info(PROC, "Waiting for previous item to finish for : " + item.UniqueKey);

                            // if shutdown called?
                            if (this.ExecutorService.WaitForShutdown(wh))
                            {
                                Log.Info(PROC, string.Format("Shutdown called. Thread : {0:D} is exiting now.", threadId));
                                break;
                            }

                            // get the signal from the previous item
                            Log.Info(PROC, "Signal got from previous item for : " + item.UniqueKey);
                        }

                        // still have item
                        if (this.ExecutorService.IsShutdown)
                        {
                            Log.Info(PROC, string.Format("Shutdown called. Thread : {0:D} is exiting now.", threadId));
                            break;
                        }

                        // actual processing and processing completion
                        if (!item.Equals(default(T)))
                        {
                            this.OnProcessItem(item);
                            this.OnProcessItemCompleted(executorItem);
                            if (_checkItemCount && (_itemCount > 0))
                            {
                                Interlocked.Decrement(ref _itemCount);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Exception(PROC, ex);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }
            finally
            {
                Log.Info(PROC, string.Format("Shutdown called. Thread : {0:D} has finished his work.", Thread.CurrentThread.ManagedThreadId));
                this.Shutdown();
            }
        }
Ejemplo n.º 5
0
        private void DoWork()
        {
            using (ILogMethod method = Log.LogMethod(this.DYN_MODULE_NAME, "DoWork"))
            {
                try
                {
                    _workerThreadId = Thread.CurrentThread.ManagedThreadId;
                    _threadName     = Thread.CurrentThread.Name;
                    if (_threadName.IsEmpty())
                    {
                        _threadName = "Thread_" + Thread.CurrentThread.ManagedThreadId.ToString();
                    }
                    method.InfoV("( {0}::DoWork ) {1} started on the thread : {2:D}", _threadName, _uniqueKey, _workerThreadId);

                    while (!_executorService2.WaitForShutdown())
                    {
                        T item = default(T);

                        try
                        {
                            byte signalItem = _signalQueue.Dequeue();
                            do
                            {
                                _dataFound     = 0;
                                _dataProcessed = 0;
                                if (_executorService2.IsShutdown)
                                {
                                    method.InfoV("( {0}::DoWork ) Shutdown called. Thread : {1:D} is exiting now.", _threadName, _workerThreadId);
                                    break;
                                }

                                int queuesCount = _dataQueues.Count;
                                Parallel.ForEach <KeyValuePair <string, ConcurrentQueue <ThreadExecutorItem <T> > > >(_dataQueues,
                                                                                                                      (KeyValuePair <string, ConcurrentQueue <ThreadExecutorItem <T> > > p, ParallelLoopState ps) =>
                                {
                                    int taskId     = Task.CurrentId.SafeValue();
                                    int queueCount = 0;
                                    if (_executorService2.WaitTokenSource.IsCancellationRequested)
                                    {
                                        ps.Stop();
                                    }
                                    ConcurrentQueue <ThreadExecutorItem <T> > queue = p.Value;
                                    queueCount = queue.Count;

                                    // data found to process
                                    if (queueCount > 0)
                                    {
                                        try
                                        {
                                            ThreadExecutorItem <T> executorItem = null;
                                            if (queue.TryDequeue(out executorItem))
                                            {
                                                _dataFound++;
                                                _dataProcessed++;
                                                method.DebugV("( {0}::DoWork_PLStart::{1} ) Item Count : {2:D}", _threadName, p.Key, queueCount);

                                                if (_executorService2.IsShutdown)
                                                {
                                                    method.InfoV("( {0}::DoWork ) Shutdown called. Thread : {1:D} is exiting now.", _threadName, _workerThreadId);
                                                    ps.Stop();
                                                }
                                                if (executorItem == null)
                                                {
                                                    method.InfoV("( {0}::DoWork ) Shutdown called. Invalid executor item received.", _threadName);
                                                    return;
                                                }

                                                // actual item
                                                method.DebugV("( {0}::DoWork ) Thread : {1:D}, Queue Size : {2:D}", _threadName, _workerThreadId, queue.Count);
                                                item = executorItem.Item;

                                                // still have item
                                                if (_executorService2.IsShutdown)
                                                {
                                                    method.InfoV("( {0}::DoWork ) Shutdown called. Thread : {1:D} is exiting now.", _threadName, _workerThreadId);
                                                    ps.Stop();
                                                }

                                                // actual processing and processing completion
                                                if (!item.Equals(default(T)))
                                                {
                                                    this.OnProcessItem(item);
                                                    this.OnProcessItemCompleted(executorItem);
                                                }
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            method.Exception(ex);
                                        }
                                        finally
                                        {
                                            method.DebugV("( {0}::DoWork_PLEnd::{1} ) Item Count : {2:D}", _threadName, p.Key, queueCount);
                                        }
                                    }
                                    else
                                    {
                                        method.DebugV("( {0}::DoWork_PLStartEnd::{1} ) Item Count : {3:D}", _threadName, p.Key, queueCount);
                                    }
                                });

                                if (_dataProcessed > 0 &&
                                    ((_itemCount - _dataProcessed) >= 0))
                                {
                                    int newCount = (_itemCount - _dataProcessed);
                                    Interlocked.Exchange(ref _itemCount, newCount);
                                    method.DebugV("( {0}::DoWork_ItemComplete ) Queue Count : {1:D}, Item Count : {2:D}", _threadName, queuesCount, _itemCount);
                                }
                                if (_executorService2.WaitForShutdown())
                                {
                                    break;
                                }
                            } while (_dataFound > 0);
                        }
                        catch (Exception ex)
                        {
                            method.Exception(ex);
                        }
                    }
                }
                catch (Exception ex)
                {
                    method.Exception(ex);
                }
                finally
                {
                    method.InfoV("( {0}::DoWork ) Shutdown called. Thread : {1:D} has finished its work.", _threadName, _workerThreadId);
                    this.Shutdown();
                }
            }
        }
Ejemplo n.º 6
0
        private void ListenMonitorQueues()
        {
            ModuleProc PROC        = new ModuleProc(DYN_MODULE_NAME, "ListenMonitorQueues");
            bool       canShutdown = true;

            // Start the queues
            try
            {
                foreach (WorkerInfo workerInfo in _workerInfos.Values)
                {
                    workerInfo.Processor.ListenAsync();
                    this.ExecutorService.WaitForShutdown();
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }

            // listen for queues
            try
            {
                int length = _queues.Count;

                // more queues found
                if (length > 1)
                {
                    while (!this.ExecutorService.WaitForShutdown())
                    {
                        for (int i = 0; i < length; i++)
                        {
                            IThreadSafeQueue <QueueMessageWrapper> queue = _queues[i];
                            bool hasDataInPriorityQueue = false;

                            while (queue.QueueCount > 0)
                            {
                                for (int j = 0; j < i; j++)
                                {
                                    // if any items available in the the previous queue
                                    if (_queues[j].QueueCount > 0)
                                    {
                                        hasDataInPriorityQueue = true;
                                        break;
                                    }
                                    if (this.ExecutorService.WaitForShutdown())
                                    {
                                        break;
                                    }
                                }

                                if (hasDataInPriorityQueue ||
                                    this.ExecutorService.IsShutdown)
                                {
                                    break;
                                }

                                try
                                {
                                    _poolExecutor.QueueWorkerItem(queue.Dequeue());
                                }
                                catch (Exception ex)
                                {
                                    Log.Exception(PROC, ex);
                                }

                                if (hasDataInPriorityQueue ||
                                    this.ExecutorService.WaitForShutdown())
                                {
                                    break;
                                }
                            }

                            if (hasDataInPriorityQueue ||
                                this.ExecutorService.WaitForShutdown())
                            {
                                break;
                            }
                        }
                    }
                }
                else // single queue is enough
                {
                    if (!this.MQUseWorkerThread)
                    {
                        canShutdown = false;
                    }
                    else
                    {
                        IThreadSafeQueue <QueueMessageWrapper> queue = _queues[0];
                        while (!this.ExecutorService.WaitForShutdown())
                        {
                            try
                            {
                                _poolExecutor.QueueWorkerItem(queue.Dequeue());
                            }
                            catch (Exception ex)
                            {
                                Log.Exception(PROC, ex);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Exception(PROC, ex);
            }
            finally
            {
                if (canShutdown)
                {
                    this.Shutdown();
                }
            }
        }