Skip to content

Commit d803e75

Browse files
Cameronjmayfieldartembilan
authored andcommitted
GH-2699: Make expressions cache concurrent
Fixes #2699 * [GH-2699] Match style * [GH-2699] Shorten test line length **Cherry-pick to 5.0.x** (cherry picked from commit cd8cbaa)
1 parent c64524b commit d803e75

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

spring-integration-core/src/main/java/org/springframework/integration/aop/MethodAnnotationPublisherMetadataSource.java

+6-4
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.
@@ -22,6 +22,7 @@
2222
import java.util.HashMap;
2323
import java.util.Map;
2424
import java.util.Set;
25+
import java.util.concurrent.ConcurrentHashMap;
2526
import java.util.stream.Collectors;
2627

2728
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
@@ -42,18 +43,19 @@
4243
* @author Mark Fisher
4344
* @author Artem Bilan
4445
* @author Gareth Chapman
46+
* @author Cameron Mayfield
4547
*
4648
* @since 2.0
4749
*/
4850
public class MethodAnnotationPublisherMetadataSource implements PublisherMetadataSource {
4951

5052
private final ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
5153

52-
private final Map<Method, String> channels = new HashMap<>();
54+
private final Map<Method, String> channels = new ConcurrentHashMap<>();
5355

54-
private final Map<Method, Expression> payloadExpressions = new HashMap<>();
56+
private final Map<Method, Expression> payloadExpressions = new ConcurrentHashMap<>();
5557

56-
private final Map<Method, Map<String, Expression>> headersExpressions = new HashMap<>();
58+
private final Map<Method, Map<String, Expression>> headersExpressions = new ConcurrentHashMap<>();
5759

5860
private final Set<Class<? extends Annotation>> annotationTypes;
5961

spring-integration-core/src/test/java/org/springframework/integration/aop/MethodAnnotationPublisherMetadataSourceTests.java

+16-4
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.
@@ -16,13 +16,16 @@
1616

1717
package org.springframework.integration.aop;
1818

19+
import static org.hamcrest.Matchers.instanceOf;
1920
import static org.junit.Assert.assertEquals;
2021
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertThat;
2123

2224
import java.lang.annotation.Retention;
2325
import java.lang.annotation.RetentionPolicy;
2426
import java.lang.reflect.Method;
2527
import java.util.Map;
28+
import java.util.concurrent.ConcurrentHashMap;
2629

2730
import org.junit.Test;
2831

@@ -31,18 +34,19 @@
3134
import org.springframework.integration.annotation.Publisher;
3235
import org.springframework.messaging.handler.annotation.Header;
3336
import org.springframework.messaging.handler.annotation.Payload;
37+
import org.springframework.test.util.ReflectionTestUtils;
3438

3539
/**
3640
* @author Mark Fisher
3741
* @author Artem Bilan
42+
* @author Cameron Mayfield
3843
*
3944
* @since 2.0
4045
*/
4146
public class MethodAnnotationPublisherMetadataSourceTests {
4247

4348
private final MethodAnnotationPublisherMetadataSource source = new MethodAnnotationPublisherMetadataSource();
4449

45-
4650
@Test
4751
public void channelNameAndExplicitReturnValuePayload() {
4852
Method method = getMethod("methodWithChannelAndExplicitReturnAsPayload");
@@ -83,6 +87,16 @@ public void payloadAndHeaders() {
8387
assertEquals("#args[2]", headerMap.get("bar").getExpressionString());
8488
}
8589

90+
@Test
91+
public void expressionsAreConcurrentHashMap() {
92+
assertThat("Expressions should be concurrent to allow startup",
93+
ReflectionTestUtils.getField(source, "channels"), instanceOf(ConcurrentHashMap.class));
94+
assertThat("Expressions should be concurrent to allow startup",
95+
ReflectionTestUtils.getField(source, "payloadExpressions"), instanceOf(ConcurrentHashMap.class));
96+
assertThat("Expressions should be concurrent to allow startup",
97+
ReflectionTestUtils.getField(source, "headersExpressions"), instanceOf(ConcurrentHashMap.class));
98+
}
99+
86100
@Test
87101
public void voidReturnWithValidPayloadExpression() {
88102
Method method = getMethod("methodWithVoidReturnAndMethodNameAsPayload");
@@ -142,7 +156,6 @@ private static Method getMethodFromClass(Class<?> sourceClass, String name, Clas
142156
}
143157
}
144158

145-
146159
@Publisher
147160
@Payload("testExpression1")
148161
public void methodWithPayloadAnnotation(String arg1, int arg2) {
@@ -160,7 +173,6 @@ public String methodWithChannelAndEmptyPayloadAnnotation() {
160173
return "hello";
161174
}
162175

163-
164176
@Publisher(channel = "foo")
165177
@Payload("#method")
166178
public void methodWithVoidReturnAndMethodNameAsPayload() {

0 commit comments

Comments
 (0)