public MessageStreamImpl(StreamMsgConsumeFactory msgConsumeFactory, HttpResponse response, IJdCometMessageListener msgListener, JdCometStreamImpl cometStreamImpl) : base(msgConsumeFactory, response) { this.msgListener = msgListener; this.objLock = cometStreamImpl.GetControlLock(); this.cometStreamImpl = cometStreamImpl; }
void ControlThread(StreamMsgConsumeFactory msgConsumeFactory, ref bool bsJd, Configuration conf, JdCometStreamRequest cometReq) { long lastSleepTime = 0; while (!bsJd) { if (allSJd) { 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 { //错误码设置出错,停止线程 bsJd = 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,sJd the stream consume" + e.Message); bsJd = true; try { Monitor.Enter(objLock); Monitor.PulseAll(objLock); } finally { Monitor.Exit(objLock); } } } if (currentStreamImpl != null) { try { currentStreamImpl.Close(); } catch (Exception e) { // ignore } } logger.Info("SJd stream consume"); }
public void Start() { List<JdCometStreamRequest> cometRequests = conf.GetConnectReqParam(); msgConsumeFactory = new StreamMsgConsumeFactory(conf.GetMinThreads(), conf.GetMaxThreads(), conf.GetQueueSize()); for (int i = 0; i < cometRequests.Count; i++) { try { JdCometStreamRequest cometRequest = cometRequests[i]; if (cometRequest.GetConnectListener() == null) { cometRequest.SetConnectListener(connectionListener); } if (cometRequest.GetMsgListener() == null) { cometRequest.SetMsgListener(cometMessageListener); } Thread controlThread = new Thread(delegate() { ControlThread(msgConsumeFactory, ref bsJd, conf, cometRequest); }); controlThread.Name = "stream-control-thread-connectid-" + cometRequest.GetConnectId(); controlThread.Start(); controlThreads.Add(controlThread); } catch (Exception e) { continue; } } }
public AbstractStreamImpl(StreamMsgConsumeFactory msgConsumeFactory, HttpResponse response) { this.msgConsumeFactory = msgConsumeFactory; this.response = response; }