|
@@ -2,13 +2,16 @@ package com.chelvc.framework.kafka.config;
|
|
|
|
|
|
import java.lang.reflect.Type;
|
|
|
import java.util.Collection;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.Set;
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
-import lombok.RequiredArgsConstructor;
|
|
|
import com.chelvc.framework.common.util.JacksonUtils;
|
|
|
import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
+import com.chelvc.framework.common.util.StringUtils;
|
|
|
import com.chelvc.framework.kafka.context.KafkaContextHolder;
|
|
|
import com.chelvc.framework.kafka.interceptor.KafkaSessionInterceptor;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
|
|
import org.springframework.beans.BeansException;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -82,14 +85,26 @@ public class KafkaConfigurer implements KafkaListenerConfigurer {
|
|
|
);
|
|
|
|
|
|
/**
|
|
|
- * 初始化消息监听器端点主题
|
|
|
+ * 隔离消息监听器端点环境
|
|
|
*
|
|
|
* @param endpoint 消息监听器端点实例
|
|
|
*/
|
|
|
- private void initializeListenerEndpointTopic(MethodKafkaListenerEndpoint<?, ?> endpoint) {
|
|
|
+ private void isolateListenerEndpointEnvironment(MethodKafkaListenerEndpoint<?, ?> endpoint) {
|
|
|
+ if (StringUtils.nonEmpty(endpoint.getGroup())) {
|
|
|
+ endpoint.setGroup(KafkaContextHolder.isolate(endpoint.getGroup()));
|
|
|
+ }
|
|
|
+ if (StringUtils.nonEmpty(endpoint.getGroupId())) {
|
|
|
+ endpoint.setGroupId(KafkaContextHolder.isolate(endpoint.getGroupId()));
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(endpoint.getTopicPattern())) {
|
|
|
+ String pattern = endpoint.getTopicPattern().pattern();
|
|
|
+ if (StringUtils.nonEmpty(pattern)) {
|
|
|
+ endpoint.setTopicPattern(Pattern.compile(KafkaContextHolder.isolate(pattern)));
|
|
|
+ }
|
|
|
+ }
|
|
|
Collection<String> topics = endpoint.getTopics();
|
|
|
if (ObjectUtils.nonEmpty(topics)) {
|
|
|
- endpoint.setTopics(topics.stream().map(KafkaContextHolder::topic).toArray(String[]::new));
|
|
|
+ endpoint.setTopics(topics.stream().map(KafkaContextHolder::isolate).toArray(String[]::new));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -128,7 +143,7 @@ public class KafkaConfigurer implements KafkaListenerConfigurer {
|
|
|
public void registerListenerContainer(KafkaListenerEndpoint endpoint,
|
|
|
KafkaListenerContainerFactory<?> factory, boolean
|
|
|
startImmediately) {
|
|
|
- this.initializeListenerEndpointTopic((MethodKafkaListenerEndpoint<?, ?>) endpoint);
|
|
|
+ this.isolateListenerEndpointEnvironment((MethodKafkaListenerEndpoint<?, ?>) endpoint);
|
|
|
this.proxy.registerListenerContainer(endpoint, factory, startImmediately);
|
|
|
}
|
|
|
|