Browse Source

SimpleAsyncTaskExecutor properly respects NO_CONCURRENCY

Issue: SPR-15895
pull/1504/merge
Juergen Hoeller 9 years ago
parent
commit
204ddebd68
  1. 5
      spring-core/src/main/java/org/springframework/core/task/SimpleAsyncTaskExecutor.java
  2. 4
      spring-core/src/main/java/org/springframework/util/ConcurrencyThrottleSupport.java
  3. 9
      spring-core/src/test/java/org/springframework/core/task/SimpleAsyncTaskExecutorTests.java

5
spring-core/src/main/java/org/springframework/core/task/SimpleAsyncTaskExecutor.java

@ -48,15 +48,18 @@ import org.springframework.util.concurrent.ListenableFutureTask;
* @see org.springframework.scheduling.commonj.WorkManagerTaskExecutor * @see org.springframework.scheduling.commonj.WorkManagerTaskExecutor
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class SimpleAsyncTaskExecutor extends CustomizableThreadCreator implements AsyncListenableTaskExecutor, Serializable { public class SimpleAsyncTaskExecutor extends CustomizableThreadCreator
implements AsyncListenableTaskExecutor, Serializable {
/** /**
* Permit any number of concurrent invocations: that is, don't throttle concurrency. * Permit any number of concurrent invocations: that is, don't throttle concurrency.
* @see ConcurrencyThrottleSupport#UNBOUNDED_CONCURRENCY
*/ */
public static final int UNBOUNDED_CONCURRENCY = ConcurrencyThrottleSupport.UNBOUNDED_CONCURRENCY; public static final int UNBOUNDED_CONCURRENCY = ConcurrencyThrottleSupport.UNBOUNDED_CONCURRENCY;
/** /**
* Switch concurrency 'off': that is, don't allow any concurrent invocations. * Switch concurrency 'off': that is, don't allow any concurrent invocations.
* @see ConcurrencyThrottleSupport#NO_CONCURRENCY
*/ */
public static final int NO_CONCURRENCY = ConcurrencyThrottleSupport.NO_CONCURRENCY; public static final int NO_CONCURRENCY = ConcurrencyThrottleSupport.NO_CONCURRENCY;

4
spring-core/src/main/java/org/springframework/util/ConcurrencyThrottleSupport.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -93,7 +93,7 @@ public abstract class ConcurrencyThrottleSupport implements Serializable {
* @see #getConcurrencyLimit() * @see #getConcurrencyLimit()
*/ */
public boolean isThrottleActive() { public boolean isThrottleActive() {
return (this.concurrencyLimit > 0); return (this.concurrencyLimit >= 0);
} }

9
spring-core/src/test/java/org/springframework/core/task/SimpleAsyncTaskExecutorTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,14 +18,13 @@ package org.springframework.core.task;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import org.junit.Ignore;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.util.ConcurrencyThrottleSupport; import org.springframework.util.ConcurrencyThrottleSupport;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
@ -39,13 +38,11 @@ public class SimpleAsyncTaskExecutorTests {
public final ExpectedException exception = ExpectedException.none(); public final ExpectedException exception = ExpectedException.none();
// TODO Determine why task is executed when concurrency is switched off.
@Ignore("Disabled because task is still executed when concurrency is switched off")
@Test @Test
public void cannotExecuteWhenConcurrencyIsSwitchedOff() throws Exception { public void cannotExecuteWhenConcurrencyIsSwitchedOff() throws Exception {
SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor(); SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor();
executor.setConcurrencyLimit(ConcurrencyThrottleSupport.NO_CONCURRENCY); executor.setConcurrencyLimit(ConcurrencyThrottleSupport.NO_CONCURRENCY);
assertFalse(executor.isThrottleActive()); assertTrue(executor.isThrottleActive());
exception.expect(IllegalStateException.class); exception.expect(IllegalStateException.class);
executor.execute(new NoOpRunnable()); executor.execute(new NoOpRunnable());
} }

Loading…
Cancel
Save