完成实现序列化的部分啦。。

pull/1/head
假正经哥哥 7 years ago
parent 822b1fbe16
commit 92f054db57
  1. 15
      samples/HessianReader/Program.cs
  2. 18
      src/DotXxlJob.Core/AdminClient.cs
  3. 3
      src/DotXxlJob.Core/Constants.cs
  4. 4
      src/DotXxlJob.Core/Enums.cs
  5. 122
      src/DotXxlJob.Core/HessianSerializer.cs
  6. 7
      src/DotXxlJob.Core/LogResult.cs
  7. 2
      src/DotXxlJob.Core/Model/AddressEntity.cs
  8. 4
      src/DotXxlJob.Core/Model/ReturnT.cs
  9. 2
      src/DotXxlJob.Core/Model/TriggerParam.cs
  10. 13
      src/Hessian.NET/ListPreamble.cs

@ -33,9 +33,22 @@ namespace HessianReader
} }
Console.WriteLine("------------------------------------------------------------"); Console.WriteLine("------------------------------------------------------------");
Console.ReadKey();
RpcResponse response = new RpcResponse {
RequestId = Guid.NewGuid().ToString("N"), Result = ReturnT.Failed("ABCDEFG")
};
using (var stream2 = new MemoryStream())
{
HessianSerializer.SerializeResponse(stream2,response);
stream2.Position = 0;
var s2 =HessianSerializer.DeserializeResponse(stream2);
Console.WriteLine(JsonConvert.SerializeObject(s2));
}
Console.WriteLine("------------------------------------------------------------");
Console.ReadKey();
/** /**
* *
* Console.WriteLine("---------------------------------------------------------------"); * Console.WriteLine("---------------------------------------------------------------");

@ -114,25 +114,29 @@ namespace DotXxlJob.Core
continue; continue;
} }
RpcResponse res; RpcResponse res = null;
try try
{ {
res = HessianSerializer.DeserializeResponse(resStream); res = HessianSerializer.DeserializeResponse(resStream);
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError("des"); _logger.LogError(ex,"DeserializeResponse error:"+ex.Message);
}
if (res == null)
{
return ReturnT.Failed("response is nul");
} }
if (res.IsError) if (res.IsError)
{ {
throw new Exception(res.error); return ReturnT.Failed(res.ErrorMsg);
}
else
{
return rpcResponse.result as ReturnT;
} }
return res.Result as ReturnT;
} }
} }
throw new Exception("xxl-rpc server address not accessable."); throw new Exception("xxl-rpc server address not accessable.");

@ -5,6 +5,7 @@ namespace DotXxlJob.Core
internal static class Constants internal static class Constants
{ {
public const string RpcRequestJavaFullName = "com.xxl.rpc.remoting.net.params.XxlRpcRequest"; public const string RpcRequestJavaFullName = "com.xxl.rpc.remoting.net.params.XxlRpcRequest";
public const string RpcResponseJavaFullName = "com.xxl.rpc.remoting.net.params.XxlRpcResponse";
public const string XxlLogsDefaultRootDirectory = "xxl-job-logs"; public const string XxlLogsDefaultRootDirectory = "xxl-job-logs";
public const string HandleLogsDirectory = "HandlerLogs"; public const string HandleLogsDirectory = "HandlerLogs";
@ -23,7 +24,7 @@ namespace DotXxlJob.Core
//Admin集群中的某台机器熔断后间隔多长时间再重试 //Admin集群中的某台机器熔断后间隔多长时间再重试
public static TimeSpan AdminServerReconnectInterval = TimeSpan.FromMinutes(3); public static TimeSpan AdminServerReconnectInterval = TimeSpan.FromMinutes(3);
//Admin集群中的某台机器请求失败多少次后熔断 //Admin集群中的某台机器请求失败多少次后熔断
public const int AdminServerCircuitFaildTimes = 3; public const int AdminServerCircuitFailedTimes = 3;
public static TimeSpan JobThreadWaitTime = TimeSpan.FromSeconds(90); public static TimeSpan JobThreadWaitTime = TimeSpan.FromSeconds(90);

@ -1,4 +0,0 @@
namespace DotXxlJob.Core
{
}

@ -18,6 +18,11 @@ namespace DotXxlJob.Core
private static readonly Dictionary<string, PropertyInfo> triggerProperties = private static readonly Dictionary<string, PropertyInfo> triggerProperties =
new Dictionary<string, PropertyInfo>(); new Dictionary<string, PropertyInfo>();
private static readonly Dictionary<string, PropertyInfo> responseProperties =
new Dictionary<string, PropertyInfo>();
private static readonly Dictionary<string, PropertyInfo> returnProperties =
new Dictionary<string, PropertyInfo>();
static HessianSerializer() static HessianSerializer()
{ {
var typeInfo = typeof(RpcRequest).GetTypeInfo(); var typeInfo = typeof(RpcRequest).GetTypeInfo();
@ -55,6 +60,42 @@ namespace DotXxlJob.Core
triggerProperties.Add(attribute.Name,property); triggerProperties.Add(attribute.Name,property);
} }
var rspTypeInfo = typeof(RpcResponse).GetTypeInfo();
foreach (var property in rspTypeInfo.DeclaredProperties)
{
var attribute = property.GetCustomAttribute<DataMemberAttribute>();
if (null == attribute)
{
continue;
}
if (!property.CanRead || !property.CanWrite)
{
continue;
}
responseProperties.Add(attribute.Name,property);
}
var retTypeInfo = typeof(ReturnT).GetTypeInfo();
foreach (var property in retTypeInfo.DeclaredProperties)
{
var attribute = property.GetCustomAttribute<DataMemberAttribute>();
if (null == attribute)
{
continue;
}
if (!property.CanRead || !property.CanWrite)
{
continue;
}
returnProperties.Add(attribute.Name,property);
}
} }
public static RpcRequest DeserializeRequest(Stream stream) public static RpcRequest DeserializeRequest(Stream stream)
@ -87,7 +128,11 @@ namespace DotXxlJob.Core
} }
else else
{ {
if (item.Item2 is HessianObject ) if (item.Item1 == "parameterTypes")
{
request.ParameterTypes = item.Item2 as List<object>;
}
else if (item.Item2 is HessianObject )
{ {
request.Parameters = new List<object>(); request.Parameters = new List<object>();
@ -100,7 +145,7 @@ namespace DotXxlJob.Core
} }
else else
{ {
throw new HessianException($"unknown item :{item.Item1}"); throw new HessianException($"unknown item :{item.Item1},{item.Item2.GetType()}");
} }
} }
} }
@ -175,7 +220,7 @@ namespace DotXxlJob.Core
return true; return true;
} }
if (typeof (String) == typeInfo.AsType()) if (typeof (string) == typeInfo.AsType())
{ {
return true; return true;
} }
@ -185,7 +230,76 @@ namespace DotXxlJob.Core
public static RpcResponse DeserializeResponse(Stream resStream) public static RpcResponse DeserializeResponse(Stream resStream)
{ {
var rsp = new RpcResponse();
try
{
var deserializer = new Deserializer(resStream);
var classDef = deserializer.ReadValue() as ClassDef;
if (!Constants.RpcResponseJavaFullName.Equals(classDef.Name))
{
throw new HessianException($"unknown class :{classDef.Name}");
}
if (responseProperties.Count != classDef.Fields.Length)
{
throw new HessianException($"unknown class :{classDef.Name}, field count not match ${responseProperties.Count} !={classDef.Fields.Length}");
}
//obj serialize
if (deserializer.ReadValue() is HessianObject hessianObject)
{
foreach (var item in hessianObject)
{
if (responseProperties.TryGetValue(item.Item1, out var p))
{
if (IsSimpleType(p.PropertyType.GetTypeInfo()))
{
p.SetValue(rsp,item.Item2);
}
else
{
if (item.Item2 is ClassDef resultClassDef )
{
//TODO:这里要做成动态的话 ,可以注册所有的实体到对应的字典中,不过这里只有这个类型哦
if (resultClassDef.Name != "com.xxl.job.core.biz.model.ReturnT")
{
throw new HessianException($"not expected parameter type [{resultClassDef.Name}]");
}
if (!(deserializer.ReadValue() is HessianObject resultData))
{
throw new HessianException("not expected parameter type ,data is null");
}
ReturnT data = new ReturnT();
foreach (var field in resultData)
{
if (returnProperties.TryGetValue(field.Item1, out var tgPropertyInfo))
{
tgPropertyInfo.SetValue(data,field.Item2);
}
}
rsp.Result = data;
}
else
{
throw new HessianException($"unknown item :{item.Item1},{item.Item2.GetType()}");
}
}
}
}
}
}
catch (EndOfStreamException)
{
//没有数据可读了
}
return rsp;
} }
} }

@ -1,7 +0,0 @@
namespace DotXxlJob.Core
{
public class LogResult
{
}
}

@ -18,7 +18,7 @@ namespace DotXxlJob.Core.Model
if (DateTime.UtcNow.Subtract(LastFailedTime.Value) > Constants.AdminServerReconnectInterval) if (DateTime.UtcNow.Subtract(LastFailedTime.Value) > Constants.AdminServerReconnectInterval)
return true; return true;
if (FailedTimes < Constants.AdminServerCircuitFaildTimes) if (FailedTimes < Constants.AdminServerCircuitFailedTimes)
return true; return true;
return false; return false;

@ -16,8 +16,8 @@ namespace DotXxlJob.Core
public ReturnT(int code, string msg) public ReturnT(int code, string msg)
{ {
this.Code = code; Code = code;
this.Msg = msg; Msg = msg;
} }

@ -6,7 +6,7 @@ namespace DotXxlJob.Core.Model
[DataContract(Name = "com.xxl.job.core.biz.model.TriggerParam")] [DataContract(Name = "com.xxl.job.core.biz.model.TriggerParam")]
public class TriggerParam public class TriggerParam
{ {
static readonly long SerialVersionUID = 42L; //static readonly long SerialVersionUID = 42L;
[DataMember(Name = "jobId",Order = 1)] [DataMember(Name = "jobId",Order = 1)]
public int JobId { get; set; } public int JobId { get; set; }

@ -1,13 +0,0 @@
namespace Hessian.Net
{
public enum ListPreamble
{
None,
VarList,
FixList,
VarListUntyped,
FixListUntyped,
CompactFixList,
CompactFixListUntyped
}
}
Loading…
Cancel
Save