Skip to content

Commit bc8cb83

Browse files
committed
support simple consumer supports subscribing to multiple topics
1 parent ed63816 commit bc8cb83

7 files changed

Lines changed: 107 additions & 36 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.rocketmq.springboot;
18+
19+
import org.apache.rocketmq.client.annotation.ExtConsumerResetConfiguration;
20+
import org.apache.rocketmq.client.core.RocketMQClientTemplate;
21+
22+
@ExtConsumerResetConfiguration(subscriptionExpressions = {
23+
@ExtConsumerResetConfiguration.FilterExpression(topic = "demo-topic", tag = "tagA", filterExpressionType = "tag"),
24+
@ExtConsumerResetConfiguration.FilterExpression(topic = "demo-topic2", tag = "tagB", filterExpressionType = "tag")
25+
})
26+
public class ExtRocketMQTemplate extends RocketMQClientTemplate {
27+
}

rocketmq-v5-client-spring-boot-samples/rocketmq-v5-client-consume-simple-subscribe-muliti-topic-demo/src/main/java/org/apache/rocketmq/springboot/V5SimpleConsumerConsumerApplication.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,20 @@ public class V5SimpleConsumerConsumerApplication implements CommandLineRunner {
3232
@Resource
3333
private RocketMQClientTemplate rocketMQClientTemplate;
3434

35+
@Resource
36+
private ExtRocketMQTemplate extRocketMQTemplate;
37+
3538
public static void main(String[] args) {
3639
SpringApplication.run(V5SimpleConsumerConsumerApplication.class, args);
3740
}
3841

3942
@Override
4043
public void run(String... args) throws Exception {
41-
for (int i = 0; i < 10; i++) {
42-
List<MessageView> messageList = rocketMQClientTemplate.receive(10, Duration.ofSeconds(60));
44+
while (true){
45+
List<MessageView> messageList = extRocketMQTemplate.receive(10, Duration.ofSeconds(10));
46+
System.out.println(messageList);
47+
48+
messageList = rocketMQClientTemplate.receive(10, Duration.ofSeconds(10));
4349
System.out.println(messageList);
4450
}
4551
}

rocketmq-v5-client-spring-boot-samples/rocketmq-v5-client-consume-simple-subscribe-muliti-topic-demo/src/main/resources/application.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
rocketmq.simple-consumer.endpoints=localhost:8081
17-
rocketmq.simple-consumer.consumer-group=localhost:8081
16+
rocketmq.simple-consumer.endpoints=localhost:8080
17+
rocketmq.simple-consumer.consumer-group=test-group
1818
rocketmq.simple-consumer.subscription-expressions.demo-topic.tag=tagA
1919
rocketmq.simple-consumer.subscription-expressions.demo-topic.filter-expression-type=tag
2020
rocketmq.simple-consumer.subscription-expressions.demo-topic2.tag=tagB
2121
rocketmq.simple-consumer.subscription-expressions.demo-topic2.filter-expression-type=tag
2222
#rocketmq.simple-consumer.access-key=
2323
#rocketmq.simple-consumer.secret-key=
24+
#rocketmq.simple-consumer.namespace=
2425

rocketmq-v5-client-spring-boot/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@
9393
<artifactId>junit</artifactId>
9494
<scope>test</scope>
9595
</dependency>
96+
<dependency>
97+
<groupId>com.alibaba</groupId>
98+
<artifactId>fastjson</artifactId>
99+
</dependency>
96100
</dependencies>
97101

98102

rocketmq-v5-client-spring-boot/src/main/java/org/apache/rocketmq/client/annotation/ExtConsumerResetConfiguration.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,28 @@
9191
* The namespace of consumer.
9292
*/
9393
String namespace() default "";
94+
95+
/**
96+
* subscribing to multiple topics
97+
*/
98+
FilterExpression[] subscriptionExpressions() default {};
99+
100+
@Retention(RetentionPolicy.RUNTIME)
101+
@Target({})
102+
@interface FilterExpression {
103+
/**
104+
* Topic name of consumer.
105+
*/
106+
String topic();
107+
108+
/**
109+
* Tag of consumer.
110+
*/
111+
String tag() default "*";
112+
113+
/**
114+
* The type of filter expression
115+
*/
116+
String filterExpressionType() default "tag";
117+
}
94118
}

rocketmq-v5-client-spring-boot/src/main/java/org/apache/rocketmq/client/autoconfigure/ExtConsumerResetConfiguration.java

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.rocketmq.client.autoconfigure;
1818

19+
import com.alibaba.fastjson.JSON;
1920
import org.apache.rocketmq.client.support.RocketMQMessageConverter;
2021
import org.apache.rocketmq.client.support.RocketMQUtil;
2122
import org.apache.rocketmq.client.apis.ClientConfiguration;
@@ -40,7 +41,7 @@
4041
import org.springframework.util.StringUtils;
4142

4243
import java.time.Duration;
43-
import java.util.Collections;
44+
import java.util.HashMap;
4445
import java.util.Map;
4546
import java.util.Objects;
4647
import 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
}

rocketmq-v5-client-spring-boot/src/main/java/org/apache/rocketmq/client/autoconfigure/RocketMQAutoConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.context.annotation.Configuration;
4343
import org.springframework.context.annotation.Import;
4444
import org.springframework.util.Assert;
45+
import org.springframework.util.CollectionUtils;
4546
import org.springframework.util.StringUtils;
4647

4748
import java.time.Duration;
@@ -118,7 +119,7 @@ public SimpleConsumerBuilder simpleConsumerBuilder(RocketMQProperties rocketMQPr
118119
}
119120

120121
// Set the subscription for the consumer.
121-
if (simpleConsumer.getSubscriptionExpressions().isEmpty()) {
122+
if (CollectionUtils.isEmpty(simpleConsumer.getSubscriptionExpressions())) {
122123
FilterExpression filterExpression = RocketMQUtil.createFilterExpression(simpleConsumer.getTag(), simpleConsumer.getFilterExpressionType());
123124
if (Objects.nonNull(filterExpression)) {
124125
simpleConsumerBuilder.setSubscriptionExpressions(Collections.singletonMap(simpleConsumer.getTopic(), filterExpression));

0 commit comments

Comments
 (0)