Skip to content

Scheduled tasks aren't cancelled on ScheduledTaskRegistrar destruction anymore [SPR-14286] #18858

@spring-projects-issues

Description

@spring-projects-issues

Romain Moreau opened SPR-14286 and commented

With Spring 4.3.0.RC1, when ScheduledTaskRegistrar was destroyed, scheduled futures were cancelled correctly.

Now, with Spring 4.3.0.RC2, when ScheduledTaskRegistrar is destroyed, scheduled tasks aren't cancelled because the scheduledTasks collection used to iterate the tasks to cancel is empty.

Here's below the code for a quick and dirty reproduction case.
To run with Spring 4.3.0.RC1, replace the spring-boot-starter-parent version with 1.4.0.M2.
With Spring 4.3.0.RC1, you'll see in the log "1 scheduled found" as expected.
With Spring 4.3.0.RC2, you'll see in the log "0 scheduled found" (scheduledTasks set is empty).

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.M3</version>
  </parent>
  <artifactId>scheduled-repro</artifactId>
  <properties>
    <!-- Needed because of a Maven issue on my side, not affecting reproduction case -->
    <spring-integration.version>4.2.6.RELEASE</spring-integration.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
      <id>spring-milestones</id>
      <url>http://repo.spring.io/milestone</url>
      <releases>
        <enabled>true</enabled>
      </releases>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>spring-milestones</id>
      <url>http://repo.spring.io/milestone</url>
      <releases>
        <enabled>true</enabled>
      </releases>
    </pluginRepository>
  </pluginRepositories>
</project>
package scheduledrepro;

import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.scheduling.annotation.*;
import org.springframework.scheduling.config.*;
import org.springframework.scheduling.concurrent.*;
import org.springframework.context.annotation.*;
import org.springframework.boot.context.event.*;
import org.springframework.context.event.*;
import org.springframework.util.*;
import java.lang.*;
import java.lang.reflect.*;
import java.util.*;
import org.slf4j.*;

@EnableScheduling
@SpringBootApplication
public class ScheduledReproApplication implements SchedulingConfigurer {
 private static final Logger LOGGER = LoggerFactory.getLogger(ScheduledReproApplication.class);

 private ScheduledTaskRegistrar taskRegistrar;

 @Override
 public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
  this.taskRegistrar = taskRegistrar;
  taskRegistrar.setScheduler(threadPoolTaskScheduler());
 }

 @Bean
 public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
  return new ThreadPoolTaskScheduler();
 }

 @org.springframework.context.event.EventListener
 public void applicationReadyEvent(ApplicationReadyEvent applicationReadyEvent) throws Exception {
  Field field = ReflectionUtils.findField(ScheduledTaskRegistrar.class, "scheduledTasks"); // Spring 4.3.0.RC2
  if (field == null) {
   field = ReflectionUtils.findField(ScheduledTaskRegistrar.class, "scheduledFutures"); // Spring 4.3.0.RC1
  }
  field.setAccessible(true);
  Set < ? > scheduledTasks = (Set < ? > ) field.get(taskRegistrar);
  LOGGER.info("{} scheduled found", scheduledTasks.size());
 }

 @Scheduled(cron = "* * * * * *")
 public void scheduled() {
  LOGGER.info("Scheduled");
 }

 public static void main(String[] args) throws Exception {
  SpringApplication.run(ScheduledReproApplication.class, args);
 }
}

Affects: 4.3 RC2

Issue Links:

Referenced from: commits 3576ff0

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: regressionA bug that is also a regression

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions