Skip to content

Commit

Permalink
further improvement on nullness checking
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanQingyangXu committed Dec 22, 2023
1 parent 930bbee commit 3e8f9a7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ public RetryTopicConfiguration processAnnotation(String[] topics, Method method,
if (StringUtils.hasText(annotation.autoStartDltHandler())) {
autoStartDlt = resolveExpressionAsBoolean(annotation.autoStartDltHandler(), "autoStartDltContainer");
}
return RetryTopicConfigurationBuilder.newInstance()
.maxAttempts(resolveExpressionAsInteger(annotation.attempts(), "attempts", true))
.concurrency(resolveExpressionAsInteger(annotation.concurrency(), "concurrency", false))
RetryTopicConfigurationBuilder builder = RetryTopicConfigurationBuilder.newInstance()
.customBackoff(createBackoffFromAnnotation(annotation.backoff(), this.beanFactory))
.retryTopicSuffix(resolveExpressionAsString(annotation.retryTopicSuffix(), "retryTopicSuffix"))
.dltSuffix(resolveExpressionAsString(annotation.dltTopicSuffix(), "dltTopicSuffix"))
Expand All @@ -146,8 +144,17 @@ public RetryTopicConfiguration processAnnotation(String[] topics, Method method,
.autoStartDltHandler(autoStartDlt)
.setTopicSuffixingStrategy(annotation.topicSuffixingStrategy())
.sameIntervalTopicReuseStrategy(annotation.sameIntervalTopicReuseStrategy())
.timeoutAfter(timeout)
.create(getKafkaTemplate(resolveExpressionAsString(annotation.kafkaTemplate(), "kafkaTemplate"), topics));
.timeoutAfter(timeout);

Integer attempts = resolveExpressionAsInteger(annotation.attempts(), "attempts", true);
if (attempts != null) {
builder.maxAttempts(attempts);
}
Integer concurrency = resolveExpressionAsInteger(annotation.concurrency(), "concurrency", false);
if (concurrency != null) {
builder.concurrency(concurrency);
}
return builder.create(getKafkaTemplate(resolveExpressionAsString(annotation.kafkaTemplate(), "kafkaTemplate"), topics));
}

private SleepingBackOffPolicy<?> createBackoffFromAnnotation(Backoff backoff, BeanFactory beanFactory) { // NOSONAR
Expand Down Expand Up @@ -231,8 +238,8 @@ private EndpointHandlerMethod getDltProcessor(Method listenerMethod, Object bean

private String resolveExpressionAsString(String value, String attribute) {
Object resolved = resolveExpression(value);
if (resolved instanceof String sResolved) {
return sResolved;
if (resolved instanceof String str) {
return str;
}
else if (resolved != null) {
throw new IllegalStateException(THE_OSQ + attribute + "] must resolve to a String. "
Expand All @@ -244,13 +251,13 @@ else if (resolved != null) {
private Integer resolveExpressionAsInteger(String value, String attribute, boolean required) {
Object resolved = resolveExpression(value);
Integer result = null;
if (resolved instanceof String sResolved) {
if (required || StringUtils.hasText(sResolved)) {
result = Integer.parseInt(sResolved);
if (resolved instanceof String str) {
if (required || StringUtils.hasText(str)) {
result = Integer.parseInt(str);
}
}
else if (resolved instanceof Number nResolved) {
result = nResolved.intValue();
else if (resolved instanceof Number num) {
result = num.intValue();
}
else if (resolved != null || required) {
throw new IllegalStateException(
Expand All @@ -264,13 +271,13 @@ else if (resolved != null || required) {
private Short resolveExpressionAsShort(String value, String attribute, boolean required) {
Object resolved = resolveExpression(value);
Short result = null;
if (resolved instanceof String sResolved) {
if (required || StringUtils.hasText(sResolved)) {
result = Short.parseShort(sResolved);
if (resolved instanceof String str) {
if (required || StringUtils.hasText(str)) {
result = Short.parseShort(str);
}
}
else if (resolved instanceof Number nResolved) {
result = nResolved.shortValue();
else if (resolved instanceof Number num) {
result = num.shortValue();
}
else if (resolved != null || required) {
throw new IllegalStateException(
Expand All @@ -284,13 +291,13 @@ else if (resolved != null || required) {
private Long resolveExpressionAsLong(String value, String attribute, boolean required) {
Object resolved = resolveExpression(value);
Long result = null;
if (resolved instanceof String sResolved) {
if (required || StringUtils.hasText(sResolved)) {
result = Long.parseLong(sResolved);
if (resolved instanceof String str) {
if (required || StringUtils.hasText(str)) {
result = Long.parseLong(str);
}
}
else if (resolved instanceof Number nResolved) {
result = nResolved.longValue();
else if (resolved instanceof Number num) {
result = num.longValue();
}
else if (resolved != null || required) {
throw new IllegalStateException(
Expand All @@ -304,13 +311,13 @@ else if (resolved != null || required) {
private Double resolveExpressionAsDouble(String value, String attribute, boolean required) {
Object resolved = resolveExpression(value);
Double result = null;
if (resolved instanceof String sResolved) {
if (required || StringUtils.hasText(sResolved)) {
result = Double.parseDouble(sResolved);
if (resolved instanceof String str) {
if (required || StringUtils.hasText(str)) {
result = Double.parseDouble(str);
}
}
else if (resolved instanceof Number nResolved) {
result = nResolved.doubleValue();
else if (resolved instanceof Number num) {
result = num.doubleValue();
}
else if (resolved != null || required) {
throw new IllegalStateException(
Expand All @@ -324,11 +331,11 @@ else if (resolved != null || required) {
private Boolean resolveExpressionAsBoolean(String value, String attribute) {
Object resolved = resolveExpression(value);
Boolean result = null;
if (resolved instanceof Boolean bResolved) {
result = bResolved;
if (resolved instanceof Boolean bool) {
result = bool;
}
else if (resolved instanceof String sResolved) {
result = Boolean.parseBoolean(sResolved);
else if (resolved instanceof String str) {
result = Boolean.parseBoolean(str);
}
else if (resolved != null) {
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2022 the original author or authors.
* Copyright 2018-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -100,10 +100,10 @@ static class TopicCreation {
private final int numPartitions;
private final short replicationFactor;

TopicCreation(boolean shouldCreate, int numPartitions, short replicationFactor) {
this.shouldCreateTopics = shouldCreate;
this.numPartitions = numPartitions;
this.replicationFactor = replicationFactor;
TopicCreation(@Nullable Boolean shouldCreate, @Nullable Integer numPartitions, @Nullable Short replicationFactor) {
this.shouldCreateTopics = shouldCreate == null || shouldCreate;
this.numPartitions = numPartitions == null ? 1 : numPartitions;
this.replicationFactor = replicationFactor == null ? -1 : replicationFactor;
}

TopicCreation() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,17 @@ public class RetryTopicConfigurationBuilder {

private EndpointHandlerMethod dltHandlerMethod;

@Nullable
private String retryTopicSuffix;

@Nullable
private String dltSuffix;

private RetryTopicConfiguration.TopicCreation topicCreationConfiguration = new RetryTopicConfiguration.TopicCreation();

private ConcurrentKafkaListenerContainerFactory<?, ?> listenerContainerFactory;

@Nullable
private String listenerContainerFactoryName;

@Nullable
Expand Down Expand Up @@ -206,7 +209,7 @@ public RetryTopicConfigurationBuilder excludeTopic(String topicName) {
* @param suffix the suffix.
* @return the builder.
*/
public RetryTopicConfigurationBuilder retryTopicSuffix(String suffix) {
public RetryTopicConfigurationBuilder retryTopicSuffix(@Nullable String suffix) {
this.retryTopicSuffix = suffix;
return this;
}
Expand All @@ -216,7 +219,7 @@ public RetryTopicConfigurationBuilder retryTopicSuffix(String suffix) {
* @param suffix the suffix.
* @return the builder.
*/
public RetryTopicConfigurationBuilder dltSuffix(String suffix) {
public RetryTopicConfigurationBuilder dltSuffix(@Nullable String suffix) {
this.dltSuffix = suffix;
return this;
}
Expand Down Expand Up @@ -422,7 +425,7 @@ public RetryTopicConfigurationBuilder doNotAutoCreateRetryTopics() {
* broker is version 2.4 or later).
* @return the builder.
*/
public RetryTopicConfigurationBuilder autoCreateTopicsWith(int numPartitions, short replicationFactor) {
public RetryTopicConfigurationBuilder autoCreateTopicsWith(@Nullable Integer numPartitions, @Nullable Short replicationFactor) {
this.topicCreationConfiguration = new RetryTopicConfiguration.TopicCreation(true, numPartitions,
replicationFactor);
return this;
Expand All @@ -437,8 +440,8 @@ public RetryTopicConfigurationBuilder autoCreateTopicsWith(int numPartitions, sh
* broker is version 2.4 or later).
* @return the builder.
*/
public RetryTopicConfigurationBuilder autoCreateTopics(boolean shouldCreate, int numPartitions,
short replicationFactor) {
public RetryTopicConfigurationBuilder autoCreateTopics(@Nullable Boolean shouldCreate, @Nullable Integer numPartitions,
@Nullable Short replicationFactor) {

this.topicCreationConfiguration = new RetryTopicConfiguration.TopicCreation(shouldCreate, numPartitions,
replicationFactor);
Expand Down Expand Up @@ -535,7 +538,7 @@ public RetryTopicConfigurationBuilder listenerFactory(ConcurrentKafkaListenerCon
* @param factoryBeanName the factory bean name.
* @return the builder.
*/
public RetryTopicConfigurationBuilder listenerFactory(String factoryBeanName) {
public RetryTopicConfigurationBuilder listenerFactory(@Nullable String factoryBeanName) {
this.listenerContainerFactoryName = factoryBeanName;
return this;
}
Expand Down

0 comments on commit 3e8f9a7

Please sign in to comment.