Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* {@link org.springframework.aop.interceptor.ConcurrencyThrottleInterceptor}.
*
* @author Juergen Hoeller
* @author Hyunsang Han
* @since 7.0
* @see EnableResilientMethods
* @see ConcurrencyLimitBeanPostProcessor
Expand All @@ -62,4 +63,12 @@
*/
int value() default 1;

/**
* The concurrency limit as a configurable String.
* A non-empty value specified here overrides the {@link #value()} attribute.
* <p>This supports Spring-style "${...}" placeholders as well as SpEL expressions.
* @see #value()
*/
String valueString() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,27 @@
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.util.Assert;
import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.StringUtils;
import org.springframework.util.StringValueResolver;

/**
* A convenient {@link org.springframework.beans.factory.config.BeanPostProcessor
* BeanPostProcessor} that applies a concurrency interceptor to all bean methods
* annotated with {@link ConcurrencyLimit @ConcurrencyLimit}.
*
* @author Juergen Hoeller
* @author Hyunsang Han
* @since 7.0
*/
@SuppressWarnings("serial")
public class ConcurrencyLimitBeanPostProcessor extends AbstractBeanFactoryAwareAdvisingPostProcessor {
public class ConcurrencyLimitBeanPostProcessor extends AbstractBeanFactoryAwareAdvisingPostProcessor
implements EmbeddedValueResolverAware {

private @Nullable StringValueResolver embeddedValueResolver;

public ConcurrencyLimitBeanPostProcessor() {
setBeforeExistingAdvisors(true);
Expand All @@ -57,7 +64,13 @@ public ConcurrencyLimitBeanPostProcessor() {
}


private static class ConcurrencyLimitInterceptor implements MethodInterceptor {
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.embeddedValueResolver = resolver;
}


private class ConcurrencyLimitInterceptor implements MethodInterceptor {

private final Map<Object, ConcurrencyThrottleCache> cachePerInstance =
new ConcurrentReferenceHashMap<>(16, ConcurrentReferenceHashMap.ReferenceType.WEAK);
Expand Down Expand Up @@ -93,7 +106,8 @@ private static class ConcurrencyLimitInterceptor implements MethodInterceptor {
}
if (interceptor == null) {
Assert.state(limit != null, "No @ConcurrencyLimit annotation found");
interceptor = new ConcurrencyThrottleInterceptor(limit.value());
int concurrencyLimit = parseInt(limit.value(), limit.valueString());
interceptor = new ConcurrencyThrottleInterceptor(concurrencyLimit);
if (!perMethod) {
cache.classInterceptor = interceptor;
}
Expand All @@ -104,6 +118,18 @@ private static class ConcurrencyLimitInterceptor implements MethodInterceptor {
}
return interceptor.invoke(invocation);
}

private int parseInt(int value, String stringValue) {
if (StringUtils.hasText(stringValue)) {
if (embeddedValueResolver != null) {
stringValue = embeddedValueResolver.resolveStringValue(stringValue);
}
if (StringUtils.hasText(stringValue)) {
return Integer.parseInt(stringValue);
}
}
return value;
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;

Expand All @@ -28,13 +29,17 @@
import org.springframework.aop.interceptor.ConcurrencyThrottleInterceptor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.resilience.annotation.ConcurrencyLimit;
import org.springframework.resilience.annotation.ConcurrencyLimitBeanPostProcessor;
import org.springframework.resilience.annotation.EnableResilientMethods;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Juergen Hoeller
* @author Hyunsang Han
* @since 7.0
*/
class ConcurrencyLimitTests {
Expand Down Expand Up @@ -97,6 +102,28 @@ void withPostProcessorForClass() {
assertThat(target.current).hasValue(0);
}

@Test
void withPlaceholderResolution() {
Properties props = new Properties();
props.setProperty("test.concurrency.limit", "3");

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("test", props));
ctx.register(PlaceholderTestConfig.class, PlaceholderBean.class);
ctx.refresh();

PlaceholderBean proxy = ctx.getBean(PlaceholderBean.class);
PlaceholderBean target = (PlaceholderBean) AopProxyUtils.getSingletonTarget(proxy);

// Test with limit=3 from properties
List<CompletableFuture<?>> futures = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
futures.add(CompletableFuture.runAsync(proxy::concurrentOperation));
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
assertThat(target.current).hasValue(0);
ctx.close();
}

static class NonAnnotatedBean {

Expand Down Expand Up @@ -185,4 +212,29 @@ public void overrideOperation() {
}
}


@EnableResilientMethods
static class PlaceholderTestConfig {
}


static class PlaceholderBean {

AtomicInteger current = new AtomicInteger();

@ConcurrencyLimit(valueString = "${test.concurrency.limit}")
public void concurrentOperation() {
if (current.incrementAndGet() > 3) { // Assumes test.concurrency.limit=3
throw new IllegalStateException();
}
try {
Thread.sleep(100);
}
catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
current.decrementAndGet();
}
}

}