Beispiel #1
0
        void JdCometStreamConsume(long lastStartConsumeThread, ref bool bsJd, IStreamImplementation stream, IConnectionLifeCycleListener connectListener)
        {
            startConsumeThreadTimes = 0;
            while (!allSJd&&!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)
                        {
                            bsJd = true;
                            if (connectionListener != null)
                            {
                                try
                                {
                                    connectionListener.OnMaxReadTimeoutException();
                                }
                                catch (Exception maxE) { logger.Error(maxE.Message); }
                            }
                            logger.Error("Occure too many exception,sJd 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);
                }
            }
        }
Beispiel #2
0
 public void SetConnectionListener(IConnectionLifeCycleListener connectionLifeCycleListener)
 {
     this.connectionListener = connectionLifeCycleListener;
 }
Beispiel #3
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);
                }
            }
        }
Beispiel #4
0
 public void SetConnectListener(IConnectionLifeCycleListener connectListener)
 {
     this.connectListener = connectListener;
 }
 private void TopCometStreamConsume(long lastStartConsumeThread, ref bool bstop, IStreamImplementation stream, IConnectionLifeCycleListener connectListener)
 {
     this.startConsumeThreadTimes = 0;
     while ((!this.allStop && !this.closed) && stream.IsAlive())
     {
         try
         {
             stream.NextMsg();
             continue;
         }
         catch (Exception)
         {
             if (stream != null)
             {
                 try
                 {
                     stream.Close();
                 }
                 catch (Exception exception)
                 {
                     this.logger.Error(exception.Message);
                 }
             }
             stream = null;
             this.closed = true;
             if (this.connectionListener != null)
             {
                 try
                 {
                     this.connectionListener.OnReadTimeout();
                 }
                 catch (Exception exception2)
                 {
                     this.logger.Error(exception2.Message);
                 }
             }
             if ((DateTime.Now.Ticks - lastStartConsumeThread) < 0x430e23400L)
             {
                 this.startConsumeThreadTimes++;
                 if (this.startConsumeThreadTimes >= 10)
                 {
                     bstop = true;
                     if (this.connectionListener != null)
                     {
                         try
                         {
                             this.connectionListener.OnMaxReadTimeoutException();
                         }
                         catch (Exception exception3)
                         {
                             this.logger.Error(exception3.Message);
                         }
                     }
                     this.logger.Error("Occure too many exception,stop the system,please check");
                     try
                     {
                         try
                         {
                             Monitor.Enter(this.objLock);
                             Monitor.PulseAll(this.objLock);
                         }
                         catch (Exception)
                         {
                         }
                         continue;
                     }
                     finally
                     {
                         Monitor.Exit(this.objLock);
                     }
                 }
                 this.startConsumeThreadTimes = 0;
                 this.serverRespCode = "500";
                 try
                 {
                     Monitor.Enter(this.objLock);
                     Monitor.PulseAll(this.objLock);
                 }
                 catch (Exception)
                 {
                 }
                 finally
                 {
                     Monitor.Exit(this.objLock);
                 }
                 this.closed = false;
             }
             else
             {
                 Console.WriteLine(" 通知重连" + DateTime.Now.ToString());
                 this.startConsumeThreadTimes = 0;
                 this.serverRespCode = "500";
                 try
                 {
                     Monitor.Enter(this.objLock);
                     Console.WriteLine(" PulseAll" + DateTime.Now.ToString());
                     Monitor.PulseAll(this.objLock);
                 }
                 catch (Exception)
                 {
                 }
                 finally
                 {
                     Monitor.Exit(this.objLock);
                 }
                 this.closed = false;
             }
             break;
         }
     }
     if (stream != null)
     {
         try
         {
             stream.Close();
         }
         catch (Exception exception4)
         {
             this.logger.Warn(exception4.Message);
         }
     }
 }