1616 */
1717package org .apache .rocketmq .client .autoconfigure ;
1818
19+ import com .alibaba .fastjson .JSON ;
1920import org .apache .rocketmq .client .support .RocketMQMessageConverter ;
2021import org .apache .rocketmq .client .support .RocketMQUtil ;
2122import org .apache .rocketmq .client .apis .ClientConfiguration ;
4041import org .springframework .util .StringUtils ;
4142
4243import java .time .Duration ;
43- import java .util .Collections ;
44+ import java .util .HashMap ;
4445import java .util .Map ;
4546import java .util .Objects ;
4647import java .util .stream .Collectors ;
@@ -110,30 +111,44 @@ private SimpleConsumerInfo createConsumer(
110111 SimpleConsumerBuilder simpleConsumerBuilder ) {
111112 RocketMQProperties .SimpleConsumer simpleConsumer = rocketMQProperties .getSimpleConsumer ();
112113 String consumerGroupName = resolvePlaceholders (annotation .consumerGroup (), simpleConsumer .getConsumerGroup ());
113- String topicName = resolvePlaceholders (annotation .topic (), simpleConsumer .getTopic ());
114114 String accessKey = resolvePlaceholders (annotation .accessKey (), simpleConsumer .getAccessKey ());
115115 String secretKey = resolvePlaceholders (annotation .secretKey (), simpleConsumer .getSecretKey ());
116116 String endPoints = resolvePlaceholders (annotation .endpoints (), simpleConsumer .getEndpoints ());
117117 String namespace = resolvePlaceholders (annotation .namespace (), simpleConsumer .getNamespace ());
118- String tag = resolvePlaceholders (annotation .tag (), simpleConsumer .getTag ());
119- String filterExpressionType = resolvePlaceholders (annotation .filterExpressionType (), simpleConsumer .getFilterExpressionType ());
120118 Duration requestTimeout = Duration .ofSeconds (annotation .requestTimeout ());
121119 int awaitDuration = annotation .awaitDuration ();
122120 Boolean sslEnabled = simpleConsumer .isSslEnabled ();
123- Assert .hasText (topicName , "[topic] must not be null" );
124121 ClientConfiguration clientConfiguration = RocketMQUtil .createClientConfiguration (accessKey , secretKey , endPoints , requestTimeout , sslEnabled , namespace );
125- FilterExpression filterExpression = RocketMQUtil .createFilterExpression (tag , filterExpressionType );
126122 Duration duration = Duration .ofSeconds (awaitDuration );
127123 simpleConsumerBuilder .setClientConfiguration (clientConfiguration );
128124 if (StringUtils .hasLength (consumerGroupName )) {
129125 simpleConsumerBuilder .setConsumerGroup (consumerGroupName );
130126 }
131127 simpleConsumerBuilder .setAwaitDuration (duration );
132- if (Objects .nonNull (filterExpression )) {
133- simpleConsumerBuilder .setSubscriptionExpressions (Collections .singletonMap (topicName , filterExpression ));
128+
129+ Map <String , FilterExpression > subscriptionExpressions = new HashMap <>();
130+ org .apache .rocketmq .client .annotation .ExtConsumerResetConfiguration .FilterExpression [] filterExpressions = annotation .subscriptionExpressions ();
131+ if (filterExpressions .length > 0 ) {
132+ for (org .apache .rocketmq .client .annotation .ExtConsumerResetConfiguration .FilterExpression expression : filterExpressions ) {
133+ Assert .hasText (expression .topic (), "[topic] must not be null" );
134+ FilterExpression filterExpression = RocketMQUtil .createFilterExpression (expression .tag (), expression .filterExpressionType ());
135+ if (Objects .nonNull (filterExpression )) {
136+ subscriptionExpressions .put (expression .topic (), filterExpression );
137+ }
138+ }
139+ } else {
140+ String topicName = resolvePlaceholders (annotation .topic (), simpleConsumer .getTopic ());
141+ Assert .hasText (topicName , "[topic] must not be null" );
142+ String tag = resolvePlaceholders (annotation .tag (), simpleConsumer .getTag ());
143+ String filterExpressionType = resolvePlaceholders (annotation .filterExpressionType (), simpleConsumer .getFilterExpressionType ());
144+ FilterExpression filterExpression = RocketMQUtil .createFilterExpression (tag , filterExpressionType );
145+ if (Objects .nonNull (filterExpression )) {
146+ subscriptionExpressions .put (topicName , filterExpression );
147+ }
134148 }
149+ simpleConsumerBuilder .setSubscriptionExpressions (subscriptionExpressions );
135150
136- return new SimpleConsumerInfo (consumerGroupName , topicName , endPoints , namespace , tag , filterExpressionType , requestTimeout , awaitDuration , sslEnabled );
151+ return new SimpleConsumerInfo (consumerGroupName , endPoints , namespace , requestTimeout , awaitDuration , sslEnabled , subscriptionExpressions );
137152 }
138153
139154 private String resolvePlaceholders (String text , String defaultValue ) {
@@ -144,47 +159,40 @@ private String resolvePlaceholders(String text, String defaultValue) {
144159 static class SimpleConsumerInfo {
145160 String consumerGroup ;
146161
147- String topicName ;
148-
149162 String endPoints ;
150163
151164 String namespace ;
152165
153- String tag ;
154-
155- String filterExpressionType ;
156-
157166 Duration requestTimeout ;
158167
159168 int awaitDuration ;
160169
161170 Boolean sslEnabled ;
162171
163- public SimpleConsumerInfo (String consumerGroupName , String topicName , String endPoints , String namespace ,
164- String tag , String filterExpressionType , Duration requestTimeout , int awaitDuration , Boolean sslEnabled ) {
172+ Map <String , FilterExpression > subscriptionExpressions ;
173+
174+ public SimpleConsumerInfo (String consumerGroupName , String endPoints , String namespace , Duration requestTimeout ,
175+ int awaitDuration , Boolean sslEnabled , Map <String , FilterExpression > subscriptionExpressions ) {
165176 this .consumerGroup = consumerGroupName ;
166- this .topicName = topicName ;
167177 this .endPoints = endPoints ;
168178 this .namespace = namespace ;
169- this .tag = tag ;
170- this .filterExpressionType = filterExpressionType ;
171179 this .requestTimeout = requestTimeout ;
172180 this .awaitDuration = awaitDuration ;
173181 this .sslEnabled = sslEnabled ;
182+ this .subscriptionExpressions = subscriptionExpressions ;
174183 }
175184
176- @ Override public String toString () {
185+ @ Override
186+ public String toString () {
177187 return "SimpleConsumerInfo{" +
178- "consumerGroup='" + consumerGroup + '\'' +
179- ", topicName='" + topicName + '\'' +
180- ", endPoints='" + endPoints + '\'' +
181- ", namespace='" + namespace + '\'' +
182- ", tag='" + tag + '\'' +
183- ", filterExpressionType='" + filterExpressionType + '\'' +
184- ", requestTimeout(seconds)=" + requestTimeout .getSeconds () +
185- ", awaitDuration=" + awaitDuration +
186- ", sslEnabled=" + sslEnabled +
187- '}' ;
188+ "consumerGroup='" + consumerGroup + '\'' +
189+ ", endPoints='" + endPoints + '\'' +
190+ ", namespace='" + namespace + '\'' +
191+ ", requestTimeout=" + requestTimeout +
192+ ", awaitDuration=" + awaitDuration +
193+ ", sslEnabled=" + sslEnabled +
194+ ", subscriptionExpressions=" + JSON .toJSONString (subscriptionExpressions ) +
195+ '}' ;
188196 }
189197 }
190198}
0 commit comments