@ -175,7 +175,20 @@ public class EnableSchedulingTests {
@@ -175,7 +175,20 @@ public class EnableSchedulingTests {
Thread . sleep ( 110 ) ;
assertThat ( ctx . getBean ( AtomicInteger . class ) . get ( ) ) . isGreaterThanOrEqualTo ( 10 ) ;
assertThat ( ctx . getBean ( QualifiedExplicitSchedulerConfigWithPlaceholder . class ) . threadName ) . startsWith ( "explicitScheduler1" ) ;
assertThat ( ctx . getBean ( QualifiedExplicitSchedulerConfigWithPlaceholder . class ) . threadName )
. startsWith ( "explicitScheduler1" ) . isNotEqualTo ( "explicitScheduler1-1" ) ;
}
@Test
@EnabledForTestGroups ( LONG_RUNNING )
public void withQualifiedSchedulerWithFixedDelayTask ( ) throws InterruptedException {
ctx = new AnnotationConfigApplicationContext ( QualifiedExplicitSchedulerConfigWithFixedDelayTask . class ) ;
assertThat ( ctx . getBean ( ScheduledTaskHolder . class ) . getScheduledTasks ( ) ) . hasSize ( 1 ) ;
Thread . sleep ( 110 ) ;
assertThat ( ctx . getBean ( AtomicInteger . class ) . get ( ) ) . isBetween ( 4 , 5 ) ;
assertThat ( ctx . getBean ( QualifiedExplicitSchedulerConfigWithFixedDelayTask . class ) . threadName )
. isEqualTo ( "explicitScheduler1-1" ) ;
}
@Test
@ -228,7 +241,20 @@ public class EnableSchedulingTests {
@@ -228,7 +241,20 @@ public class EnableSchedulingTests {
// The @Scheduled method should have been called several times
// but not more times than the delay allows.
assertThat ( counter . get ( ) ) . isBetween ( 2 , 10 ) ;
assertThat ( counter . get ( ) ) . isBetween ( 6 , 10 ) ;
}
@Test
@EnabledForTestGroups ( LONG_RUNNING )
public void withInitiallyDelayedFixedDelayTask ( ) throws InterruptedException {
ctx = new AnnotationConfigApplicationContext ( FixedDelayTaskConfig_withInitialDelay . class ) ;
Thread . sleep ( 1950 ) ;
AtomicInteger counter = ctx . getBean ( AtomicInteger . class ) ;
// The @Scheduled method should have been called several times
// but not more times than the delay allows.
assertThat ( counter . get ( ) ) . isBetween ( 1 , 5 ) ;
}
@Test
@ -333,14 +359,14 @@ public class EnableSchedulingTests {
@@ -333,14 +359,14 @@ public class EnableSchedulingTests {
@Bean
public TaskScheduler taskScheduler1 ( ) {
SimpleAsyncTaskScheduler scheduler = new SimpleAsyncTaskScheduler ( ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler1" ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler1- " ) ;
return scheduler ;
}
@Bean
public TaskScheduler taskScheduler2 ( ) {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler ( ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler2" ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler2- " ) ;
return scheduler ;
}
@ -359,14 +385,14 @@ public class EnableSchedulingTests {
@@ -359,14 +385,14 @@ public class EnableSchedulingTests {
@Bean
public TaskScheduler taskScheduler1 ( ) {
SimpleAsyncTaskScheduler scheduler = new SimpleAsyncTaskScheduler ( ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler1" ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler1- " ) ;
return scheduler ;
}
@Bean
public TaskScheduler taskScheduler2 ( ) {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler ( ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler2" ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler2- " ) ;
return scheduler ;
}
@ -397,14 +423,14 @@ public class EnableSchedulingTests {
@@ -397,14 +423,14 @@ public class EnableSchedulingTests {
@Bean @Qualifier ( "myScheduler" )
public TaskScheduler taskScheduler1 ( ) {
SimpleAsyncTaskScheduler scheduler = new SimpleAsyncTaskScheduler ( ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler1" ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler1- " ) ;
return scheduler ;
}
@Bean
public TaskScheduler taskScheduler2 ( ) {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler ( ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler2" ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler2- " ) ;
return scheduler ;
}
@ -414,9 +440,10 @@ public class EnableSchedulingTests {
@@ -414,9 +440,10 @@ public class EnableSchedulingTests {
}
@Scheduled ( fixedRate = 10 , scheduler = "myScheduler" )
public void task ( ) {
public void task ( ) throws InterruptedException {
threadName = Thread . currentThread ( ) . getName ( ) ;
counter ( ) . incrementAndGet ( ) ;
Thread . sleep ( 10 ) ;
}
}
@ -430,14 +457,14 @@ public class EnableSchedulingTests {
@@ -430,14 +457,14 @@ public class EnableSchedulingTests {
@Bean @Qualifier ( "myScheduler" )
public TaskScheduler taskScheduler1 ( ) {
SimpleAsyncTaskScheduler scheduler = new SimpleAsyncTaskScheduler ( ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler1" ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler1- " ) ;
return scheduler ;
}
@Bean
public TaskScheduler taskScheduler2 ( ) {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler ( ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler2" ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler2- " ) ;
return scheduler ;
}
@ -447,9 +474,10 @@ public class EnableSchedulingTests {
@@ -447,9 +474,10 @@ public class EnableSchedulingTests {
}
@Scheduled ( fixedRate = 10 , scheduler = "${scheduler}" )
public void task ( ) {
public void task ( ) throws InterruptedException {
threadName = Thread . currentThread ( ) . getName ( ) ;
counter ( ) . incrementAndGet ( ) ;
Thread . sleep ( 10 ) ;
}
@Bean
@ -463,6 +491,40 @@ public class EnableSchedulingTests {
@@ -463,6 +491,40 @@ public class EnableSchedulingTests {
}
@Configuration
@EnableScheduling
static class QualifiedExplicitSchedulerConfigWithFixedDelayTask {
String threadName ;
@Bean @Qualifier ( "myScheduler" )
public TaskScheduler taskScheduler1 ( ) {
SimpleAsyncTaskScheduler scheduler = new SimpleAsyncTaskScheduler ( ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler1-" ) ;
return scheduler ;
}
@Bean
public TaskScheduler taskScheduler2 ( ) {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler ( ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler2-" ) ;
return scheduler ;
}
@Bean
public AtomicInteger counter ( ) {
return new AtomicInteger ( ) ;
}
@Scheduled ( fixedDelay = 10 , scheduler = "myScheduler" )
public void task ( ) throws InterruptedException {
threadName = Thread . currentThread ( ) . getName ( ) ;
counter ( ) . incrementAndGet ( ) ;
Thread . sleep ( 10 ) ;
}
}
@Configuration
@EnableScheduling
static class SchedulingEnabled_withAmbiguousTaskSchedulers_butNoActualTasks {
@ -470,14 +532,14 @@ public class EnableSchedulingTests {
@@ -470,14 +532,14 @@ public class EnableSchedulingTests {
@Bean
public TaskScheduler taskScheduler1 ( ) {
SimpleAsyncTaskScheduler scheduler = new SimpleAsyncTaskScheduler ( ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler1" ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler1- " ) ;
return scheduler ;
}
@Bean
public TaskScheduler taskScheduler2 ( ) {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler ( ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler2" ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler2- " ) ;
return scheduler ;
}
}
@ -494,7 +556,7 @@ public class EnableSchedulingTests {
@@ -494,7 +556,7 @@ public class EnableSchedulingTests {
@Bean
public TaskScheduler taskScheduler1 ( ) {
SimpleAsyncTaskScheduler scheduler = new SimpleAsyncTaskScheduler ( ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler1" ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler1- " ) ;
scheduler . setConcurrencyLimit ( 1 ) ;
return scheduler ;
}
@ -502,7 +564,7 @@ public class EnableSchedulingTests {
@@ -502,7 +564,7 @@ public class EnableSchedulingTests {
@Bean
public TaskScheduler taskScheduler2 ( ) {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler ( ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler2" ) ;
scheduler . setThreadNamePrefix ( "explicitScheduler2- " ) ;
return scheduler ;
}
}
@ -620,8 +682,26 @@ public class EnableSchedulingTests {
@@ -620,8 +682,26 @@ public class EnableSchedulingTests {
}
@Scheduled ( initialDelay = 1000 , fixedRate = 100 )
public void task ( ) {
public void task ( ) throws InterruptedException {
counter ( ) . incrementAndGet ( ) ;
Thread . sleep ( 100 ) ;
}
}
@Configuration
@EnableScheduling
static class FixedDelayTaskConfig_withInitialDelay {
@Bean
public AtomicInteger counter ( ) {
return new AtomicInteger ( ) ;
}
@Scheduled ( initialDelay = 1000 , fixedDelay = 100 )
public void task ( ) throws InterruptedException {
counter ( ) . incrementAndGet ( ) ;
Thread . sleep ( 100 ) ;
}
}