Backend Development 14 min read

Spring Boot Super Scheduled: Dynamic Management and Enhancement of @Scheduled Tasks

This article introduces a Spring Boot starter that enhances native @Scheduled tasks with dynamic management capabilities, explains how to integrate the dependency, and details the underlying implementation using post‑processors, application runners, custom managers, and CGLIB‑based proxy chains to modify scheduling parameters at runtime.

Architecture Digest
Architecture Digest
Architecture Digest
Spring Boot Super Scheduled: Dynamic Management and Enhancement of @Scheduled Tasks

Function Description

Spring Boot's enhanced scheduled‑task tool provides dynamic management for native @Scheduled tasks, fully compatible with the original annotation and requiring no changes to existing jobs.

Quick Start

Add the starter as a Maven dependency:

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

Source code and usage examples are available on Gitee and GitHub.

Implementation Principle

1. Dynamic Management

Configuration management is introduced via @Component("superScheduledConfig") , storing mappings between task names, callbacks, and source metadata.

@Component("superScheduledConfig")
public class SuperScheduledConfig {
    private ThreadPoolTaskScheduler taskScheduler;
    private Map
nameToScheduledFuture = new ConcurrentHashMap<>();
    private Map
nameToRunnable = new ConcurrentHashMap<>();
    private Map
nameToScheduledSource = new ConcurrentHashMap<>();
    // getters/setters omitted
}

A post‑processor intercepts Spring's bean creation, extracts @Scheduled annotations, registers their metadata, and disables the original scheduling.

@Component
@Order
public class SuperScheduledPostProcessor implements BeanPostProcessor, ApplicationContextAware {
    // ...postProcessAfterInitialization extracts @Scheduled, creates ScheduledSource, disables original task, and stores metadata.
}

2. ApplicationRunner Initialization

After all beans are created, SuperScheduledApplicationRunner builds a runnable for each task, assembles a chain of strengtheners, and schedules the task using ThreadPoolTaskScheduler .

@Component
@DependsOn("threadPoolTaskScheduler")
public class SuperScheduledApplicationRunner implements ApplicationRunner, ApplicationContextAware {
    // ...run() creates SuperScheduledRunnable, builds Point chain, and starts ScheduledFuture.
}

3. Management Operations

SuperScheduledManager provides methods to modify cron, fixedDelay, or fixedRate at runtime, cancel, and restart tasks.

public void setScheduledCron(String name, String cron) { /* cancel & recreate */ }
public void cancelScheduled(String name) { /* cancel future */ }
public void addScheduled(String name, ScheduledSource source) { /* schedule new task */ }

4. Strengthen Interface and Proxy Chain

The BaseStrengthen interface defines before, after, exception, and afterFinally hooks. Implementations are proxied into Point objects via CGLIB, forming a Chain that the runnable traverses.

public interface BaseStrengthen {
    void before(Object bean, Method method, Object[] args);
    void after(Object bean, Method method, Object[] args);
    void exception(Object bean, Method method, Object[] args);
    void afterFinally(Object bean, Method method, Object[] args);
}

The RunnableBaseInterceptor invokes these hooks around the original task execution.

public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
    if ("invoke".equals(method.getName())) {
        strengthen.before(...);
        try { result = runnable.invoke(); }
        catch (Exception e) { strengthen.exception(...); throw new SuperScheduledException(...); }
        finally { strengthen.afterFinally(...); }
        strengthen.after(...);
    } else {
        result = proxy.invokeSuper(obj, args);
    }
    return result;
}
JavaAOPSpring BootCglibscheduled tasksdynamic-management
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

login 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.