public MessageStreamImpl(StreamMsgConsumeFactory msgConsumeFactory,
     HttpResponse response, ITopCometMessageListener msgListener, TopCometStreamImpl cometStreamImpl)
     : base(msgConsumeFactory, response)
 {
     this.msgListener = msgListener;
     this.objLock = cometStreamImpl.GetControlLock();
     this.cometStreamImpl = cometStreamImpl;
 }
Exemplo n.º 2
0
 public MessageStreamImpl(StreamMsgConsumeFactory msgConsumeFactory,
                          HttpResponse response, ITopCometMessageListener msgListener, TopCometStreamImpl cometStreamImpl)
     : base(msgConsumeFactory, response)
 {
     this.msgListener     = msgListener;
     this.objLock         = cometStreamImpl.GetControlLock();
     this.cometStreamImpl = cometStreamImpl;
 }
 public AbstractStreamImpl(StreamMsgConsumeFactory msgConsumeFactory, HttpResponse response)
 {
     this.msgConsumeFactory = msgConsumeFactory;
     this.response = response;
 }
Exemplo n.º 4
0
        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");
        }
Exemplo n.º 5
0
        public void Start()
        {
            if (cometMessageListener == null)
            {
                throw new Exception("Comet message listener must not null");
            }

            List<TopCometStreamRequest> cometRequests = conf.GetConnectReqParam();

            msgConsumeFactory = new StreamMsgConsumeFactory(conf.GetMinThreads(), conf.GetMaxThreads(), conf.GetQueueSize());

            for (int i = 0; i < cometRequests.Count; i++)
            {
                try
                {
                    TopCometStreamRequest 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 bstop, conf, cometRequest);
                    });
                    controlThread.Name = "stream-control-thread-connectid-" + cometRequest.GetConnectId();
                    controlThread.Start();
                    controlThreads.Add(controlThread);
                }
                catch (Exception e)
                {
                    continue;
                }
            }
        }
        public void Start()
        {
            //原以为设置了cometRequst的msgListenr,就没有必要在本地msgListener未设置时抛出异常
            //我把检测放在后面,当cometRequst的msglistener和本地msglistener都找不到时才抛出异常
            //后来发现本地cometMessageListener必须赋值,从cometRequst中获取

            List<TopCometStreamRequest> cometRequests = conf.GetConnectReqParam();

            msgConsumeFactory = new StreamMsgConsumeFactory(conf.GetMinThreads(), conf.GetMaxThreads(), conf.GetQueueSize());

            for (int i = 0; i < cometRequests.Count; i++)
            {
                try
                {
                    TopCometStreamRequest cometRequest = cometRequests[i];
                    if (cometRequest.GetConnectListener() == null)
                    {
                        cometRequest.SetConnectListener(connectionListener);
                    }
                    if (cometMessageListener == null)
                    {
                        if (cometRequest.GetMsgListener() == null)
                            throw new Exception("Comet message listener must not null");
                        else
                            cometMessageListener = cometRequest.GetMsgListener();
                        //cometRequest.SetMsgListener(cometMessageListener);
                    }
                    Thread controlThread = new Thread(delegate()
                    {
                        ControlThread(msgConsumeFactory, ref bstop, conf, cometRequest);
                    });
                    controlThread.Name = "stream-control-thread-connectid-" + cometRequest.GetConnectId();
                    controlThread.Start();
                    controlThreads.Add(controlThread);
                }
                catch
                {
                    continue;
                }
            }
        }