Creating an Auto‑Register Spring Boot Starter for XXL‑Job Executors and JobHandlers
This article explains how to build a Spring Boot starter that automatically registers XXL‑Job executors and job handlers by logging into the admin center, invoking the necessary REST APIs, and using custom annotations to capture job metadata, eliminating manual configuration for large numbers of scheduled tasks.
XXL‑Job is a lightweight distributed task‑scheduling middleware widely used in Java projects, but manually configuring executors and jobs through its admin UI becomes tedious when the number of tasks grows.
To remove this manual step, a Spring Boot starter is implemented that automatically registers executors and job handlers when the application starts.
The starter first logs into the XXL‑Job admin center to obtain the XXL_JOB_LOGIN_IDENTITY cookie, caching it for subsequent API calls.
Key admin endpoints used are /jobgroup/save , /jobgroup/pageList , /jobinfo/add and /jobinfo/pageList . These are wrapped in service classes such as JobGroupService and JobInfoService to handle HTTP requests and response parsing.
A custom annotation @XxlRegister is defined to provide additional metadata (cron expression, job description, author, trigger status). Methods annotated with both @XxlJob and @XxlRegister are discovered via the Spring ApplicationContext and registered only if they are not already present in the scheduler.
The core auto‑registration component implements ApplicationListener . In its onApplicationEvent method it calls addJobGroup() to register the executor and addJobInfo() to register each qualified job handler.
Example code snippets: private final Map loginCookie = new HashMap<>(); public void login(){ String url = adminAddresses + "/login"; HttpResponse response = HttpRequest.post(url) .form("userName", username) .form("password", password) .execute(); // extract XXL_JOB_LOGIN_IDENTITY cookie } private void addJobGroup(){ if (jobGroupService.preciselyCheck()) return; if (jobGroupService.autoRegisterGroup()) log.info("auto register xxl-job group success!"); } @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface XxlRegister { String cron(); String jobDesc() default "default jobDesc"; String author() default "default Author"; int triggerStatus() default 0; } After packaging the starter as a Maven artifact and adding it to META-INF/spring.factories , developers can include the dependency, configure the required XXL‑Job properties, and let the starter handle executor and job registration automatically.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
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.