Dynamic Management of SpringBoot @Scheduled Tasks with SuperScheduled

This article explains how to enhance SpringBoot's native @Scheduled tasks with the spring-boot-starter-super-scheduled library, allowing dynamic creation, modification, and cancellation of scheduled jobs at runtime without altering existing annotations, and details the underlying implementation using BeanPostProcessor, CGLIB proxies, and a custom task manager.

Architect
Architect
Architect
Dynamic Management of SpringBoot @Scheduled Tasks with SuperScheduled

SpringBoot’s native @Scheduled tasks can be dynamically managed without modifying existing annotations by using the spring-boot-starter-super-scheduled library.

First, add the starter dependency:

<dependency>
    <groupId>com.github.guoyixing</groupId>
    <artifactId>spring-boot-starter-super-scheduled</artifactId>
    <version>0.3.1</version>
</dependency>

The core of the solution is a configuration bean ( SuperScheduledConfig) that stores a thread‑pool scheduler and mappings from task name to ScheduledFuture, runnable, and source metadata.

Dynamic management is achieved by a BeanPostProcessor ( SuperScheduledPostProcessor) that implements ApplicationContextAware. During postProcessAfterInitialization, it scans every bean’s methods, extracts the @Scheduled annotation, validates the parameters, registers a ScheduledSource object, and disables the original scheduling by resetting annotation attributes (cron, fixedDelay, etc.) via reflection.

Task execution is delegated to SuperScheduledRunnable. The runnable holds the target method, bean instance, and a Chain of Point objects representing enhancement interceptors. When invoke() is called, the chain iterates; each Point proxies a user‑defined BaseStrengthen implementation that provides before, after, exception, and afterFinally hooks. The interceptor ( RunnableBaseInterceptor) invokes these hooks around the original method execution, handling exceptions and ensuring finally logic.

An ApplicationRunner ( SuperScheduledApplicationRunner) runs after all beans are initialized. It retrieves all ScheduledSource entries, discovers all BaseStrengthen beans, builds the Chain, creates a SuperScheduledRunnable, and schedules the task using ScheduledFutureFactory.create. It also logs task start information.

The SuperScheduledManager offers a public API to modify a task’s cron expression, fixedDelay, or fixedRate at runtime, to query running or defined tasks, to cancel a task, or to trigger a task manually. Each modification first cancels the existing ScheduledFuture, clears the old source, sets the new timing parameters, and re‑adds the task through the same scheduling flow.

Enhancement interfaces are defined by BaseStrengthen, which declares four lifecycle methods. Implementations are turned into Point proxies via CGLIB ( ProxyUtils.getInstance) and linked into the Chain. The chain logic ensures that all enhancements run in order before the original method, and that after‑finally logic always executes.

Repository links for the source code are provided (Gitee and GitHub), allowing developers to explore the full implementation.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaaopspringSpringBootcglibScheduled TasksDynamic Management
Architect
Written by

Architect

Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.