Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions rocketmq-v5-client-spring-boot-samples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-v5-client-spring-boot-samples</artifactId>
<packaging>pom</packaging>
<version>2.3.2-SNAPSHOT</version>
<version>2.3.6-SNAPSHOT</version>

<name>rocketmq-v5-client-spring-boot-samples</name>
<description>rocketmq-v5-client-spring-boot-samples</description>
Expand All @@ -35,7 +35,9 @@
<module>rocketmq-v5-client-producer-simple-demo</module>
<module>rocketmq-v5-client-consumer-simple-demo</module>
<module>rocketmq-v5-client-consumer-push-simple-demo</module>
</modules>
<module>rocketmq-v5-client-consume-simple-subscribe-muliti-topic-demo</module>
<module>rocketmq-v5-client-consume-simple-subscribe-muliti-topic-demo</module>
</modules>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-v5-client-spring-boot-samples</artifactId>
<version>2.3.2-SNAPSHOT</version>
<version>2.3.6-SNAPSHOT</version>
</parent>
<artifactId>rocketmq-v5-client-consume-acl-demo</artifactId>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-v5-client-spring-boot-samples</artifactId>
<version>2.3.2-SNAPSHOT</version>
<version>2.3.6-SNAPSHOT</version>
</parent>

<artifactId>rocketmq-v5-client-consume-demo</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-v5-client-spring-boot-samples</artifactId>
<version>2.3.6-SNAPSHOT</version>
</parent>

<artifactId>rocketmq-v5-client-consume-simple-subscribe-muliti-topic-demo</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.rocketmq.springboot;

import org.apache.rocketmq.client.apis.message.MessageView;
import org.apache.rocketmq.client.core.RocketMQClientTemplate;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.List;

@SpringBootApplication
public class V5SimpleConsumerConsumerApplication implements CommandLineRunner {
@Resource
private RocketMQClientTemplate rocketMQClientTemplate;

public static void main(String[] args) {
SpringApplication.run(V5SimpleConsumerConsumerApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
for (int i = 0; i < 10; i++) {
List<MessageView> messageList = rocketMQClientTemplate.receive(10, Duration.ofSeconds(60));
System.out.println(messageList);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

rocketmq.simple-consumer.endpoints=localhost:8081
rocketmq.simple-consumer.consumer-group=localhost:8081
rocketmq.simple-consumer.subscription-expressions.demo-topic.tag=tagA
rocketmq.simple-consumer.subscription-expressions.demo-topic.filter-expression-type=tag
rocketmq.simple-consumer.subscription-expressions.demo-topic2.tag=tagB
rocketmq.simple-consumer.subscription-expressions.demo-topic2.filter-expression-type=tag
#rocketmq.simple-consumer.access-key=
#rocketmq.simple-consumer.secret-key=

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-v5-client-spring-boot-samples</artifactId>
<version>2.3.2-SNAPSHOT</version>
<version>2.3.6-SNAPSHOT</version>
</parent>

<artifactId>rocketmq-v5-client-consumer-push-simple-demo</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-v5-client-spring-boot-samples</artifactId>
<version>2.3.2-SNAPSHOT</version>
<version>2.3.6-SNAPSHOT</version>
</parent>

<artifactId>rocketmq-v5-client-consumer-simple-demo</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-v5-client-spring-boot-samples</artifactId>
<version>2.3.2-SNAPSHOT</version>
<version>2.3.6-SNAPSHOT</version>
</parent>

<artifactId>rocketmq-v5-client-producer-acl-demo</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-v5-client-spring-boot-samples</artifactId>
<version>2.3.2-SNAPSHOT</version>
<version>2.3.6-SNAPSHOT</version>
</parent>

<artifactId>rocketmq-v5-client-producer-demo</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-v5-client-spring-boot-samples</artifactId>
<version>2.3.2-SNAPSHOT</version>
<version>2.3.6-SNAPSHOT</version>
</parent>

<artifactId>rocketmq-v5-client-producer-simple-demo</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import java.time.Duration;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;

@Configuration
Expand Down Expand Up @@ -105,7 +106,6 @@ public SimpleConsumerBuilder simpleConsumerBuilder(RocketMQProperties rocketMQPr
RocketMQProperties.SimpleConsumer simpleConsumer = rocketMQProperties.getSimpleConsumer();
final ClientServiceProvider provider = ClientServiceProvider.loadService();
String consumerGroup = simpleConsumer.getConsumerGroup();
FilterExpression filterExpression = RocketMQUtil.createFilterExpression(simpleConsumer.getTag(), simpleConsumer.getFilterExpressionType());
ClientConfiguration clientConfiguration = RocketMQUtil.createConsumerClientConfiguration(simpleConsumer);
SimpleConsumerBuilder simpleConsumerBuilder = provider.newSimpleConsumerBuilder()
.setClientConfiguration(clientConfiguration);
Expand All @@ -116,9 +116,16 @@ public SimpleConsumerBuilder simpleConsumerBuilder(RocketMQProperties rocketMQPr
if (StringUtils.hasLength(consumerGroup)) {
simpleConsumerBuilder.setConsumerGroup(consumerGroup);
}

// Set the subscription for the consumer.
if (Objects.nonNull(filterExpression)) {
simpleConsumerBuilder.setSubscriptionExpressions(Collections.singletonMap(simpleConsumer.getTopic(), filterExpression));
if (simpleConsumer.getSubscriptionExpressions().isEmpty()) {
FilterExpression filterExpression = RocketMQUtil.createFilterExpression(simpleConsumer.getTag(), simpleConsumer.getFilterExpressionType());
if (Objects.nonNull(filterExpression)) {
simpleConsumerBuilder.setSubscriptionExpressions(Collections.singletonMap(simpleConsumer.getTopic(), filterExpression));
}
} else {
Map<String, FilterExpression> subscriptionExpressions = RocketMQUtil.createSubscriptionExpressions(simpleConsumer.getSubscriptionExpressions());
simpleConsumerBuilder.setSubscriptionExpressions(subscriptionExpressions);
}
return simpleConsumerBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Map;

@SuppressWarnings("WeakerAccess")
@ConfigurationProperties(prefix = "rocketmq")
public class RocketMQProperties {
Expand Down Expand Up @@ -211,6 +213,11 @@ public static class SimpleConsumer {

private String namespace = "";

/**
* key is topic
*/
private Map<String, FilterExpression> subscriptionExpressions;

public String getAccessKey() {
return accessKey;
}
Expand Down Expand Up @@ -299,6 +306,14 @@ public void setNamespace(String namespace) {
this.namespace = namespace;
}

public Map<String, FilterExpression> getSubscriptionExpressions() {
return subscriptionExpressions;
}

public void setSubscriptionExpressions(Map<String, FilterExpression> subscriptionExpressions) {
this.subscriptionExpressions = subscriptionExpressions;
}

@Override
public String toString() {
return "SimpleConsumer{" +
Expand All @@ -315,4 +330,25 @@ public String toString() {
}
}

public static class FilterExpression {
private String tag;

private String filterExpressionType;

public String getTag() {
return tag;
}

public void setTag(String tag) {
this.tag = tag;
}

public String getFilterExpressionType() {
return filterExpressionType;
}

public void setFilterExpressionType(String filterExpressionType) {
this.filterExpressionType = filterExpressionType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

import java.nio.charset.Charset;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class RocketMQUtil {
Expand Down Expand Up @@ -184,4 +186,14 @@ public static FilterExpression createFilterExpression(String tag, String type) {
FilterExpression filterExpression = new FilterExpression(tag, filterExpressionType);
return filterExpression;
}

public static Map<String, FilterExpression> createSubscriptionExpressions(Map<String, RocketMQProperties.FilterExpression> map) {
Map<String, FilterExpression> subscriptionExpressions = new HashMap<>();
map.forEach((topic, expression) -> {
FilterExpressionType filterExpressionType = "tag".equalsIgnoreCase(expression.getFilterExpressionType()) ? FilterExpressionType.TAG : FilterExpressionType.SQL92;
FilterExpression filterExpression = new FilterExpression(expression.getTag(), filterExpressionType);
subscriptionExpressions.put(topic, filterExpression);
});
return subscriptionExpressions;
}
}
Loading