Skip to content

Commit 7e5154d

Browse files
committed
2 parents 8d5e195 + 2166416 commit 7e5154d

4 files changed

Lines changed: 72 additions & 19 deletions

File tree

NewLife.RocketMQ/Consumer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public class Consumer : MqBase
4848
/// <summary>消息模型。广播/集群</summary>
4949
public MessageModels MessageModel { get; set; } = MessageModels.Clustering;
5050

51+
/// <summary>消费类型。CONSUME_PASSIVELY/CONSUME_ACTIVELY</summary>
52+
public String ConsumeType { get; set; } = "CONSUME_PASSIVELY";
53+
5154
/// <summary>消费委托</summary>
5255
public Func<MessageQueue, MessageExt[], Boolean> OnConsume;
5356

@@ -111,6 +114,7 @@ protected override void OnStart()
111114
ConsumeFromWhere = FromLastOffset ? "CONSUME_FROM_LAST_OFFSET" : "CONSUME_FROM_FIRST_OFFSET",
112115
MessageModel = MessageModel.ToString().ToUpper(),
113116
SubscriptionDataSet = new[] { sd },
117+
ConsumeType = ConsumeType,
114118
};
115119

116120
list = new[] { cd };

NewLife.RocketMQ/MessageTrace/MessageTraceHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void ExecuteHookBefore(SendMessageContext context)
3636
public void ExecuteHookAfter(SendMessageContext context)
3737
{
3838

39-
if (context.Message.Topic.Equals("RMQ_SYS_TRACE_TOPIC"))
39+
if (context.Message?.Topic?.Equals("RMQ_SYS_TRACE_TOPIC") == true)
4040
{
4141
return;
4242
}

NewLife.RocketMQ/Protocol/Message.cs

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ public class Message
1414
public String Topic { get; set; }
1515

1616
/// <summary>标签</summary>
17-
public String Tags { get; set; }
17+
public String Tags
18+
{
19+
get => Properties.TryGetValue("TAGS", out var str) ? str : null;
20+
set => Properties["TAGS"] = value;
21+
}
1822

1923
/// <summary>键</summary>
20-
public String Keys { get; set; }
24+
public String Keys
25+
{
26+
get => Properties.TryGetValue("KEYS", out var str) ? str : null;
27+
set => Properties["KEYS"] = value;
28+
}
2129

2230
/// <summary>标记</summary>
2331
public Int32 Flag { get; set; }
@@ -31,16 +39,37 @@ public class Message
3139
public String BodyString { get => _BodyString ??= Body?.ToStr(); set => Body = (_BodyString = value)?.GetBytes(); }
3240

3341
/// <summary>等待存储消息</summary>
34-
public Boolean WaitStoreMsgOK { get; set; } = true;
42+
public Boolean WaitStoreMsgOK
43+
{
44+
get => Properties.TryGetValue("WAIT", out var str) ? str.ToBoolean() : true;
45+
set => Properties["WAIT"] = value.ToString();
46+
}
3547

3648
/// <summary>延迟时间等级</summary>
37-
public Int32 DelayTimeLevel { get; set; }
49+
public Int32 DelayTimeLevel
50+
{
51+
get => Properties.TryGetValue("DELAY", out var str) ? str.ToInt() : 0;
52+
set => Properties["DELAY"] = value.ToString();
53+
}
3854

3955
/// <summary>事务标识</summary>
40-
public String TransactionId { get; set; }
56+
public String TransactionId
57+
{
58+
get => Properties.TryGetValue("UNIQ_KEY", out var str) ? str : null;
59+
set => Properties["UNIQ_KEY"] = value;
60+
}
61+
62+
/// <summary>附加属性</summary>
63+
public IDictionary<String, String> Properties { get; set; }
4164
#endregion
4265

4366
#region 构造
67+
/// <summary>实例化</summary>
68+
public Message()
69+
{
70+
Properties = new NullableDictionary<String, String>(StringComparer.OrdinalIgnoreCase);
71+
}
72+
4473
/// <summary>友好字符串</summary>
4574
/// <returns></returns>
4675
public override String ToString() => Body != null && Body.Length > 0 ? BodyString : base.ToString();
@@ -77,29 +106,53 @@ public String GetProperties()
77106
{
78107
var sb = Pool.StringBuilder.Get();
79108

80-
if (!TransactionId.IsNullOrEmpty()) sb.AppendFormat("{0}\u0001{1}\u0002", "UNIQ_KEY", TransactionId);
81-
sb.AppendFormat("{0}\u0001{1}\u0002", "WAIT", WaitStoreMsgOK);
82-
if (!Tags.IsNullOrEmpty()) sb.AppendFormat("{0}\u0001{1}\u0002", "TAGS", Tags);
83-
if (!Keys.IsNullOrEmpty()) sb.AppendFormat("{0}\u0001{1}\u0002", "KEYS", Keys);
84-
if (DelayTimeLevel > 0) sb.AppendFormat("{0}\u0001{1}\u0002", "DELAY", DelayTimeLevel);
109+
if (Properties != null && Properties.Count > 0)
110+
{
111+
foreach (var item in Properties)
112+
{
113+
sb.AppendFormat("{0}\u0001{1}\u0002", item.Key, item.Value);
114+
}
115+
}
85116

86117
return sb.Return(true);
87118
}
88119

120+
/// <summary>设置属性</summary>
121+
/// <param name="key"></param>
122+
/// <param name="value"></param>
123+
public void PutUserProperty(String key, String value)
124+
{
125+
if (String.IsNullOrEmpty(key)) throw new ArgumentNullException(nameof(key));
126+
if (String.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(value));
127+
128+
Properties[key] = value;
129+
}
130+
131+
/// <summary>获取属性</summary>
132+
/// <param name="key"></param>
133+
/// <returns></returns>
134+
public String GetUserProperty(String key)
135+
{
136+
Properties.TryGetValue(key, out var value);
137+
return value;
138+
}
139+
89140
/// <summary>分析字典属性</summary>
90141
/// <param name="properties"></param>
91142
public IDictionary<String, String> ParseProperties(String properties)
92143
{
93-
if (properties.IsNullOrEmpty()) return null;
144+
if (properties.IsNullOrEmpty()) return Properties;
94145

95146
var dic = SplitAsDictionary(properties, "\u0001", "\u0002");
96147

148+
Properties = dic;
149+
97150
if (TryGetAndRemove(dic, nameof(Tags), out var str)) Tags = str;
98151
if (TryGetAndRemove(dic, nameof(Keys), out str)) Keys = str;
99152
if (TryGetAndRemove(dic, "DELAY", out str)) DelayTimeLevel = str.ToInt();
100153
if (TryGetAndRemove(dic, "WAIT", out str)) WaitStoreMsgOK = str.ToBoolean();
101-
102-
return dic;
154+
155+
return Properties;
103156
}
104157

105158
private static IDictionary<String, String> SplitAsDictionary(String value, String nameValueSeparator, String separator)

NewLife.RocketMQ/Protocol/MessageExt.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ public class MessageExt : Message, IAccessor
4545
/// <summary>准备事务偏移</summary>
4646
public Int64 PreparedTransactionOffset { get; set; }
4747

48-
/// <summary>属性</summary>
49-
public IDictionary<String, String> Properties { get; set; }
50-
5148
/// <summary>消息编号</summary>
5249
public String MsgId { get; set; }
5350
#endregion
@@ -112,8 +109,7 @@ public Boolean Read(Stream stream, Object context = null)
112109

113110
var len2 = bn.Read<Int16>();
114111
var str = bn.ReadBytes(len2).ToStr();
115-
var dic = ParseProperties(str);
116-
if (dic != null && dic.Count > 0) Properties = dic;
112+
ParseProperties(str);
117113

118114
// MsgId
119115
var ms = Pool.MemoryStream.Get();

0 commit comments

Comments
 (0)