Dynamic Management of SpringBoot @Scheduled Tasks with SuperScheduled Starter
This article presents SuperScheduled, a SpringBoot starter that enhances native @Scheduled tasks by enabling dynamic runtime management, custom cron, fixedDelay and fixedRate adjustments, and a plug‑in AOP‑like enhancement mechanism without modifying existing scheduled methods.
This article introduces SuperScheduled, a SpringBoot‑starter that strengthens the native @Scheduled annotation by providing dynamic management of scheduled tasks without altering the original code.
Feature Overview
All functionality is packaged as a SpringBoot‑starter, making it plug‑and‑play compatible with existing @Scheduled methods.
Quick Usage
Add the Maven dependency to your project:
<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
The configuration manager ( SuperScheduledConfig ) holds mappings between task names, their ScheduledFuture , the original ScheduledSource , and the runnable logic.
2. Bean Post‑Processor
A BeanPostProcessor intercepts every bean after initialization, scans for methods annotated with @Scheduled , extracts the annotation, disables the original scheduling parameters, and registers the task in the configuration manager.
@Component("superScheduledConfig")
public class SuperScheduledConfig {
/** Execution thread pool */
private ThreadPoolTaskScheduler taskScheduler;
/** name → ScheduledFuture */
private Map
nameToScheduledFuture = new ConcurrentHashMap<>();
/** name → Runnable */
private Map
nameToRunnable = new ConcurrentHashMap<>();
/** name → source info */
private Map
nameToScheduledSource = new ConcurrentHashMap<>();
/* getters/setters omitted */
}3. ApplicationRunner Initialization
After the Spring context is fully initialized, SuperScheduledApplicationRunner reads all registered sources, builds a chain of enhancement points, creates a SuperScheduledRunnable for each task, and schedules them via ScheduledFutureFactory .
@Component
@Order
public class SuperScheduledApplicationRunner implements ApplicationRunner, ApplicationContextAware {
@Autowired
private SuperScheduledConfig superScheduledConfig;
@Autowired
private ThreadPoolTaskScheduler threadPoolTaskScheduler;
@Override
public void run(ApplicationArguments args) {
superScheduledConfig.setTaskScheduler(threadPoolTaskScheduler);
for (String name : superScheduledConfig.getNameToScheduledSource().keySet()) {
ScheduledSource source = superScheduledConfig.getNameToScheduledSource().get(name);
SuperScheduledRunnable runnable = new SuperScheduledRunnable();
runnable.setMethod(source.getMethod());
runnable.setBean(source.getBean());
// build point chain ...
superScheduledConfig.addRunnable(name, runnable::invoke);
ScheduledFuture
schedule = ScheduledFutureFactory.create(threadPoolTaskScheduler, source, runnable::invoke);
superScheduledConfig.addScheduledFuture(name, schedule);
}
}
}4. Enhancement Interface
Users can implement BaseStrengthen to add before, after, exception and finally logic. These enhancers are turned into Point objects via CGLIB proxy ( RunnableBaseInterceptor ) and linked into a Chain that the runnable executes sequentially.
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);
}5. Runtime Control API
The SuperScheduledManager provides methods to modify cron expressions, fixedDelay, fixedRate, start, stop, and manually trigger tasks at runtime.
public void setScheduledCron(String name, String cron) { /* cancel & recreate */ }
public void cancelScheduled(String name) { /* cancel future */ }
public void runScheduled(String name) { /* invoke once */ }Overall, SuperScheduled offers a comprehensive solution for dynamic scheduling, plug‑in enhancements, and runtime task management in SpringBoot backend applications.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.