-
Notifications
You must be signed in to change notification settings - Fork 1.1k
INT-3459: Log exceptions in case of failOver #2790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2002-2018 the original author or authors. | ||
* Copyright 2002-2019 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. | ||
|
@@ -19,10 +19,12 @@ | |
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.Executor; | ||
|
||
import org.springframework.integration.MessageDispatchingException; | ||
import org.springframework.integration.support.utils.IntegrationUtils; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.messaging.Message; | ||
import org.springframework.messaging.MessageHandler; | ||
import org.springframework.messaging.support.MessageHandlingRunnable; | ||
|
@@ -47,25 +49,26 @@ | |
* @author Gary Russell | ||
* @author Oleg Zhurakousky | ||
* @author Artem Bilan | ||
* | ||
* @since 1.0.2 | ||
*/ | ||
public class UnicastingDispatcher extends AbstractDispatcher { | ||
|
||
private final MessageHandler dispatchHandler = message -> doDispatch(message); | ||
private final MessageHandler dispatchHandler = this::doDispatch; | ||
|
||
private final Executor executor; | ||
|
||
private volatile boolean failover = true; | ||
private boolean failover = true; | ||
|
||
private volatile LoadBalancingStrategy loadBalancingStrategy; | ||
private LoadBalancingStrategy loadBalancingStrategy; | ||
|
||
private volatile MessageHandlingTaskDecorator messageHandlingTaskDecorator = task -> task; | ||
private MessageHandlingTaskDecorator messageHandlingTaskDecorator = task -> task; | ||
|
||
public UnicastingDispatcher() { | ||
this.executor = null; | ||
} | ||
|
||
public UnicastingDispatcher(Executor executor) { | ||
public UnicastingDispatcher(@Nullable Executor executor) { | ||
this.executor = executor; | ||
} | ||
|
||
|
@@ -74,7 +77,6 @@ public UnicastingDispatcher(Executor executor) { | |
* Specify whether this dispatcher should failover when a single | ||
* {@link MessageHandler} throws an Exception. The default value is | ||
* <code>true</code>. | ||
* | ||
* @param failover The failover boolean. | ||
*/ | ||
public void setFailover(boolean failover) { | ||
|
@@ -83,10 +85,9 @@ public void setFailover(boolean failover) { | |
|
||
/** | ||
* Provide a {@link LoadBalancingStrategy} for this dispatcher. | ||
* | ||
* @param loadBalancingStrategy The load balancing strategy implementation. | ||
*/ | ||
public void setLoadBalancingStrategy(LoadBalancingStrategy loadBalancingStrategy) { | ||
public void setLoadBalancingStrategy(@Nullable LoadBalancingStrategy loadBalancingStrategy) { | ||
this.loadBalancingStrategy = loadBalancingStrategy; | ||
} | ||
|
||
|
@@ -133,22 +134,30 @@ private boolean doDispatch(Message<?> message) { | |
return true; | ||
} | ||
boolean success = false; | ||
Iterator<MessageHandler> handlerIterator = this.getHandlerIterator(message); | ||
Iterator<MessageHandler> handlerIterator = getHandlerIterator(message); | ||
if (!handlerIterator.hasNext()) { | ||
throw new MessageDispatchingException(message, "Dispatcher has no subscribers"); | ||
} | ||
List<RuntimeException> exceptions = new ArrayList<RuntimeException>(); | ||
List<RuntimeException> exceptions = null; | ||
while (!success && handlerIterator.hasNext()) { | ||
MessageHandler handler = handlerIterator.next(); | ||
try { | ||
handler.handleMessage(message); | ||
success = true; // we have a winner. | ||
} | ||
catch (Exception e) { | ||
RuntimeException runtimeException = IntegrationUtils.wrapInDeliveryExceptionIfNecessary(message, | ||
() -> "Dispatcher failed to deliver Message", e); | ||
catch (Exception ex) { | ||
RuntimeException runtimeException = | ||
IntegrationUtils.wrapInDeliveryExceptionIfNecessary(message, | ||
() -> "Dispatcher failed to deliver Message", ex); | ||
if (exceptions == null) { | ||
exceptions = new ArrayList<>(); | ||
} | ||
exceptions.add(runtimeException); | ||
this.handleExceptions(exceptions, message, !handlerIterator.hasNext()); | ||
boolean isLast = !handlerIterator.hasNext(); | ||
if (!isLast && this.failover) { | ||
logExceptionBeforeFailOver(ex, handler, message); | ||
} | ||
handleExceptions(exceptions, message, isLast); | ||
} | ||
} | ||
return success; | ||
|
@@ -160,10 +169,22 @@ private boolean doDispatch(Message<?> message) { | |
* it simply returns the Iterator for the existing handler List. | ||
*/ | ||
private Iterator<MessageHandler> getHandlerIterator(Message<?> message) { | ||
Set<MessageHandler> handlers = getHandlers(); | ||
if (this.loadBalancingStrategy != null) { | ||
return this.loadBalancingStrategy.getHandlerIterator(message, this.getHandlers()); | ||
return this.loadBalancingStrategy.getHandlerIterator(message, handlers); | ||
} | ||
return handlers.iterator(); | ||
} | ||
|
||
private void logExceptionBeforeFailOver(Exception ex, MessageHandler handler, Message<?> message) { | ||
if (this.logger.isDebugEnabled()) { | ||
this.logger.debug("An exception was thrown by '" + handler + "' while handling '" + message + | ||
"'. Failing over to the next subscriber.", ex); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Debug has to be detected first otherwise we'll always take the INFO branch. |
||
} | ||
else if (this.logger.isInfoEnabled()) { | ||
this.logger.info("An exception was thrown by '" + handler + "' while handling '" + message + "': " + | ||
ex.getMessage() + ". Failing over to the next subscriber."); | ||
} | ||
return this.getHandlers().iterator(); | ||
} | ||
|
||
/** | ||
|
@@ -176,10 +197,10 @@ private Iterator<MessageHandler> getHandlerIterator(Message<?> message) { | |
*/ | ||
private void handleExceptions(List<RuntimeException> allExceptions, Message<?> message, boolean isLast) { | ||
if (isLast || !this.failover) { | ||
if (allExceptions != null && allExceptions.size() == 1) { | ||
if (allExceptions.size() == 1) { | ||
throw allExceptions.get(0); | ||
} | ||
throw new AggregateMessageDeliveryException(message, //NOSONAR - false positive | ||
throw new AggregateMessageDeliveryException(message, | ||
"All attempts to deliver Message to MessageHandlers failed.", allExceptions); | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this line wrap? It was previously 112.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, my own preferences: I find it is much readable when variable initialization is multi-line. So, I start initialization block fully from a new line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK