Beispiel #1
0
        void TopCometStreamConsume(long lastStartConsumeThread, ref bool bstop, IStreamImplementation stream, IConnectionLifeCycleListener connectListener)
        {
            startConsumeThreadTimes = 0;
            while (!allStop && !closed && stream.IsAlive())
            {
                try
                {
                    stream.NextMsg();
                }
                catch (Exception e)
                {//出现了read time out异常
                    // 资源清理
                    if (stream != null)
                    {
                        try
                        {
                            stream.Close();
                        }
                        catch (Exception e1)
                        {
                            logger.Error(e1.Message);
                        }
                    }
                    stream = null;
                    closed = true;
                    //通知
                    if (connectionListener != null)
                    {
                        try
                        {
                            connectionListener.OnReadTimeout();
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.Message);
                        }
                    }

                    /**
                     * 30分钟内发送了10次IOException
                     */
                    if (DateTime.Now.Ticks - lastStartConsumeThread < 18000000000)
                    {// 短时间内由于读取IOException连接了10次,则退出
                        startConsumeThreadTimes++;
                        if (startConsumeThreadTimes >= 10)
                        {
                            bstop = true;
                            if (connectionListener != null)
                            {
                                try
                                {
                                    connectionListener.OnMaxReadTimeoutException();
                                }
                                catch (Exception maxE) { logger.Error(maxE.Message); }
                            }
                            logger.Error("Occure too many exception,stop the system,please check");
                            //通知唤醒控制线程,但是不在发起重连接
                            try
                            {
                                Monitor.Enter(objLock);
                                Monitor.PulseAll(objLock);
                            }
                            catch (Exception e2)
                            {
                            }
                            finally
                            {
                                Monitor.Exit(objLock);
                            }
                        }
                        else
                        {   //没有到达10次,通知重连
                            startConsumeThreadTimes = 0;
                            serverRespCode          = StreamConstants.RECONNECT;
                            try
                            {
                                Monitor.Enter(objLock);
                                Monitor.PulseAll(objLock);
                            }
                            catch (Exception e2)
                            {
                            }
                            finally
                            {
                                Monitor.Exit(objLock);
                            }
                            closed = false;
                            break;
                        }
                    }
                    else
                    {
                        // 通知重连
                        Console.WriteLine(" 通知重连" + DateTime.Now.ToString());
                        startConsumeThreadTimes = 0;
                        serverRespCode          = StreamConstants.RECONNECT;

                        try
                        {
                            Monitor.Enter(objLock);
                            Console.WriteLine(" PulseAll" + DateTime.Now.ToString());
                            Monitor.PulseAll(objLock);
                        }
                        catch (Exception e2)
                        {
                        }
                        finally
                        {
                            Monitor.Exit(objLock);
                        }
                        closed = false;
                        break;
                    }
                }
            }
            //出现异常情况下做资源清理
            if (stream != null)
            {
                try
                {
                    stream.Close();
                }
                catch (Exception e)
                {
                    logger.Warn(e.Message);
                }
            }
        }