From d803e7564754bc9d31f13d5504cff82c360c0e16 Mon Sep 17 00:00:00 2001 From: Cameron Mayfield Date: Wed, 16 Jan 2019 18:03:03 -0500 Subject: [PATCH] GH-2699: Make expressions cache concurrent Fixes spring-projects/spring-integration#2699 * [GH-2699] Match style * [GH-2699] Shorten test line length **Cherry-pick to 5.0.x** (cherry picked from commit cd8cbaa99c6fcef62eaf875564789a0e48e14972) --- ...thodAnnotationPublisherMetadataSource.java | 10 ++++++---- ...nnotationPublisherMetadataSourceTests.java | 20 +++++++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/aop/MethodAnnotationPublisherMetadataSource.java b/spring-integration-core/src/main/java/org/springframework/integration/aop/MethodAnnotationPublisherMetadataSource.java index ffe9e8e082..0c4df0ab88 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/aop/MethodAnnotationPublisherMetadataSource.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/aop/MethodAnnotationPublisherMetadataSource.java @@ -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. @@ -22,6 +22,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; import org.springframework.core.LocalVariableTableParameterNameDiscoverer; @@ -42,6 +43,7 @@ import org.springframework.util.StringUtils; * @author Mark Fisher * @author Artem Bilan * @author Gareth Chapman + * @author Cameron Mayfield * * @since 2.0 */ @@ -49,11 +51,11 @@ public class MethodAnnotationPublisherMetadataSource implements PublisherMetadat private final ParameterNameDiscoverer parameterNameDiscoverer = new LocalVariableTableParameterNameDiscoverer(); - private final Map channels = new HashMap<>(); + private final Map channels = new ConcurrentHashMap<>(); - private final Map payloadExpressions = new HashMap<>(); + private final Map payloadExpressions = new ConcurrentHashMap<>(); - private final Map> headersExpressions = new HashMap<>(); + private final Map> headersExpressions = new ConcurrentHashMap<>(); private final Set> annotationTypes; diff --git a/spring-integration-core/src/test/java/org/springframework/integration/aop/MethodAnnotationPublisherMetadataSourceTests.java b/spring-integration-core/src/test/java/org/springframework/integration/aop/MethodAnnotationPublisherMetadataSourceTests.java index 858392fa84..9ae1b0f68f 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/aop/MethodAnnotationPublisherMetadataSourceTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/aop/MethodAnnotationPublisherMetadataSourceTests.java @@ -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. @@ -16,13 +16,16 @@ package org.springframework.integration.aop; +import static org.hamcrest.Matchers.instanceOf; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.junit.Test; @@ -31,10 +34,12 @@ import org.springframework.expression.Expression; import org.springframework.integration.annotation.Publisher; import org.springframework.messaging.handler.annotation.Header; import org.springframework.messaging.handler.annotation.Payload; +import org.springframework.test.util.ReflectionTestUtils; /** * @author Mark Fisher * @author Artem Bilan + * @author Cameron Mayfield * * @since 2.0 */ @@ -42,7 +47,6 @@ public class MethodAnnotationPublisherMetadataSourceTests { private final MethodAnnotationPublisherMetadataSource source = new MethodAnnotationPublisherMetadataSource(); - @Test public void channelNameAndExplicitReturnValuePayload() { Method method = getMethod("methodWithChannelAndExplicitReturnAsPayload"); @@ -83,6 +87,16 @@ public class MethodAnnotationPublisherMetadataSourceTests { assertEquals("#args[2]", headerMap.get("bar").getExpressionString()); } + @Test + public void expressionsAreConcurrentHashMap() { + assertThat("Expressions should be concurrent to allow startup", + ReflectionTestUtils.getField(source, "channels"), instanceOf(ConcurrentHashMap.class)); + assertThat("Expressions should be concurrent to allow startup", + ReflectionTestUtils.getField(source, "payloadExpressions"), instanceOf(ConcurrentHashMap.class)); + assertThat("Expressions should be concurrent to allow startup", + ReflectionTestUtils.getField(source, "headersExpressions"), instanceOf(ConcurrentHashMap.class)); + } + @Test public void voidReturnWithValidPayloadExpression() { Method method = getMethod("methodWithVoidReturnAndMethodNameAsPayload"); @@ -142,7 +156,6 @@ public class MethodAnnotationPublisherMetadataSourceTests { } } - @Publisher @Payload("testExpression1") public void methodWithPayloadAnnotation(String arg1, int arg2) { @@ -160,7 +173,6 @@ public class MethodAnnotationPublisherMetadataSourceTests { return "hello"; } - @Publisher(channel = "foo") @Payload("#method") public void methodWithVoidReturnAndMethodNameAsPayload() {