Skip to content

Commit 5cdeb29

Browse files
committed
* Fix RouterTests for proper mapping order
* Polishing for `AbstractMappingMessageRouter` hierarchy, so we don't use `protected channelMappings` field access any more
1 parent 117dbc5 commit 5cdeb29

File tree

4 files changed

+16
-20
lines changed

4 files changed

+16
-20
lines changed

spring-integration-core/src/main/java/org/springframework/integration/router/AbstractMappingMessageRouter.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ protected boolean removeEldestEntry(Entry<String, MessageChannel> eldest) {
6868

6969
});
7070

71-
protected volatile Map<String, String> channelMappings = new LinkedHashMap<>();
71+
private String prefix;
7272

73-
private volatile String prefix;
73+
private String suffix;
7474

75-
private volatile String suffix;
75+
private boolean resolutionRequired = true;
7676

77-
private volatile boolean resolutionRequired = true;
77+
private volatile Map<String, String> channelMappings = new LinkedHashMap<>();
7878

7979

8080
/**
@@ -228,9 +228,6 @@ private MessageChannel resolveChannelForName(String channelName, Message<?> mess
228228
throw new MessagingException(message, "failed to resolve channel name '" + channelName + "'", e);
229229
}
230230
}
231-
if (channel == null && this.resolutionRequired) {
232-
throw new MessagingException(message, "failed to resolve channel name '" + channelName + "'");
233-
}
234231
return channel;
235232
}
236233

spring-integration-core/src/main/java/org/springframework/integration/router/ErrorMessageExceptionTypeRouter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ public void removeChannelMapping(String key) {
101101
@ManagedOperation
102102
public void replaceChannelMappings(Properties channelMappings) {
103103
super.replaceChannelMappings(channelMappings);
104-
populateClassNameMapping(this.channelMappings.keySet());
104+
populateClassNameMapping(getChannelMappings().keySet());
105105
}
106106

107107
@Override
108108
protected void onInit() {
109109
super.onInit();
110-
populateClassNameMapping(this.channelMappings.keySet());
110+
populateClassNameMapping(getChannelMappings().keySet());
111111
this.initialized = true;
112112
}
113113

spring-integration-core/src/main/java/org/springframework/integration/router/PayloadTypeRouter.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@
1616

1717
package org.springframework.integration.router;
1818

19-
import java.util.ArrayList;
2019
import java.util.Collections;
20+
import java.util.LinkedList;
2121
import java.util.List;
2222

2323
import org.springframework.messaging.Message;
2424
import org.springframework.util.CollectionUtils;
2525

2626
/**
2727
* A Message Router that resolves the {@link org.springframework.messaging.MessageChannel}
28-
* based on the
29-
* {@link Message Message's} payload type.
28+
* based on the {@link Message Message's} payload type.
3029
*
3130
* @author Mark Fisher
3231
* @author Oleg Zhurakousky
3332
* @author Gary Russell
33+
* @author Artem Bilan
3434
*/
3535
public class PayloadTypeRouter extends AbstractMappingMessageRouter {
3636

@@ -47,23 +47,22 @@ public class PayloadTypeRouter extends AbstractMappingMessageRouter {
4747
*/
4848
@Override
4949
protected List<Object> getChannelKeys(Message<?> message) {
50-
if (CollectionUtils.isEmpty(this.channelMappings)) {
50+
if (CollectionUtils.isEmpty(getChannelMappings())) {
5151
return null;
5252
}
5353
Class<?> type = message.getPayload().getClass();
5454
boolean isArray = type.isArray();
5555
if (isArray) {
5656
type = type.getComponentType();
5757
}
58-
String closestMatch = this.findClosestMatch(type, isArray);
59-
return (closestMatch != null) ? Collections.<Object>singletonList(closestMatch) : null;
58+
String closestMatch = findClosestMatch(type, isArray);
59+
return (closestMatch != null) ? Collections.singletonList(closestMatch) : null;
6060
}
6161

62-
6362
private String findClosestMatch(Class<?> type, boolean isArray) {
6463
int minTypeDiffWeight = Integer.MAX_VALUE;
65-
List<String> matches = new ArrayList<String>();
66-
for (String candidate : this.channelMappings.keySet()) {
64+
List<String> matches = new LinkedList<>();
65+
for (String candidate : getChannelMappings().keySet()) {
6766
if (isArray) {
6867
if (!candidate.endsWith(ARRAY_SUFFIX)) {
6968
continue;
@@ -118,7 +117,7 @@ private int determineTypeDifferenceWeight(String candidate, Class<?> type, int l
118117
// exhausted hierarchy and found no match
119118
return Integer.MAX_VALUE;
120119
}
121-
return this.determineTypeDifferenceWeight(candidate, type.getSuperclass(), level + 2);
120+
return determineTypeDifferenceWeight(candidate, type.getSuperclass(), level + 2);
122121
}
123122

124123
}

spring-integration-core/src/test/java/org/springframework/integration/dsl/routers/RouterTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,8 @@ public IntegrationFlow payloadTypeRouteFlow() {
743743
public IntegrationFlow exceptionTypeRouteFlow() {
744744
return f -> f
745745
.routeByException(r -> r
746-
.channelMapping(IllegalArgumentException.class, "illegalArgumentChannel")
747746
.channelMapping(RuntimeException.class, "runtimeExceptionChannel")
747+
.channelMapping(IllegalArgumentException.class, "illegalArgumentChannel")
748748
.subFlowMapping(MessageHandlingException.class, sf ->
749749
sf.channel("messageHandlingExceptionChannel"))
750750
.defaultOutputChannel("exceptionRouterDefaultChannel"));

0 commit comments

Comments
 (0)