/// <summary>生产添加</summary> /// <param name="value">消息</param> /// <returns></returns> public Int32 Add(T value) { using var span = Redis.Tracer?.NewSpan($"redismq:Add:{TraceName}", value); var val = AttachTraceId ? Redis.AttachTraceId(value) : value; var rs = 0; for (var i = 0; i <= RetryTimesWhenSendFailed; i++) { // 返回插入后的LIST长度。Redis执行命令不会失败,因此正常插入不应该返回0,如果返回了0或者服务,可能是中间代理出了问题 rs = Execute(rc => rc.Execute <Int32>("LPUSH", Key, val), true); if (rs > 0) { return(rs); } span?.SetError(new RedisException($"发布到队列[{Topic}]失败!"), null); if (i < RetryTimesWhenSendFailed) { Thread.Sleep(RetryIntervalWhenSendFailed); } } ValidWhenSendFailed(span); return(rs); }
/// <summary>批量生产添加</summary> /// <param name="values">消息集合</param> /// <returns></returns> public Int32 Add(params T[] values) { using var span = Redis.Tracer?.NewSpan($"redismq:Add:{TraceName}", values); var args = new List <Object> { Key }; foreach (var item in values) { if (AttachTraceId) { args.Add(Redis.AttachTraceId(item)); } else { args.Add(item); } } // 返回插入后的LIST长度 var rs = Execute(rc => rc.Execute <Int32>("LPUSH", args.ToArray()), true); if (rs <= 0 && ThrowOnFailure) { var ex = new RedisException($"发布到队列[{Topic}]失败!"); span?.SetError(ex, null); throw ex; } return(rs); }
/// <summary>批量生产添加</summary> /// <param name="values">消息集合</param> /// <returns></returns> public Int32 Add(params T[] values) { if (values == null || values.Length == 0) { return(0); } using var span = Redis.Tracer?.NewSpan($"redismq:AddReliable:{Key}", values); var args = new List <Object> { Key }; foreach (var item in values) { if (AttachTraceId) { args.Add(Redis.AttachTraceId(item)); } else { args.Add(item); } } return(Execute(rc => rc.Execute <Int32>("LPUSH", args.ToArray()), true)); }
/// <summary>生产添加</summary> /// <param name="value">消息体</param> /// <param name="msgId">消息ID</param> /// <returns>返回消息ID</returns> public String Add(T value, String msgId = null) { if (value == null) { throw new ArgumentNullException(nameof(value)); } using var span = Redis.Tracer?.NewSpan($"redismq:AddStream:{Key}", value); // 自动修剪超长部分,每1000次生产,修剪一次 if (_count <= 0) { _count = Count; } Interlocked.Increment(ref _count); var args = new List <Object> { Key }; if (MaxLenngth > 0 && _count % 1000 == 0) { _count = Count + 1; args.Add("maxlen"); args.Add("~"); args.Add(MaxLenngth); } // *号表示服务器自动生成ID args.Add(msgId.IsNullOrEmpty() ? "*" : msgId); // 数组和复杂对象字典,分开处理 if (Type.GetTypeCode(value.GetType()) != TypeCode.Object) { //throw new ArgumentOutOfRangeException(nameof(value), "消息体必须是复杂对象!"); args.Add(PrimitiveKey); args.Add(value); } else if (value.GetType().IsArray) { foreach (var item in (value as Array)) { args.Add(item); } } else { // 在消息体内注入TraceId,用于构建调用链 var val = AttachTraceId ? Redis.AttachTraceId(value) : value; foreach (var item in val.ToDictionary()) { args.Add(item.Key); args.Add(item.Value); } } return(Execute(rc => rc.Execute <String>("XADD", args.ToArray()), true)); }
/// <summary>生产添加</summary> /// <param name="value">消息</param> /// <returns></returns> public Int32 Add(T value) { using var span = Redis.Tracer?.NewSpan($"redismq:Add:{Key}", value); var val = AttachTraceId ? Redis.AttachTraceId(value) : value; return(Execute(rc => rc.Execute <Int32>("LPUSH", Key, val), true)); }
/// <summary>批量生产添加</summary> /// <param name="values">消息集合</param> /// <returns>返回插入后的LIST长度</returns> public Int32 Add(params T[] values) { if (values == null || values.Length == 0) { return(0); } using var span = Redis.Tracer?.NewSpan($"redismq:AddReliable:{TraceName}", values); var args = new List <Object> { Key }; foreach (var item in values) { if (AttachTraceId) { args.Add(Redis.AttachTraceId(item)); } else { args.Add(item); } } var rs = 0; for (var i = 0; i <= RetryTimesWhenSendFailed; i++) { // 返回插入后的LIST长度。Redis执行命令不会失败,因此正常插入不应该返回0,如果返回了0或者空,可能是中间代理出了问题 rs = Execute(rc => rc.Execute <Int32>("LPUSH", args.ToArray()), true); if (rs > 0) { return(rs); } span?.SetError(new RedisException($"发布到队列[{Topic}]失败!"), null); if (i < RetryTimesWhenSendFailed) { Thread.Sleep(RetryIntervalWhenSendFailed); } } ValidWhenSendFailed(span); return(rs); }
/// <summary>生产添加</summary> /// <param name="value">消息</param> /// <returns></returns> public Int32 Add(T value) { using var span = Redis.Tracer?.NewSpan($"redismq:Add:{TraceName}", value); var val = AttachTraceId ? Redis.AttachTraceId(value) : value; // 返回插入后的LIST长度 var rs = Execute(rc => rc.Execute <Int32>("LPUSH", Key, val), true); if (rs <= 0 && ThrowOnFailure) { var ex = new RedisException($"发布到队列[{Topic}]失败!"); span?.SetError(ex, null); throw ex; } return(rs); }
/// <summary>批量生产添加</summary> /// <param name="values">消息集合</param> /// <returns></returns> public Int32 Add(params T[] values) { using var span = Redis.Tracer?.NewSpan($"redismq:Add:{TraceName}", values); var args = new List <Object> { Key }; foreach (var item in values) { if (AttachTraceId) { args.Add(Redis.AttachTraceId(item)); } else { args.Add(item); } } return(Execute(rc => rc.Execute <Int32>("LPUSH", args.ToArray()), true)); }
/// <summary>批量生产添加</summary> /// <param name="values">消息集合</param> /// <returns>返回插入后的LIST长度</returns> public Int32 Add(params T[] values) { if (values == null || values.Length == 0) { return(0); } using var span = Redis.Tracer?.NewSpan($"redismq:AddReliable:{TraceName}", values); var args = new List <Object> { Key }; foreach (var item in values) { if (AttachTraceId) { args.Add(Redis.AttachTraceId(item)); } else { args.Add(item); } } // 返回插入后的LIST长度。Redis执行命令不会失败,因此正常插入不应该返回0,如果返回了0或者服务,可能是中间代理出了问题 var rs = Execute(rc => rc.Execute <Int32>("LPUSH", args.ToArray()), true); if (rs <= 0 && ThrowOnFailure) { var ex = new RedisException($"发布到队列[{Topic}]失败!"); span?.SetError(ex, null); throw ex; } return(rs); }
/// <summary>生产添加</summary> /// <param name="value">消息体</param> /// <param name="msgId">消息ID</param> /// <returns>返回消息ID</returns> public String Add(T value, String msgId = null) { if (value == null) { throw new ArgumentNullException(nameof(value)); } using var span = Redis.Tracer?.NewSpan($"redismq:AddStream:{TraceName}", value); // 自动修剪超长部分,每1000次生产,修剪一次 if (_count <= 0) { _count = Count; } Interlocked.Increment(ref _count); var args = new List <Object> { Key }; if (MaxLenngth > 0 && _count % 1000 == 0) { _count = Count + 1; args.Add("maxlen"); args.Add("~"); args.Add(MaxLenngth); } // *号表示服务器自动生成ID args.Add(msgId.IsNullOrEmpty() ? "*" : msgId); // 数组和复杂对象字典,分开处理 if (Type.GetTypeCode(value.GetType()) != TypeCode.Object) { //throw new ArgumentOutOfRangeException(nameof(value), "消息体必须是复杂对象!"); args.Add(PrimitiveKey); args.Add(value); } else if (value.GetType().IsArray) { foreach (var item in (value as Array)) { args.Add(item); } } else { // 在消息体内注入TraceId,用于构建调用链 var val = AttachTraceId ? Redis.AttachTraceId(value) : value; foreach (var item in val.ToDictionary()) { args.Add(item.Key); args.Add(item.Value); } } var rs = ""; for (var i = 0; i <= RetryTimesWhenSendFailed; i++) { rs = Execute(rc => rc.Execute <String>("XADD", args.ToArray()), true); if (!rs.IsNullOrEmpty()) { return(rs); } span?.SetError(new RedisException($"发布到队列[{Topic}]失败!"), null); if (i < RetryTimesWhenSendFailed) { Thread.Sleep(RetryIntervalWhenSendFailed); } } ValidWhenSendFailed(span); return(rs); }