@@ -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 )
0 commit comments