예제 #1
0
        private void Execute()
        {
            int threadNo;

            lock (_counterLock)
            {
                threadNo = _counter;
                _counter++;
            }

            while (!_shutdown)
            {
                try
                {
                    ReceivedMessage item = _queue.TryDequeue(_timeOut);
                    if (item != null)
                    {
                        try
                        {
                            IWorker worker = CreateWorker(item, threadNo);
                            worker.StartLog();
                            if (worker.CanExecuteNow())
                            {
                                if (worker.AquireLock())
                                {
                                    try
                                    {
                                        worker.DoWork();
                                        try
                                        {
                                            worker.EndLog(true, false, false);
                                        }
                                        finally
                                        {
                                            _queue.Ack(item);
                                            worker.ReleaseLock();
                                            worker.Dispose();
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        _queue.Ack(item);
                                        worker.ReleaseLock();
                                        worker.EndLog(false, true, true);
                                        worker.Dispose();
                                        ex.Data.Add("PoolType", GetType().Name);
                                        ex.Data.Add("Key", item.Payload);
                                        //todo: send alert
                                    }
                                }
                                else
                                {
                                    //Thread.Sleep(2000);
                                    _queue.Ack(item);
                                    worker.EndLog(false, false, true);
                                    worker.Dispose();
                                }
                            }
                            else
                            {
                                _queue.Ack(item);
                                worker.EndLog(false, false, true);
                                worker.Dispose();
                                //_queue.Requeue(item);
                            }
                        }
                        catch (Exception ex)
                        {
                            Thread.Sleep(_timeOut); //throttle retries
                            _queue.Ack(item);
                            ex.Data.Add("PoolType", GetType().Name);
                            ex.Data.Add("Key", item.Payload);
                            //todo: send alert
                        }
                    }
                    else
                    {
                        Thread.Sleep(_timeOut); //no work
                    }
                }
                catch (Exception ex)
                {
                    //todo: send alert
                }
            }
        }