Skip to content

Commit 221393e

Browse files
artembilangaryrussell
authored andcommitted
MessageHandlingException consistency
* Use `IntegrationUtils.wrapInHandlingExceptionIfNecessary()` whenever it is possible to avoid double wrapping into the `MessageHandlingException` * Some code polishing for affected classes `@Nullable`, streams, diamonds etc.
1 parent 9c84ece commit 221393e

File tree

35 files changed

+356
-303
lines changed

35 files changed

+356
-303
lines changed

spring-integration-core/src/main/java/org/springframework/integration/core/MessageProducer.java

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.integration.core;
1818

19+
import org.springframework.lang.Nullable;
1920
import org.springframework.messaging.MessageChannel;
2021

2122
/**
@@ -49,6 +50,7 @@ default void setOutputChannelName(String outputChannel) {
4950
* @return the channel.
5051
* @since 4.3
5152
*/
53+
@Nullable
5254
MessageChannel getOutputChannel();
5355

5456
}

spring-integration-core/src/main/java/org/springframework/integration/endpoint/MessageSourcePollingTemplate.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 the original author or authors.
2+
* Copyright 2018-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,9 +20,9 @@
2020
import org.springframework.integration.acks.AckUtils;
2121
import org.springframework.integration.acks.AcknowledgmentCallback;
2222
import org.springframework.integration.core.MessageSource;
23+
import org.springframework.integration.support.utils.IntegrationUtils;
2324
import org.springframework.messaging.Message;
2425
import org.springframework.messaging.MessageHandler;
25-
import org.springframework.messaging.MessageHandlingException;
2626
import org.springframework.util.Assert;
2727

2828
/**
@@ -56,7 +56,9 @@ public boolean poll(MessageHandler handler) {
5656
}
5757
catch (Exception e) {
5858
AckUtils.autoNack(ackCallback);
59-
throw new MessageHandlingException(message, e);
59+
throw IntegrationUtils.wrapInHandlingExceptionIfNecessary(message,
60+
() -> "error occurred during handling message in 'MessageSourcePollingTemplate' ["
61+
+ this + "]", e);
6062
}
6163
return true;
6264
}

spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractMessageHandler.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.integration.support.management.MetricsContext;
3434
import org.springframework.integration.support.management.Statistics;
3535
import org.springframework.integration.support.management.TrackableComponent;
36+
import org.springframework.integration.support.management.metrics.MeterFacade;
3637
import org.springframework.integration.support.management.metrics.MetricsCaptor;
3738
import org.springframework.integration.support.management.metrics.SampleFacade;
3839
import org.springframework.integration.support.management.metrics.TimerFacade;
@@ -338,7 +339,7 @@ public String getManagedType() {
338339

339340
@Override
340341
public void destroy() throws Exception {
341-
this.timers.forEach(t -> t.remove());
342+
this.timers.forEach(MeterFacade::remove);
342343
}
343344

344345
}

spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractMessageProcessor.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.handler;
1818

1919
import org.springframework.integration.util.AbstractExpressionEvaluator;
20+
import org.springframework.lang.Nullable;
2021
import org.springframework.messaging.Message;
2122

2223
/**
@@ -28,6 +29,7 @@
2829
*/
2930
public abstract class AbstractMessageProcessor<T> extends AbstractExpressionEvaluator implements MessageProcessor<T> {
3031

32+
@Nullable
3133
public abstract T processMessage(Message<?> message);
3234

3335
}

spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractMessageProducingHandler.java

+3
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ public abstract class AbstractMessageProducingHandler extends AbstractMessageHan
6868

6969
private boolean async;
7070

71+
@Nullable
7172
private String outputChannelName;
7273

74+
@Nullable
7375
private MessageChannel outputChannel;
7476

7577
private String[] notPropagatedHeaders;
@@ -205,6 +207,7 @@ protected void onInit() {
205207
}
206208

207209
@Override
210+
@Nullable
208211
public MessageChannel getOutputChannel() {
209212
if (this.outputChannelName != null) {
210213
this.outputChannel = getChannelResolver().resolveDestination(this.outputChannelName);

spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractReplyProducingMessageHandler.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import org.springframework.aop.framework.ProxyFactory;
2525
import org.springframework.beans.factory.BeanClassLoaderAware;
2626
import org.springframework.integration.handler.advice.HandleMessageAdvice;
27+
import org.springframework.lang.Nullable;
2728
import org.springframework.messaging.Message;
2829
import org.springframework.util.Assert;
2930
import org.springframework.util.ClassUtils;
@@ -137,6 +138,7 @@ else if (!isAsync() && logger.isDebugEnabled()) {
137138
}
138139
}
139140

141+
@Nullable
140142
protected Object doInvokeAdvisedRequestHandler(Message<?> message) {
141143
return this.advisedRequestHandler.handleRequestMessage(message);
142144
}
@@ -149,6 +151,7 @@ protected Object doInvokeAdvisedRequestHandler(Message<?> message) {
149151
* @param requestMessage The request message.
150152
* @return The result of handling the message, or {@code null}.
151153
*/
154+
@Nullable
152155
protected abstract Object handleRequestMessage(Message<?> requestMessage);
153156

154157

@@ -166,6 +169,7 @@ protected Object doInvokeAdvisedRequestHandler(Message<?> message) {
166169
*/
167170
public interface RequestHandler {
168171

172+
@Nullable
169173
Object handleRequestMessage(Message<?> requestMessage);
170174

171175
/**
@@ -189,6 +193,7 @@ private class AdvisedRequestHandler implements RequestHandler {
189193
}
190194

191195
@Override
196+
@Nullable
192197
public Object handleRequestMessage(Message<?> requestMessage) {
193198
return AbstractReplyProducingMessageHandler.this.handleRequestMessage(requestMessage);
194199
}

spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractReplyProducingPostProcessingMessageHandler.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,13 @@
1616

1717
package org.springframework.integration.handler;
1818

19+
import org.springframework.lang.Nullable;
1920
import org.springframework.messaging.Message;
2021

2122
/**
2223
* @author Gary Russell
24+
* @author Artem Bilan
25+
*
2326
* @since 3.0
2427
*
2528
*/
@@ -42,6 +45,7 @@ public void setPostProcessWithinAdvice(boolean postProcessWithinAdvice) {
4245
}
4346

4447
@Override
48+
@Nullable
4549
protected final Object handleRequestMessage(Message<?> requestMessage) {
4650
Object result = this.doHandleRequestMessage(requestMessage);
4751
if (this.postProcessWithinAdvice || !this.hasAdviceChain()) {
@@ -51,6 +55,7 @@ protected final Object handleRequestMessage(Message<?> requestMessage) {
5155
}
5256

5357
@Override
58+
@Nullable
5459
protected final Object doInvokeAdvisedRequestHandler(Message<?> message) {
5560
Object result = super.doInvokeAdvisedRequestHandler(message);
5661
if (!this.postProcessWithinAdvice) {
@@ -59,6 +64,7 @@ protected final Object doInvokeAdvisedRequestHandler(Message<?> message) {
5964
return result;
6065
}
6166

67+
@Nullable
6268
protected abstract Object doHandleRequestMessage(Message<?> requestMessage);
6369

6470
}

spring-integration-core/src/main/java/org/springframework/integration/handler/BeanNameMessageProcessor.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,8 +19,8 @@
1919
import org.springframework.beans.BeansException;
2020
import org.springframework.beans.factory.BeanFactory;
2121
import org.springframework.beans.factory.BeanFactoryAware;
22+
import org.springframework.lang.Nullable;
2223
import org.springframework.messaging.Message;
23-
import org.springframework.util.Assert;
2424

2525
/**
2626
* An "artificial" {@link MessageProcessor} for lazy-load of target bean by its name.
@@ -29,6 +29,7 @@
2929
* @param <T> the expected {@link #processMessage} result type.
3030
*
3131
* @author Artem Bilan
32+
*
3233
* @since 5.0
3334
*/
3435
public class BeanNameMessageProcessor<T> implements MessageProcessor<T>, BeanFactoryAware {
@@ -48,11 +49,11 @@ public BeanNameMessageProcessor(String object, String methodName) {
4849

4950
@Override
5051
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
51-
Assert.notNull(beanFactory, "'beanFactory' must not be null");
5252
this.beanFactory = beanFactory;
5353
}
5454

5555
@Override
56+
@Nullable
5657
public T processMessage(Message<?> message) {
5758
if (this.delegate == null) {
5859
Object target = this.beanFactory.getBean(this.beanName);

spring-integration-core/src/main/java/org/springframework/integration/handler/DiscardingMessageHandler.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.integration.handler;
1818

19+
import org.springframework.lang.Nullable;
1920
import org.springframework.messaging.MessageChannel;
2021
import org.springframework.messaging.MessageHandler;
2122

@@ -32,6 +33,7 @@ public interface DiscardingMessageHandler extends MessageHandler {
3233
* Return the discard channel.
3334
* @return the channel.
3435
*/
36+
@Nullable
3537
MessageChannel getDiscardChannel();
3638

3739
}

spring-integration-core/src/main/java/org/springframework/integration/handler/ExpressionCommandMessageProcessor.java

+22-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,7 +31,9 @@
3131
import org.springframework.expression.MethodFilter;
3232
import org.springframework.expression.MethodResolver;
3333
import org.springframework.expression.spel.support.ReflectiveMethodResolver;
34+
import org.springframework.lang.Nullable;
3435
import org.springframework.messaging.Message;
36+
import org.springframework.util.CollectionUtils;
3537

3638
/**
3739
* A MessageProcessor implementation that expects an Expression or expressionString
@@ -40,33 +42,40 @@
4042
* @author Dave Syer
4143
* @author Mark Fisher
4244
* @author Gary Russell
45+
* @author Artem Bilan
46+
*
4347
* @since 2.0
4448
*/
4549
public class ExpressionCommandMessageProcessor extends AbstractMessageProcessor<Object> {
4650

4751
public ExpressionCommandMessageProcessor() {
4852
}
4953

50-
public ExpressionCommandMessageProcessor(MethodFilter methodFilter) {
54+
public ExpressionCommandMessageProcessor(@Nullable MethodFilter methodFilter) {
5155
this(methodFilter, null);
5256
}
5357

54-
public ExpressionCommandMessageProcessor(MethodFilter methodFilter, BeanFactory beanFactory) {
58+
public ExpressionCommandMessageProcessor(@Nullable MethodFilter methodFilter, @Nullable BeanFactory beanFactory) {
5559
if (beanFactory != null) {
56-
this.setBeanFactory(beanFactory);
60+
setBeanFactory(beanFactory);
5761
}
5862
if (methodFilter != null) {
5963
MethodResolver methodResolver = new ExpressionCommandMethodResolver(methodFilter);
60-
this.getEvaluationContext(false).setMethodResolvers(Collections.singletonList(methodResolver));
64+
getEvaluationContext(false).setMethodResolvers(Collections.singletonList(methodResolver));
6165
}
6266
}
6367

68+
@Override
69+
public final void setBeanFactory(BeanFactory beanFactory) {
70+
super.setBeanFactory(beanFactory);
71+
}
6472

6573
/**
6674
* Evaluates the Message payload expression as a command.
6775
* @throws IllegalArgumentException if the payload is not an Exception or String
6876
*/
6977
@Override
78+
@Nullable
7079
public Object processMessage(Message<?> message) {
7180
Object expression = message.getPayload();
7281
if (expression instanceof Expression) {
@@ -91,18 +100,17 @@ private static final class ExpressionCommandMethodResolver extends ReflectiveMet
91100

92101
@Override
93102
public MethodExecutor resolve(EvaluationContext context,
94-
Object targetObject, String name, List<TypeDescriptor> argumentTypes) throws AccessException {
95-
this.validateMethod(targetObject, name, (argumentTypes != null ? argumentTypes.size() : 0));
103+
Object targetObject, String name, List<TypeDescriptor> argumentTypes)
104+
throws AccessException {
105+
106+
validateMethod(targetObject, name, !CollectionUtils.isEmpty(argumentTypes) ? argumentTypes.size() : 0);
96107
return super.resolve(context, targetObject, name, argumentTypes);
97108
}
98109

99110
private void validateMethod(Object targetObject, String name, int argumentCount) {
100-
if (this.methodFilter == null) {
101-
return;
102-
}
103111
Class<?> type = (targetObject instanceof Class ? (Class<?>) targetObject : targetObject.getClass());
104112
Method[] methods = type.getMethods();
105-
List<Method> candidates = new ArrayList<Method>();
113+
List<Method> candidates = new ArrayList<>();
106114
for (Method method : methods) {
107115
if (method.getName().equals(name) && method.getParameterTypes().length == argumentCount) {
108116
candidates.add(method);
@@ -111,10 +119,12 @@ private void validateMethod(Object targetObject, String name, int argumentCount)
111119
List<Method> supportedMethods = this.methodFilter.filter(candidates);
112120
if (supportedMethods.size() == 0) {
113121
String methodDescription = (candidates.size() > 0) ? candidates.get(0).toString() : name;
114-
throw new EvaluationException("The method '" + methodDescription + "' is not supported by this command processor. " +
122+
throw new EvaluationException("The method '" + methodDescription +
123+
"' is not supported by this command processor. " +
115124
"If using the Control Bus, consider adding @ManagedOperation or @ManagedAttribute.");
116125
}
117126
}
127+
118128
}
119129

120130
}

spring-integration-core/src/main/java/org/springframework/integration/handler/ExpressionEvaluatingMessageProcessor.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import org.springframework.expression.Expression;
2020
import org.springframework.expression.ParseException;
21+
import org.springframework.lang.Nullable;
2122
import org.springframework.messaging.Message;
2223
import org.springframework.util.Assert;
2324

@@ -28,6 +29,7 @@
2829
* @author Mark Fisher
2930
* @author Artem Bilan
3031
* @author Gary Russell
32+
*
3133
* @since 2.0
3234
*/
3335
public class ExpressionEvaluatingMessageProcessor<T> extends AbstractMessageProcessor<T> {
@@ -51,7 +53,7 @@ public ExpressionEvaluatingMessageProcessor(Expression expression) {
5153
* @param expression The expression.
5254
* @param expectedType The expected type.
5355
*/
54-
public ExpressionEvaluatingMessageProcessor(Expression expression, Class<T> expectedType) {
56+
public ExpressionEvaluatingMessageProcessor(Expression expression, @Nullable Class<T> expectedType) {
5557
Assert.notNull(expression, "The expression must not be null");
5658
try {
5759
this.expression = expression;
@@ -84,7 +86,7 @@ public ExpressionEvaluatingMessageProcessor(String expression) {
8486
* @param expectedType the expected result type.
8587
* @since 5.0
8688
*/
87-
public ExpressionEvaluatingMessageProcessor(String expression, Class<T> expectedType) {
89+
public ExpressionEvaluatingMessageProcessor(String expression, @Nullable Class<T> expectedType) {
8890
try {
8991
this.expression = EXPRESSION_PARSER.parseExpression(expression);
9092
this.expectedType = expectedType;

0 commit comments

Comments
 (0)