Skip to content

Commit

Permalink
some minor improvements on RetryableTopicAnnotationProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanQingyangXu committed Dec 22, 2023
1 parent b1e7623 commit 930bbee
Showing 1 changed file with 39 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public class RetryableTopicAnnotationProcessor {
* @param beanFactory the bean factory.
*/
public RetryableTopicAnnotationProcessor(BeanFactory beanFactory) {
this(beanFactory, new StandardBeanExpressionResolver(), beanFactory instanceof ConfigurableBeanFactory
? new BeanExpressionContext((ConfigurableBeanFactory) beanFactory, null)
this(beanFactory, new StandardBeanExpressionResolver(), beanFactory instanceof ConfigurableBeanFactory cbf
? new BeanExpressionContext(cbf, null)
: null); // NOSONAR
}

Expand All @@ -101,7 +101,6 @@ public RetryableTopicAnnotationProcessor(BeanFactory beanFactory, BeanExpression
this.expressionContext = expressionContext;
}

@SuppressWarnings("deprecation")
public RetryTopicConfiguration processAnnotation(String[] topics, Method method, RetryableTopic annotation,
Object bean) {

Expand All @@ -121,7 +120,7 @@ public RetryTopicConfiguration processAnnotation(String[] topics, Method method,
traverse = traverseResolved;
}
else {
traverse = includes.size() > 0 || excludes.size() > 0;
traverse = !includes.isEmpty() || !excludes.isEmpty();
}
}
Boolean autoStartDlt = null;
Expand Down Expand Up @@ -173,9 +172,13 @@ private SleepingBackOffPolicy<?> createBackoffFromAnnotation(Backoff backoff, Be
if (backoff.random()) {
policy = new ExponentialRandomBackOffPolicy();
}
policy.setInitialInterval(min);
if (min != null) {
policy.setInitialInterval(min);
}
policy.setMultiplier(multiplier);
policy.setMaxInterval(max > min ? max : ExponentialBackOffPolicy.DEFAULT_MAX_INTERVAL);
if (max != null && min != null && max > min) {
policy.setMaxInterval(max);
}
return policy;
}
if (max != null && min != null && max > min) {
Expand All @@ -200,7 +203,6 @@ private EndpointHandlerMethod getDltProcessor(Method listenerMethod, Object bean
.orElse(RetryTopicConfigurer.DEFAULT_DLT_HANDLER);
}

@SuppressWarnings("deprecation")
private KafkaOperations<?, ?> getKafkaTemplate(String kafkaTemplateName, String[] topics) {
if (StringUtils.hasText(kafkaTemplateName)) {
Assert.state(this.beanFactory != null, "BeanFactory must be set to obtain kafka template by bean name");
Expand All @@ -213,6 +215,7 @@ private EndpointHandlerMethod getDltProcessor(Method listenerMethod, Object bean
+ " with id '" + kafkaTemplateName + "' was found in the application context", ex);
}
}
Assert.state(this.beanFactory != null, "BeanFactory must be set to obtain kafka template by default bean name");
try {
return this.beanFactory.getBean(RetryTopicBeanNames.DEFAULT_KAFKA_TEMPLATE_BEAN_NAME,
KafkaOperations.class);
Expand All @@ -228,8 +231,8 @@ private EndpointHandlerMethod getDltProcessor(Method listenerMethod, Object bean

private String resolveExpressionAsString(String value, String attribute) {
Object resolved = resolveExpression(value);
if (resolved instanceof String) {
return (String) resolved;
if (resolved instanceof String sResolved) {
return sResolved;
}
else if (resolved != null) {
throw new IllegalStateException(THE_OSQ + attribute + "] must resolve to a String. "
Expand All @@ -241,16 +244,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) {
if (!required && !StringUtils.hasText((String) resolved)) {
result = null;
}
else {
result = Integer.parseInt((String) resolved);
if (resolved instanceof String sResolved) {
if (required || StringUtils.hasText(sResolved)) {
result = Integer.parseInt(sResolved);
}
}
else if (resolved instanceof Number) {
result = ((Number) resolved).intValue();
else if (resolved instanceof Number nResolved) {
result = nResolved.intValue();
}
else if (resolved != null || required) {
throw new IllegalStateException(
Expand All @@ -264,16 +264,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) {
if (!required && !StringUtils.hasText((String) resolved)) {
result = null;
}
else {
result = Short.parseShort((String) resolved);
if (resolved instanceof String sResolved) {
if (required || StringUtils.hasText(sResolved)) {
result = Short.parseShort(sResolved);
}
}
else if (resolved instanceof Number) {
result = ((Number) resolved).shortValue();
else if (resolved instanceof Number nResolved) {
result = nResolved.shortValue();
}
else if (resolved != null || required) {
throw new IllegalStateException(
Expand All @@ -287,16 +284,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) {
if (!required && !StringUtils.hasText((String) resolved)) {
result = null;
}
else {
result = Long.parseLong((String) resolved);
if (resolved instanceof String sResolved) {
if (required || StringUtils.hasText(sResolved)) {
result = Long.parseLong(sResolved);
}
}
else if (resolved instanceof Number) {
result = ((Number) resolved).longValue();
else if (resolved instanceof Number nResolved) {
result = nResolved.longValue();
}
else if (resolved != null || required) {
throw new IllegalStateException(
Expand All @@ -310,16 +304,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) {
if (!required && !StringUtils.hasText((String) resolved)) {
result = null;
}
else {
result = Double.parseDouble((String) resolved);
if (resolved instanceof String sResolved) {
if (required || StringUtils.hasText(sResolved)) {
result = Double.parseDouble(sResolved);
}
}
else if (resolved instanceof Number) {
result = ((Number) resolved).doubleValue();
else if (resolved instanceof Number nResolved) {
result = nResolved.doubleValue();
}
else if (resolved != null || required) {
throw new IllegalStateException(
Expand All @@ -333,11 +324,11 @@ else if (resolved != null || required) {
private Boolean resolveExpressionAsBoolean(String value, String attribute) {
Object resolved = resolveExpression(value);
Boolean result = null;
if (resolved instanceof Boolean) {
result = (Boolean) resolved;
if (resolved instanceof Boolean bResolved) {
result = bResolved;
}
else if (resolved instanceof String) {
result = Boolean.parseBoolean((String) resolved);
else if (resolved instanceof String sResolved) {
result = Boolean.parseBoolean(sResolved);
}
else if (resolved != null) {
throw new IllegalStateException(
Expand Down Expand Up @@ -368,8 +359,8 @@ private List<Class<? extends Throwable>> resolveClasses(Class<? extends Throwabl
}

private Object resolveExpression(String value) {
String resolved = resolve(value);
if (this.expressionContext != null) {
String resolved = resolve(value);
return this.resolver.evaluate(resolved, this.expressionContext);
}
else {
Expand All @@ -378,8 +369,8 @@ private Object resolveExpression(String value) {
}

private String resolve(String value) {
if (this.beanFactory != null && this.beanFactory instanceof ConfigurableBeanFactory) {
return ((ConfigurableBeanFactory) this.beanFactory).resolveEmbeddedValue(value);
if (this.beanFactory instanceof ConfigurableBeanFactory cbf) {
return cbf.resolveEmbeddedValue(value);
}
return value;
}
Expand Down

0 comments on commit 930bbee

Please sign in to comment.