Exemplo n.º 1
0
 public Writer(Index index)
 {
     this.index = index;
     pool = new MiscUtil.Threading.CustomThreadPool(index.Path);
     pool.SetMinMaxThreads(0, 5);
     pool.IdlePeriod = 10;
     pool.WorkerThreadPriority = ThreadPriority.BelowNormal;
     pool.WorkerThreadsAreBackground = false;
     pool.BeforeWorkItem += new BeforeWorkItemHandler(pool_BeforeWorkItem);
     pool.AfterWorkItem += new AfterWorkItemHandler(pool_AfterWorkItem);
     pool.WorkerException += new ThreadPoolExceptionHandler(pool_WorkerException);
 }
        private void QueueHandler()
        {
            //this will receive a notification when there is a new HttpRequest in the thread.
            workerThreadPool = new CustomThreadPool();

            int minThreads = 1;
            int maxThreads = 20;
            int processedQueueItemsCount = 0;

            workerThreadPool.SetMinMaxThreads(minThreads, maxThreads);

            logger.Info("[" + MethodBase.GetCurrentMethod().Name + "()] - HTTP Web Request thread started");

            while (1 == 1)
            {
                resourceLockOut.WaitOne();

                while (itemList.Count > 0)
                {
                    if ((itemList.Count > 100) && (itemList.Count % 1000 == 0))
                    {
                        logger.Error("[" + MethodBase.GetCurrentMethod().Name + "()] - http queue size: " + itemList.Count);
                    }

                    lock (itemList)
                    {
                        try
                        {
                            //Fetch item from front of queue and request a thread from thread pool to process it
                            HttpWebRequest req = itemList.Dequeue();

                            processHTTPWebRequestImmediately(req);

                            processedQueueItemsCount++;

                        }
                        catch (Exception e)
                        {
                            logger.Error("[" + MethodBase.GetCurrentMethod().Name + "()] - System Exception doing QUEUE Request: " + e.ToString());
                        }

                    }
                }
            }
        }
        private void QueueHandler()
        {
            logger.Debug(MethodBase.GetCurrentMethod().Name + "() - START");

            //this will receive a notification when there is a new HttpRequest in the thread.
            workerThreadPool = new CustomThreadPool();

            int minThreads = 1;
            int maxThreads = ConnectionConfig.QueueHelperMaxThreads_OutgoingMessage;

            workerThreadPool.SetMinMaxThreads(minThreads, maxThreads);

            logger.Info(MethodBase.GetCurrentMethod().Name + "() - queue handler thread started");

            while (true)
            {
                resourceLockOut.WaitOne();

                while (itemList.Count > 0 && !ConnectionManager.Instance.IsReconnecting)
                {
                    if ((itemList.Count > 100) && (itemList.Count % 1000 == 0))
                    {
                        logger.Info(MethodBase.GetCurrentMethod().Name + "() - queue size: " + itemList.Count);
                    }

                    IMessageToSend item = null;

                    lock (itemList)
                    {
                        try
                        {
                            //Fetch item from front of queue and request a thread from thread pool to process it
                            item = itemList.Dequeue();

                        }
                        catch (Exception e)
                        {
                            logger.Error(MethodBase.GetCurrentMethod().Name + "() - System Exception doing QUEUE Request: " + e.ToString());
                        }

                    }

                    if (item != null)
                        addRequestToWorkerPool(item);
                }
            }

            logger.Debug(MethodBase.GetCurrentMethod().Name + "() - END");
        }
        //Public method exposed for starting the thread pools
        private void initThreadPool()
        {
            logger.Debug(MethodBase.GetCurrentMethod().Name + "() - START");

            Console.Write(DateTime.Now.ToString() + " Creating Thread Pool: ThreadPool_SendRESTMessage... ");

            //Create the thread pool
            workerThreadPool = new CustomThreadPool();

            int minThreads = 1;
            int maxThreads = ConnectionConfig.QueueHelperMaxThreads_OutgoingRESTMessage;

            workerThreadPool.SetMinMaxThreads(minThreads, maxThreads);

            Console.WriteLine(" OK");

            logger.Debug(MethodBase.GetCurrentMethod().Name + "() - END");
        }
        private void QueueHandler()
        {
            //this will receive a notification when there is a new HttpRequest in the thread.
            workerThreadPool = new CustomThreadPool();

            int minThreads = 1;
            int maxThreads = 1;
            int processedQueueItemsCount = 0;

            workerThreadPool.SetMinMaxThreads(minThreads, maxThreads);

            logger.Info("[" + MethodBase.GetCurrentMethod().Name + "()] - HTTP Web Request thread started");

            while (1 == 1)
            {
                resourceLockOut.WaitOne();

                while (itemList.Count > 0)
                {
                    if ((itemList.Count > 100) && (itemList.Count % 1000 == 0))
                    {
                        logger.Error("[" + MethodBase.GetCurrentMethod().Name + "()] - http queue size: " + itemList.Count);
                    }

                    lock (itemList)
                    {
                        try
                        {
                            //Fetch item from front of queue and request a thread from thread pool to process it
                            HttpWebRequest req = itemList.Dequeue();

                            using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
                            {
                                using (Stream responseStream = response.GetResponseStream())
                                {
                                    using (StreamReader streamIn = new StreamReader(responseStream))
                                    {
                                        String strResponse = streamIn.ReadToEnd();
                                        streamIn.Close();
                                    }
                                    responseStream.Flush();
                                    responseStream.Close();
                                }
                                response.Close();
                            }

                            processedQueueItemsCount++;

                        }
                        catch (Exception e)
                        {
                            logger.Error("[" + MethodBase.GetCurrentMethod().Name + "()] - System Exception doing QUEUE Request: " + e.ToString());
                        }

                    }
                }
            }
        }
        private void QueueHandler()
        {
            //this will receive a notification when there is a new HttpRequest in the thread.
            workerThreadPool = new CustomThreadPool();

            int minThreads = 1;
            int maxThreads = 20;

            workerThreadPool.SetMinMaxThreads(minThreads, maxThreads);

            logger.Info("[" + MethodBase.GetCurrentMethod().Name + "()] - HTTP Web Request thread started");

            while (1 == 1)
            {
                resourceLockOut.WaitOne();

                while (itemList.Count > 0)
                {
                    if ((itemList.Count > 100) && (itemList.Count % 1000 == 0))
                    {
                        logger.Error("[" + MethodBase.GetCurrentMethod().Name + "()] - http queue size: " + itemList.Count);

                        if (itemList.Count > 5000) {
                            //we're in trouble if we get to this point
                            //better to dump queue and start over?
                            itemList.Clear();
                            logger.Fatal("[" + MethodBase.GetCurrentMethod().Name + "()] - queue size exceeds max threshold: " + itemList.Count + ", clearing queue!");
                        }
                    }

                    lock (itemList)
                    {
                        try
                        {
                            //Fetch item from front of queue and request a thread from thread pool to process it
                            HttpWebRequest req = itemList.Dequeue();
                            addHTTPRequestDelegateToThreadPool(req);
                        }
                        catch (Exception e)
                        {
                            logger.Error("[" + MethodBase.GetCurrentMethod().Name + "()] - System Exception doing QUEUE Request: " + e.ToString());
                        }

                    }
                }
            }
        }