Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import java.util.Objects;
import java.util.stream.Collectors;


@Configuration
public class ExtConsumerResetConfiguration implements ApplicationContextAware, SmartInitializingSingleton {
private static final Logger log = LoggerFactory.getLogger(ExtConsumerResetConfiguration.class);
Expand All @@ -61,7 +60,7 @@ public class ExtConsumerResetConfiguration implements ApplicationContextAware, S
private RocketMQMessageConverter rocketMQMessageConverter;

public ExtConsumerResetConfiguration(RocketMQMessageConverter rocketMQMessageConverter,
ConfigurableEnvironment environment, RocketMQProperties rocketMQProperties) {
ConfigurableEnvironment environment, RocketMQProperties rocketMQProperties) {
this.rocketMQMessageConverter = rocketMQMessageConverter;
this.environment = environment;
this.rocketMQProperties = rocketMQProperties;
Expand All @@ -75,9 +74,9 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
@Override
public void afterSingletonsInstantiated() {
Map<String, Object> beans = this.applicationContext
.getBeansWithAnnotation(org.apache.rocketmq.client.annotation.ExtConsumerResetConfiguration.class)
.entrySet().stream().filter(entry -> !ScopedProxyUtils.isScopedTarget(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
.getBeansWithAnnotation(org.apache.rocketmq.client.annotation.ExtConsumerResetConfiguration.class)
.entrySet().stream().filter(entry -> !ScopedProxyUtils.isScopedTarget(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
beans.forEach(this::registerTemplate);
}

Expand All @@ -93,20 +92,25 @@ private void registerTemplate(String beanName, Object bean) {

SimpleConsumerBuilder consumerBuilder = null;
SimpleConsumer simpleConsumer = null;
SimpleConsumerInfo simpleConsumerInfo = null;

try {
consumerBuilder = createConsumer(annotation);
simpleConsumer = consumerBuilder.build();
final ClientServiceProvider provider = ClientServiceProvider.loadService();
SimpleConsumerBuilder simpleConsumerBuilder = provider.newSimpleConsumerBuilder();
simpleConsumerInfo = createConsumer(annotation, simpleConsumerBuilder);
} catch (Exception e) {
log.error("Failed to startup SimpleConsumer for RocketMQTemplate {}", beanName, e);
}
RocketMQClientTemplate rocketMQTemplate = (RocketMQClientTemplate) bean;
rocketMQTemplate.setSimpleConsumerBuilder(consumerBuilder);
rocketMQTemplate.setSimpleConsumer(simpleConsumer);
rocketMQTemplate.setMessageConverter(rocketMQMessageConverter.getMessageConverter());
log.info("Set real simpleConsumer to :{} {}", beanName, annotation.value());
log.info("Set real simpleConsumer {} to {}", simpleConsumerInfo, beanName);
}

private SimpleConsumerBuilder createConsumer(org.apache.rocketmq.client.annotation.ExtConsumerResetConfiguration annotation) {
private SimpleConsumerInfo createConsumer(
org.apache.rocketmq.client.annotation.ExtConsumerResetConfiguration annotation,
SimpleConsumerBuilder simpleConsumerBuilder) {
RocketMQProperties.SimpleConsumer simpleConsumer = rocketMQProperties.getSimpleConsumer();
String consumerGroupName = resolvePlaceholders(annotation.consumerGroup(), simpleConsumer.getConsumerGroup());
String topicName = resolvePlaceholders(annotation.topic(), simpleConsumer.getTopic());
Expand All @@ -121,10 +125,8 @@ private SimpleConsumerBuilder createConsumer(org.apache.rocketmq.client.annotati
Boolean sslEnabled = simpleConsumer.isSslEnabled();
Assert.hasText(topicName, "[topic] must not be null");
ClientConfiguration clientConfiguration = RocketMQUtil.createClientConfiguration(accessKey, secretKey, endPoints, requestTimeout, sslEnabled, namespace);
final ClientServiceProvider provider = ClientServiceProvider.loadService();
FilterExpression filterExpression = RocketMQUtil.createFilterExpression(tag, filterExpressionType);
Duration duration = Duration.ofSeconds(awaitDuration);
SimpleConsumerBuilder simpleConsumerBuilder = provider.newSimpleConsumerBuilder();
simpleConsumerBuilder.setClientConfiguration(clientConfiguration);
if (StringUtils.hasLength(consumerGroupName)) {
simpleConsumerBuilder.setConsumerGroup(consumerGroupName);
Expand All @@ -133,7 +135,8 @@ private SimpleConsumerBuilder createConsumer(org.apache.rocketmq.client.annotati
if (Objects.nonNull(filterExpression)) {
simpleConsumerBuilder.setSubscriptionExpressions(Collections.singletonMap(topicName, filterExpression));
}
return simpleConsumerBuilder;

return new SimpleConsumerInfo(consumerGroupName, topicName, endPoints, namespace, tag, filterExpressionType, requestTimeout, awaitDuration, sslEnabled);
}

private String resolvePlaceholders(String text, String defaultValue) {
Expand All @@ -142,12 +145,59 @@ private String resolvePlaceholders(String text, String defaultValue) {
}

private void validate(org.apache.rocketmq.client.annotation.ExtConsumerResetConfiguration annotation,
GenericApplicationContext genericApplicationContext) {
GenericApplicationContext genericApplicationContext) {
if (genericApplicationContext.isBeanNameInUse(annotation.value())) {
throw new BeanDefinitionValidationException(
String.format("Bean {} has been used in Spring Application Context, " +
"please check the @ExtRocketMQConsumerConfiguration",
annotation.value()));
String.format("Bean {} has been used in Spring Application Context, " +
"please check the @ExtRocketMQConsumerConfiguration",
annotation.value()));
}
}

static class SimpleConsumerInfo {
String consumerGroup;

String topicName;

String endPoints;

String namespace;

String tag;

String filterExpressionType;

Duration requestTimeout;

int awaitDuration;

Boolean sslEnabled;

public SimpleConsumerInfo(String consumerGroupName, String topicName, String endPoints, String namespace,
String tag, String filterExpressionType, Duration requestTimeout, int awaitDuration, Boolean sslEnabled) {
this.consumerGroup = consumerGroupName;
this.topicName = topicName;
this.endPoints = endPoints;
this.namespace = namespace;
this.tag = tag;
this.filterExpressionType = filterExpressionType;
this.requestTimeout = requestTimeout;
this.awaitDuration = awaitDuration;
this.sslEnabled = sslEnabled;
}

@Override public String toString() {
return "SimpleConsumerInfo{" +
"consumerGroup='" + consumerGroup + '\'' +
", topicName='" + topicName + '\'' +
", endPoints='" + endPoints + '\'' +
", namespace='" + namespace + '\'' +
", tag='" + tag + '\'' +
", filterExpressionType='" + filterExpressionType + '\'' +
", requestTimeout(seconds)=" + requestTimeout.getSeconds() +
", awaitDuration=" + awaitDuration +
", sslEnabled=" + sslEnabled +
'}';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ private void registerTemplate(String beanName, Object bean) {
RocketMQClientTemplate rocketMQTemplate = (RocketMQClientTemplate) bean;
rocketMQTemplate.setProducerBuilder(producerBuilder);
rocketMQTemplate.setMessageConverter(rocketMQMessageConverter.getMessageConverter());
log.info("Set real producerBuilder to :{} {}", beanName, annotation.value());
String topic = environment.resolvePlaceholders(annotation.topic());
log.info("Set real producer to {} using topic {}", beanName, topic);
}

private ProducerBuilder createProducer(ExtProducerResetConfiguration annotation) {
Expand Down