Example #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);
                }
            }
        }
Example #2
0
        private T DoExecute <T> (IJdRequest <T> request, string access_token, DateTime timestamp) where T : JdResponse
        {
            // 提前检查业务参数
            try {
                request.Validate();
            } catch (JdException e) {
                return(createErrorResponse <T> (e.ErrorCode, e.ErrorMsg));
            }

            // 添加协议级请求参数
            JdDictionary txtParams = new JdDictionary();

            txtParams.Add(METHOD, request.GetApiName());
            txtParams.Add(VERSION, "2.0");
            txtParams.Add(APP_KEY, appKey);
            txtParams.Add(TIMESTAMP, timestamp);
            if (access_token != null)
            {
                txtParams.Add(ACCESS_TOKEN, access_token);
            }
            txtParams.Add(PARAM_JSON, JsonConvert.ExportToString(request.GetParameters()));
            // 添加签名参数
            txtParams.Add(SIGN, JdUtils.SignJdRequest(txtParams, appSecret, true));
            string reqUrl = webUtils.BuildGetUrl(this.serverUrl, txtParams);

            try {
                string body;
                if (request is IJdUploadRequest <T> )                 // 是否需要上传文件
                {
                    IJdUploadRequest <T>           uRequest   = (IJdUploadRequest <T>)request;
                    IDictionary <string, FileItem> fileParams = JdUtils.CleanupDictionary(uRequest.GetFileParameters());
                    body = webUtils.DoPost(this.serverUrl, txtParams, fileParams);
                }
                else
                {
                    body = webUtils.DoPost(this.serverUrl, txtParams);
                }

                // 解释响应结果
                T rsp;
                if (disableParser)
                {
                    rsp      = Activator.CreateInstance <T> ();
                    rsp.Body = body;
                }
                else
                {
                    if (FORMAT_XML.Equals(format))
                    {
                        IJdParser tp = new JdXmlParser();
                        rsp = tp.Parse <T> (body);
                    }
                    else
                    {
                        IJdParser tp = new JdJsonParser();
                        rsp = tp.Parse <T> (body);
                    }
                }

                // 追踪错误的请求
                if (!disableTrace && rsp.IsError)
                {
                    StringBuilder sb = new StringBuilder(reqUrl).Append(" response error!\r\n").Append(rsp.Body);
                    JdLogger.Warn(sb.ToString());
                }
                return(rsp);
            } catch (Exception e) {
                if (!disableTrace)
                {
                    StringBuilder sb = new StringBuilder(reqUrl).Append(" request error!\r\n").Append(e.StackTrace);
                    JdLogger.Error(sb.ToString());
                }
                throw e;
            }
        }
Example #3
0
        private T DoExecute <T>(IJdRequest <T> request, string accessToken, DateTime timestamp) where T : JdResponse
        {
            //if (String.IsNullOrEmpty(accessToken))
            //{
            //    throw new Exception("Access Token 必须提供!");
            //}

            // 提前检查业务参数
            try
            {
                request.Validate();
            }
            catch (JdException e)
            {
                return(createErrorResponse <T>(e.ErrorCode, e.ErrorMsg));
            }

            // 添加协议级请求参数
            JdDictionary txtParams = new JdDictionary();

            txtParams.Add(APP_PARAM, request.GetParamJson());
            txtParams.Add(METHOD, request.ApiName);
            txtParams.Add(VERSION, "2.0");
            txtParams.Add(APP_KEY, appKey);
            //txtParams.Add(FORMAT, format);
            //txtParams.Add(PARTNER_ID, "top-sdk-net-20111024");
            txtParams.Add(TIMESTAMP, timestamp);
            txtParams.Add(ACCESSTOKEN, accessToken);

            // 添加签名参数
            txtParams.Add(SIGN, JdUtils.SignJdRequest(txtParams, appSecret));

            // 是否需要上传文件
            string body;

            if (request is IJdUploadRequest <T> )
            {
                IJdUploadRequest <T>           uRequest   = (IJdUploadRequest <T>)request;
                IDictionary <string, FileItem> fileParams = JdUtils.CleanupDictionary(uRequest.GetFileParameters());
                body = webUtils.DoPost(this.serverUrl, txtParams, fileParams);
            }
            else
            {
                body = webUtils.DoPost(this.serverUrl, txtParams);
            }

            // 解释响应结果
            T rsp;

            if (disableParser)
            {
                rsp      = Activator.CreateInstance <T>();
                rsp.Body = body;
            }
            else
            {
                if (FORMAT_JSON.Equals(format))
                {
                    IJdParser <T> tp = new JdJsonParser <T>();
                    rsp = tp.Parse(body);
                }
                else
                {
                    IJdParser <T> tp = new JdXmlParser <T>();
                    rsp = tp.Parse(body);
                }
            }

            // 追踪错误的请求
            if (!disableTrace)
            {
                rsp.ReqUrl = webUtils.BuildGetUrl(this.serverUrl, txtParams);
                if (rsp.IsError)
                {
                    topLogger.Warn(rsp.ReqUrl + "\r\n" + rsp.Body);
                }
            }

            return(rsp);
        }