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); } } }
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); } } }
void ControlThread(StreamMsgConsumeFactory msgConsumeFactory, ref bool bstop, Configuration conf, TopCometStreamRequest cometReq) { long lastSleepTime = 0; while (!bstop) { if (allStop) { break; } try { if (StreamConstants.SERVER_DEPLOY.Equals(serverRespCode)) { // 服务端在发布 logger.Info("Server is upgrade sleep " + conf.GetSleepTimeOfServerInUpgrade() + " seconds"); Thread.Sleep(conf.GetSleepTimeOfServerInUpgrade() * 1000); StartConsumeThread(cometReq); } else if (/*客户端第一次发起连接请求*/ StreamConstants.CLIENT_FIRST_CONNECT.Equals(serverRespCode) || /*服务端主动断开了所有的连接*/ StreamConstants.SERVER_REHASH.Equals(serverRespCode) || /*连接到达最大时间*/ StreamConstants.CONNECT_REACH_MAX_TIME.Equals(serverRespCode) || /*在一些异常情况下需要重连*/ StreamConstants.RECONNECT.Equals(serverRespCode)) { StartConsumeThread(cometReq); } else if (/*客户端自己把自己踢开*/ StreamConstants.CLIENT_KICKOFF.Equals(serverRespCode) || /*服务端把客户端踢开*/ StreamConstants.SERVER_KICKOFF.Equals(serverRespCode)) { if ((StreamConstants.CLIENT_KICKOFF.Equals(serverRespCode) && !isReconnect) || StreamConstants.SERVER_KICKOFF.Equals(serverRespCode)) { break;// 终止掉当前线程 } } else { //错误码设置出错,停止线程 bstop = true; break; } //连接成功,开始休眠 try { Monitor.Enter(objLock); { lastSleepTime = DateTime.Now.Ticks; Monitor.Wait(objLock, conf.GetHttpReconnectInterval() * 1000); if (DateTime.Now.Ticks - lastSleepTime >= (conf.GetHttpReconnectInterval()) * 1000 * 10000) { /* * 快要到达连接的最大时间了,需要重新发起连接 */ serverRespCode = StreamConstants.RECONNECT; isReconnect = true; }//否则,是由于某种原因被唤醒的 } } catch (Exception e) { logger.Error(e.Message); } finally { Monitor.Exit(objLock); } } catch (Exception e) { logger.Error("Occur some error,stop the stream consume" + e.Message); bstop = true; try { Monitor.Enter(objLock); Monitor.PulseAll(objLock); } finally { Monitor.Exit(objLock); } } } if (currentStreamImpl != null) { try { currentStreamImpl.Close(); } catch (Exception e) { // ignore } } logger.Info("Stop stream consume"); }
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); } } }