A distributed task scheduling framework core library for Java applications
—
Executor lifecycle management for both Spring and standalone environments. Handles registration with admin servers, job thread management, and server communication.
Main executor class that manages job execution lifecycle, communication with admin servers, and embedded HTTP server.
/**
* Core executor class for managing job execution and lifecycle
* Handles registration with admin servers and job thread management
*/
public class XxlJobExecutor {
// Configuration methods
/**
* Set admin server addresses (comma-separated for multiple servers)
* @param adminAddresses - Admin server URLs, e.g., "http://127.0.0.1:8080/xxl-job-admin"
*/
public void setAdminAddresses(String adminAddresses);
/**
* Set access token for authentication with admin servers
* @param accessToken - Token for secure communication
*/
public void setAccessToken(String accessToken);
/**
* Set timeout for network operations in seconds
* @param timeout - Timeout value (default: 30 seconds)
*/
public void setTimeout(int timeout);
/**
* Set application name for executor identification
* @param appname - Unique application name for this executor
*/
public void setAppname(String appname);
/**
* Set executor address for admin callback (optional, auto-detected if not set)
* @param address - Full executor address including port
*/
public void setAddress(String address);
/**
* Set executor IP address (optional, auto-detected if not set)
* @param ip - IP address for admin callback
*/
public void setIp(String ip);
/**
* Set executor port for embedded HTTP server
* @param port - Port number for receiving job triggers
*/
public void setPort(int port);
/**
* Set log file storage path
* @param logPath - Directory path for job execution logs
*/
public void setLogPath(String logPath);
/**
* Set log retention period in days
* @param logRetentionDays - Number of days to keep log files
*/
public void setLogRetentionDays(int logRetentionDays);
// Lifecycle methods
/**
* Start the executor and register with admin servers
* Initializes embedded HTTP server and begins heartbeat
* @throws Exception if startup fails
*/
public void start() throws Exception;
/**
* Stop the executor and cleanup resources
* Unregisters from admin servers and stops embedded server
*/
public void destroy();
// Static management methods
/**
* Get list of admin server communication clients
* @return List of AdminBiz clients for admin server communication
*/
public static List<AdminBiz> getAdminBizList();
/**
* Load job handler by name
* @param name - Job handler name
* @return IJobHandler instance or null if not found
*/
public static IJobHandler loadJobHandler(String name);
/**
* Register job handler with given name
* @param name - Job handler name for registration
* @param jobHandler - IJobHandler implementation
* @return Previously registered handler or null
*/
public static IJobHandler registJobHandler(String name, IJobHandler jobHandler);
/**
* Register job thread for specific job ID
* @param jobId - Unique job identifier
* @param handler - Job handler to execute
* @param removeOldReason - Reason for removing old thread (if exists)
* @return JobThread instance
*/
public static JobThread registJobThread(int jobId, IJobHandler handler, String removeOldReason);
/**
* Remove job thread for specific job ID
* @param jobId - Job identifier
* @param removeOldReason - Reason for removal
* @return Removed JobThread or null
*/
public static JobThread removeJobThread(int jobId, String removeOldReason);
/**
* Load existing job thread for specific job ID
* @param jobId - Job identifier
* @return JobThread instance or null if not found
*/
public static JobThread loadJobThread(int jobId);
}Spring-integrated executor with automatic discovery of @XxlJob annotated methods and Spring lifecycle management.
/**
* Spring-integrated executor with automatic @XxlJob method discovery
* Implements Spring lifecycle interfaces for proper integration
*/
public class XxlJobSpringExecutor extends XxlJobExecutor
implements ApplicationContextAware, SmartInitializingSingleton, DisposableBean {
/**
* Get Spring application context
* @return ApplicationContext instance
*/
public static ApplicationContext getApplicationContext();
// Inherited from XxlJobExecutor: all configuration and lifecycle methods
// Additional Spring lifecycle management handled automatically
}Usage Examples:
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class XxlJobConfig {
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
// Required configuration
xxlJobSpringExecutor.setAdminAddresses("http://127.0.0.1:8080/xxl-job-admin");
xxlJobSpringExecutor.setAppname("xxl-job-executor-sample");
// Optional configuration
xxlJobSpringExecutor.setAddress(""); // Auto-detect if empty
xxlJobSpringExecutor.setIp(""); // Auto-detect if empty
xxlJobSpringExecutor.setPort(9999);
xxlJobSpringExecutor.setAccessToken("default_token");
xxlJobSpringExecutor.setLogPath("/data/applogs/xxl-job/jobhandler");
xxlJobSpringExecutor.setLogRetentionDays(30);
return xxlJobSpringExecutor;
}
}
// Multiple admin servers for high availability
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
XxlJobSpringExecutor executor = new XxlJobSpringExecutor();
executor.setAdminAddresses("http://admin1:8080/xxl-job-admin,http://admin2:8080/xxl-job-admin");
// Other configuration...
return executor;
}Simple executor for non-Spring environments with manual job handler registration.
/**
* Simple executor for non-Spring environments
* Requires manual job handler registration
*/
public class XxlJobSimpleExecutor extends XxlJobExecutor {
/**
* Get list of job bean objects containing @XxlJob methods
* @return List of objects with job handler methods
*/
public List<Object> getXxlJobBeanList();
/**
* Set list of job bean objects containing @XxlJob methods
* @param xxlJobBeanList - List of objects with @XxlJob annotated methods
*/
public void setXxlJobBeanList(List<Object> xxlJobBeanList);
// Inherited from XxlJobExecutor: all configuration and lifecycle methods
}Usage Examples:
import com.xxl.job.core.executor.impl.XxlJobSimpleExecutor;
import java.util.Arrays;
public class StandaloneJobExecutor {
public static void main(String[] args) throws Exception {
// Create and configure executor
XxlJobSimpleExecutor xxlJobExecutor = new XxlJobSimpleExecutor();
// Configuration
xxlJobExecutor.setAdminAddresses("http://127.0.0.1:8080/xxl-job-admin");
xxlJobExecutor.setAppname("standalone-executor");
xxlJobExecutor.setPort(9999);
xxlJobExecutor.setLogPath("./logs");
// Register job handlers manually
xxlJobExecutor.setXxlJobBeanList(Arrays.asList(
new MyJobHandlerBean(),
new AnotherJobHandlerBean()
));
// Start executor
xxlJobExecutor.start();
// Keep application running
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
xxlJobExecutor.destroy();
}));
Thread.sleep(Long.MAX_VALUE);
}
}
// Job handler bean class
public class MyJobHandlerBean {
@XxlJob("standaloneJob")
public void standaloneJobHandler() throws Exception {
XxlJobHelper.log("Standalone job executing");
XxlJobHelper.handleSuccess();
}
}@Configuration
@Profile("production")
public class ProductionXxlJobConfig {
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
XxlJobSpringExecutor executor = new XxlJobSpringExecutor();
// High availability admin servers
executor.setAdminAddresses(
"http://xxl-job-admin-1:8080/xxl-job-admin," +
"http://xxl-job-admin-2:8080/xxl-job-admin"
);
// Unique application identification
executor.setAppname("production-order-service");
// Security token
executor.setAccessToken("${xxl.job.access.token}");
// Network configuration
executor.setPort(9999);
executor.setTimeout(60); // 60 seconds for long-running jobs
// Logging configuration
executor.setLogPath("/var/log/xxl-job");
executor.setLogRetentionDays(7); // Keep logs for 7 days
return executor;
}
}@Configuration
@Profile("development")
public class DevelopmentXxlJobConfig {
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
XxlJobSpringExecutor executor = new XxlJobSpringExecutor();
// Local admin server
executor.setAdminAddresses("http://localhost:8080/xxl-job-admin");
executor.setAppname("dev-" + System.getProperty("user.name"));
// Development settings
executor.setPort(0); // Random available port
executor.setLogPath("./logs/xxl-job");
executor.setLogRetentionDays(3);
return executor;
}
}@Configuration
public class DynamicXxlJobConfig {
@Value("${xxl.job.admin.addresses}")
private String adminAddresses;
@Value("${xxl.job.executor.appname}")
private String appname;
@Value("${xxl.job.executor.port:9999}")
private int port;
@Value("${xxl.job.access.token:}")
private String accessToken;
@Value("${xxl.job.executor.logpath:./logs}")
private String logPath;
@Value("${xxl.job.executor.logretentiondays:30}")
private int logRetentionDays;
@Bean
public XxlJobSpringExecutor xxlJobExecutor() {
XxlJobSpringExecutor executor = new XxlJobSpringExecutor();
executor.setAdminAddresses(adminAddresses);
executor.setAppname(appname);
executor.setPort(port);
executor.setAccessToken(accessToken);
executor.setLogPath(logPath);
executor.setLogRetentionDays(logRetentionDays);
return executor;
}
}The executor automatically handles:
Install with Tessl CLI
npx tessl i tessl/maven-com-xuxueli--xxl-job-core