Example #1
0
        /// <summary>
        /// 处理XxlRpc请求流
        /// </summary>
        /// <param name="reqStream"></param>
        /// <returns></returns>
        public async Task <byte[]> HandlerAsync(Stream reqStream)
        {
            var req = HessianSerializer.DeserializeRequest(reqStream);

            var res = new RpcResponse {
                RequestId = req.RequestId
            };

            if (!ValidRequest(req, out var error))
            {
                this._logger.LogWarning("job task request is not valid:{error}", error);
                res.ErrorMsg = error;
            }
            else
            {
                this._logger.LogDebug("receive job task ,req.RequestId={requestId},method={methodName}"
                                      , req.RequestId, req.MethodName);
                await Invoke(req, res);

                this._logger.LogDebug("completed receive job task ,req.RequestId={requestId},method={methodName},IsError={IsError}"
                                      , req.RequestId, req.MethodName, res.IsError);
            }

            using (var outputStream = new MemoryStream())
            {
                HessianSerializer.SerializeResponse(outputStream, res);
                return(outputStream.GetBuffer());
            }
        }
Example #2
0
        private async Task <ReturnT> InvokeRpcService(string methodName, List <object> parameterTypes,
                                                      object parameters, bool polling = false)
        {
            var request = new RpcRequest {
                RequestId        = Guid.NewGuid().ToString("N"),
                CreateMillisTime = DateTime.Now.GetTotalMilliseconds(),
                AccessToken      = _options.AccessToken,
                ClassName        = "com.xxl.job.core.biz.AdminBiz",
                MethodName       = methodName,
                ParameterTypes   = parameterTypes,
                Parameters       = new List <object> {
                    parameters
                }
            };

            byte[] postBuf;
            using (var stream = new MemoryStream())
            {
                HessianSerializer.SerializeRequest(stream, request);

                postBuf = stream.ToArray();
            }

            var triedTimes = 0;
            var retList    = new List <ReturnT>();

            using (var client = this._clientFactory.CreateClient(Constants.DefaultHttpClientName))
            {
                while (triedTimes++ < this._addresses.Count)
                {
                    var address = this._addresses[this._currentIndex];
                    this._currentIndex = (this._currentIndex + 1) % this._addresses.Count;
                    if (!address.CheckAccessible())
                    {
                        continue;
                    }

                    Stream resStream;
                    try
                    {
                        resStream = await DoPost(client, address, postBuf);

                        address.Reset();
                    }
                    catch (Exception ex)
                    {
                        this._logger.LogError(ex, "request admin error.{0}", ex.Message);
                        address.SetFail();
                        continue;
                    }

                    RpcResponse res = null;
                    try
                    {
                        /*
                         * using (StreamReader reader = new StreamReader(resStream))
                         * {
                         * string content  = await reader.ReadToEndAsync();
                         *
                         * this._logger.LogWarning(content);
                         * }
                         */
                        res = HessianSerializer.DeserializeResponse(resStream);
                    }
                    catch (Exception ex)
                    {
                        this._logger.LogError(ex, "DeserializeResponse error:{errorMessage}", ex.Message);
                    }

                    if (res == null)
                    {
                        retList.Add(ReturnT.Failed("response is null"));
                    }
                    else if (res.IsError)
                    {
                        retList.Add(ReturnT.Failed(res.ErrorMsg));
                    }
                    else if (res.Result is ReturnT ret)
                    {
                        retList.Add(ret);
                    }
                    else
                    {
                        retList.Add(ReturnT.Failed("response is null"));
                    }

                    if (!polling)
                    {
                        return(retList[0]);
                    }
                }

                if (retList.Count > 0)
                {
                    return(retList.Last());
                }
            }
            throw new Exception("xxl-rpc server address not accessible.");
        }