refactor(core): 重构注册类型枚举并优化代码结构

- 将 RegistType 重命名为 RegistTypeEnum 并更新所有引用
- 为 ExecutorBlockStrategyEnum 添加详细的 JavaDoc 注释
- 为 XxlJobContext 类的常量添加完整的注释文档
- 在 XxlJobHelper 中完善方法注释
- 优化 XxlJobExecutor 启动方法的日志描述
- 在 SpringGlueFactory 中改进字符串判空逻辑
- 为多个核心类的字段添加 final 修饰符增强安全性
- 完善 OpenAPI 接口的方法参数注释
- 优化线程停止逻辑,添加空指针检查避免异常
- 修复重复触发作业的日志消息拼写错误
- 改进队列状态检查方法的性能表现
3.4.1-release
xuxueli 3 days ago
parent 9d4ba92537
commit f4487acdfc

@ -8,7 +8,7 @@ import com.xxl.job.admin.business.mapper.XxlJobGroupMapper;
import com.xxl.job.admin.business.mapper.XxlJobInfoMapper;
import com.xxl.job.admin.business.mapper.XxlJobRegistryMapper;
import com.xxl.job.core.constant.Const;
import com.xxl.job.core.constant.RegistType;
import com.xxl.job.core.constant.RegistTypeEnum;
import com.xxl.sso.core.annotation.XxlSso;
import com.xxl.tool.core.CollectionTool;
import com.xxl.tool.core.StringTool;
@ -164,7 +164,7 @@ public class JobGroupController {
List<XxlJobRegistry> list = xxlJobRegistryMapper.findAll(Const.DEAD_TIMEOUT, new Date());
if (CollectionTool.isNotEmpty(list)) {
for (XxlJobRegistry item: list) {
if (!RegistType.EXECUTOR.name().equals(item.getRegistryGroup())) {
if (!RegistTypeEnum.EXECUTOR.name().equals(item.getRegistryGroup())) {
continue;
}
@ -211,7 +211,7 @@ public class JobGroupController {
// remove group
int ret = xxlJobGroupMapper.remove(id);
// remove registry-data
xxlJobRegistryMapper.removeByRegistryGroupAndKey(RegistType.EXECUTOR.name(), xxlJobGroup.getAppname());
xxlJobRegistryMapper.removeByRegistryGroupAndKey(RegistTypeEnum.EXECUTOR.name(), xxlJobGroup.getAppname());
return (ret>0)?Response.ofSuccess():Response.ofFail();
}

@ -4,7 +4,7 @@ import com.xxl.job.admin.business.model.XxlJobGroup;
import com.xxl.job.admin.business.model.XxlJobRegistry;
import com.xxl.job.admin.business.scheduler.config.XxlJobAdminBootstrap;
import com.xxl.job.core.constant.Const;
import com.xxl.job.core.constant.RegistType;
import com.xxl.job.core.constant.RegistTypeEnum;
import com.xxl.job.core.openapi.model.RegistryRequest;
import com.xxl.tool.concurrent.CyclicThread;
import com.xxl.tool.core.StringTool;
@ -79,7 +79,7 @@ public class JobRegistryHelper {
List<XxlJobRegistry> list = XxlJobAdminBootstrap.getInstance().getXxlJobRegistryMapper().findAll(Const.DEAD_TIMEOUT, new Date());
if (list != null) {
for (XxlJobRegistry item: list) {
if (RegistType.EXECUTOR.name().equals(item.getRegistryGroup())) {
if (RegistTypeEnum.EXECUTOR.name().equals(item.getRegistryGroup())) {
String appname = item.getRegistryKey();
List<String> registryList = appAddressMap.get(appname);
if (registryList == null) {

@ -1,7 +1,7 @@
package com.xxl.job.admin.business.mapper;
import com.xxl.job.admin.business.model.XxlJobRegistry;
import com.xxl.job.core.constant.RegistType;
import com.xxl.job.core.constant.RegistTypeEnum;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@ -19,7 +19,7 @@ public class XxlJobRegistryMapperTest {
@Test
public void test(){
int ret = xxlJobRegistryMapper.registrySaveOrUpdate(RegistType.EXECUTOR.name(), "xxl-job-executor-z1", "v1", new Date());
int ret = xxlJobRegistryMapper.registrySaveOrUpdate(RegistTypeEnum.EXECUTOR.name(), "xxl-job-executor-z1", "v1", new Date());
/*int ret = xxlJobRegistryDao.registryUpdate("g1", "k1", "v1", new Date());
if (ret < 1) {
ret = xxlJobRegistryDao.registrySave("g1", "k1", "v1", new Date());

@ -1,6 +1,6 @@
package com.xxl.job.openapi;
import com.xxl.job.core.constant.RegistType;
import com.xxl.job.core.constant.RegistTypeEnum;
import com.xxl.job.core.openapi.AdminBiz;
import com.xxl.job.core.openapi.model.CallbackRequest;
import com.xxl.job.core.openapi.model.RegistryRequest;
@ -61,7 +61,7 @@ public class AdminBizTest {
public void registry() throws Exception {
AdminBiz adminBiz = buildClient();
RegistryRequest registryParam = new RegistryRequest(RegistType.EXECUTOR.name(), "xxl-job-executor-example", "127.0.0.1:9999");
RegistryRequest registryParam = new RegistryRequest(RegistTypeEnum.EXECUTOR.name(), "xxl-job-executor-example", "127.0.0.1:9999");
Response<String> returnT = adminBiz.registry(registryParam);
assertTrue(returnT.isSuccess());
@ -76,7 +76,7 @@ public class AdminBizTest {
public void registryRemove() throws Exception {
AdminBiz adminBiz = buildClient();
RegistryRequest registryParam = new RegistryRequest(RegistType.EXECUTOR.name(), "xxl-job-executor-example", "127.0.0.1:9999");
RegistryRequest registryParam = new RegistryRequest(RegistTypeEnum.EXECUTOR.name(), "xxl-job-executor-example", "127.0.0.1:9999");
Response<String> returnT = adminBiz.registryRemove(registryParam);
assertTrue(returnT.isSuccess());

@ -5,13 +5,30 @@ package com.xxl.job.core.constant;
*/
public enum ExecutorBlockStrategyEnum {
/**
* serial execution
*/
SERIAL_EXECUTION("Serial execution"),
/**
* concurrent execution
*/
/*CONCURRENT_EXECUTION("并行"),*/
/**
* discard later
*/
DISCARD_LATER("Discard Later"),
/**
* cover early
*/
COVER_EARLY("Cover Early");
private String title;
private ExecutorBlockStrategyEnum (String title) {
ExecutorBlockStrategyEnum (String title) {
this.title = title;
}
@ -22,6 +39,13 @@ public enum ExecutorBlockStrategyEnum {
return title;
}
/**
* match by name
*
* @param name enum name
* @param defaultItem default item
* @return match item
*/
public static ExecutorBlockStrategyEnum match(String name, ExecutorBlockStrategyEnum defaultItem) {
if (name != null) {
for (ExecutorBlockStrategyEnum item:ExecutorBlockStrategyEnum.values()) {

@ -3,7 +3,7 @@ package com.xxl.job.core.constant;
/**
* Created by xuxueli on 17/5/9.
*/
public enum RegistType{
public enum RegistTypeEnum {
/**
* executor registry

@ -8,10 +8,22 @@ package com.xxl.job.core.context;
*/
public class XxlJobContext {
/**
* handle success
*/
public static final int HANDLE_CODE_SUCCESS = 200;
/**
* handle fail
*/
public static final int HANDLE_CODE_FAIL = 500;
/**
* handle timeout
*/
public static final int HANDLE_CODE_TIMEOUT = 502;
// ---------------------- base info ----------------------
/**
@ -24,6 +36,7 @@ public class XxlJobContext {
*/
private final String jobParam;
// ---------------------- for log ----------------------
/**
@ -41,6 +54,7 @@ public class XxlJobContext {
*/
private final String logFileName;
// ---------------------- for shard ----------------------
/**
@ -53,6 +67,7 @@ public class XxlJobContext {
*/
private final int shardTotal;
// ---------------------- for handle ----------------------
/**
@ -133,6 +148,7 @@ public class XxlJobContext {
return handleMsg;
}
// ---------------------- tool ----------------------
/**

@ -256,6 +256,8 @@ public class XxlJobHelper {
}
/**
* handle result
*
* @param handleCode
*
* 200 : success

@ -117,7 +117,7 @@ public class XxlJobExecutor {
*/
public void start() throws Exception {
// init instance
// bind instance
xxlJobExecutor = this;
// valid enabled

@ -2,6 +2,7 @@ package com.xxl.job.core.glue;
import com.xxl.job.core.glue.impl.SpringGlueFactory;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.tool.core.StringTool;
import groovy.lang.GroovyClassLoader;
import java.math.BigInteger;
@ -39,31 +40,28 @@ public class GlueFactory {
/**
* groovy class loader
*/
private GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
private ConcurrentMap<String, Class<?>> CLASS_CACHE = new ConcurrentHashMap<>();
private final GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
private final ConcurrentMap<String, Class<?>> CLASS_CACHE = new ConcurrentHashMap<>();
/**
* load new instance, prototype
*
* @param codeSource
* @return
* @throws Exception
* @param codeSource code source
* @return IJobHandler
*/
public IJobHandler loadNewInstance(String codeSource) throws Exception{
if (codeSource!=null && codeSource.trim().length()>0) {
if (StringTool.isNotBlank(codeSource)) {
Class<?> clazz = getCodeSourceClass(codeSource);
if (clazz != null) {
Object instance = clazz.newInstance();
if (instance!=null) {
if (instance instanceof IJobHandler) {
this.injectService(instance);
return (IJobHandler) instance;
} else {
throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadNewInstance error, "
+ "cannot convert from instance["+ instance.getClass() +"] to IJobHandler");
}
}
}
if (instance instanceof IJobHandler) {
this.injectService(instance);
return (IJobHandler) instance;
} else {
throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadNewInstance error, "
+ "cannot convert from instance[" + instance.getClass() + "] to IJobHandler");
}
}
}
throw new IllegalArgumentException(">>>>>>>>>>> xxl-glue, loadNewInstance error, instance is null");
}
@ -87,7 +85,7 @@ public class GlueFactory {
/**
* inject service of bean field
*
* @param instance
* @param instance instance
*/
public void injectService(Object instance) {
// do something

@ -2,6 +2,7 @@ package com.xxl.job.core.glue.impl;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import com.xxl.job.core.glue.GlueFactory;
import com.xxl.tool.core.StringTool;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -16,12 +17,13 @@ import java.lang.reflect.Modifier;
* @author xuxueli 2018-11-01
*/
public class SpringGlueFactory extends GlueFactory {
private static Logger logger = LoggerFactory.getLogger(SpringGlueFactory.class);
private static final Logger logger = LoggerFactory.getLogger(SpringGlueFactory.class);
/**
* inject action of spring
* @param instance
*
* @param instance instance
*/
@Override
public void injectService(Object instance){
@ -40,24 +42,23 @@ public class SpringGlueFactory extends GlueFactory {
}
Object fieldBean = null;
// with bean-id, bean could be found by both @Resource and @Autowired, or bean could only be found by @Autowired
// with bean-id, bean could be found by both @Resource and @Autowired, or bean could only be found by @Autowired
if (AnnotationUtils.getAnnotation(field, Resource.class) != null) {
try {
Resource resource = AnnotationUtils.getAnnotation(field, Resource.class);
if (resource.name()!=null && resource.name().length()>0){
if (resource!=null && StringTool.isNotBlank(resource.name())){
fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(resource.name());
} else {
fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(field.getName());
}
} catch (Exception e) {
}
} catch (Exception e) {}
if (fieldBean==null ) {
fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(field.getType());
}
} else if (AnnotationUtils.getAnnotation(field, Autowired.class) != null) {
Qualifier qualifier = AnnotationUtils.getAnnotation(field, Qualifier.class);
if (qualifier!=null && qualifier.value()!=null && qualifier.value().length()>0) {
if (qualifier!=null && StringTool.isNotBlank(qualifier.value())) {
fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(qualifier.value());
} else {
fieldBean = XxlJobSpringExecutor.getApplicationContext().getBean(field.getType());

@ -10,8 +10,6 @@ public abstract class IJobHandler {
/**
* execute handler, invoked when executor receives a scheduling request
*
* @throws Exception
*/
public abstract void execute() throws Exception;

@ -10,12 +10,21 @@ import com.xxl.job.core.handler.IJobHandler;
*/
public class GlueJobHandler extends IJobHandler {
/**
* glue update time
*/
private long glueUpdatetime;
/**
* job handler
*/
private IJobHandler jobHandler;
public GlueJobHandler(IJobHandler jobHandler, long glueUpdatetime) {
this.jobHandler = jobHandler;
this.glueUpdatetime = glueUpdatetime;
}
public long getGlueUpdatetime() {
return glueUpdatetime;
}
@ -23,7 +32,7 @@ public class GlueJobHandler extends IJobHandler {
@Override
public void execute() throws Exception {
XxlJobHelper.log("----------- glue.version:"+ glueUpdatetime +" -----------");
jobHandler.execute();
this.jobHandler.execute();
}
@Override

@ -11,8 +11,8 @@ public class MethodJobHandler extends IJobHandler {
private final Object target;
private final Method method;
private Method initMethod;
private Method destroyMethod;
private final Method initMethod;
private final Method destroyMethod;
public MethodJobHandler(Object target, Method method, Method initMethod, Method destroyMethod) {
this.target = target;
@ -50,4 +50,5 @@ public class MethodJobHandler extends IJobHandler {
public String toString() {
return super.toString()+"["+ target.getClass() + "#" + method.getName() +"]";
}
}

@ -14,10 +14,10 @@ import java.io.File;
*/
public class ScriptJobHandler extends IJobHandler {
private int jobId;
private long glueUpdatetime;
private String gluesource;
private GlueTypeEnum glueType;
private final int jobId;
private final long glueUpdatetime;
private final String gluesource;
private final GlueTypeEnum glueType;
public ScriptJobHandler(int jobId, long glueUpdatetime, String gluesource, GlueTypeEnum glueType){
this.jobId = jobId;

@ -17,8 +17,8 @@ public interface AdminBiz {
/**
* callback
*
* @param callbackRequestList
* @return
* @param callbackRequestList callback request list
* @return response
*/
public Response<String> callback(List<CallbackRequest> callbackRequestList);
@ -28,16 +28,16 @@ public interface AdminBiz {
/**
* registry
*
* @param registryRequest
* @return
* @param registryRequest registry request
* @return response
*/
public Response<String> registry(RegistryRequest registryRequest);
/**
* registry remove
*
* @param registryRequest
* @return
* @param registryRequest registry request
* @return response
*/
public Response<String> registryRemove(RegistryRequest registryRequest);

@ -10,6 +10,7 @@ public interface ExecutorBiz {
/**
* beat
*
* @return response
*/
public Response<String> beat();
@ -24,6 +25,7 @@ public interface ExecutorBiz {
/**
* run
*
* @param triggerRequest triggerRequest
* @return response
*/
@ -31,6 +33,7 @@ public interface ExecutorBiz {
/**
* kill
*
* @param killRequest killRequest
* @return response
*/
@ -38,6 +41,7 @@ public interface ExecutorBiz {
/**
* log
*
* @param logRequest logRequest
* @return response
*/

@ -134,8 +134,8 @@ public class EmbedServer {
// ---------------------- registry ----------------------
/**
* netty_http
* <p>
* netty_http server handler
*
* Copy from : https://github.com/xuxueli/xxl-rpc
*
* @author xuxueli 2015-11-24 22:25:15

@ -1,6 +1,6 @@
package com.xxl.job.core.thread;
import com.xxl.job.core.constant.RegistType;
import com.xxl.job.core.constant.RegistTypeEnum;
import com.xxl.job.core.openapi.AdminBiz;
import com.xxl.job.core.openapi.model.RegistryRequest;
import com.xxl.job.core.constant.Const;
@ -48,7 +48,7 @@ public class ExecutorRegistryThreadHelper {
registryThread = new CyclicThread("ExecutorRegistryThread#registryThread", true, new Runnable() {
@Override
public void run() {
RegistryRequest registryParam = new RegistryRequest(RegistType.EXECUTOR.name(), xxlJobExecutor.getAppname(), xxlJobExecutor.getAddress());
RegistryRequest registryParam = new RegistryRequest(RegistTypeEnum.EXECUTOR.name(), xxlJobExecutor.getAppname(), xxlJobExecutor.getAddress());
for (AdminBiz adminBiz: xxlJobExecutor.getAdminBizList()) {
try {
Response<String> registryResult = adminBiz.registry(registryParam);
@ -78,7 +78,9 @@ public class ExecutorRegistryThreadHelper {
/**
* 1stop registryThread
*/
registryThread.stop();
if (registryThread != null) {
registryThread.stop();
}
/**
* 2registry remove
@ -87,7 +89,7 @@ public class ExecutorRegistryThreadHelper {
}
private void registryRemove(final XxlJobExecutor xxlJobExecutor){
RegistryRequest registryParam = new RegistryRequest(RegistType.EXECUTOR.name(), xxlJobExecutor.getAppname(), xxlJobExecutor.getAddress());
RegistryRequest registryParam = new RegistryRequest(RegistTypeEnum.EXECUTOR.name(), xxlJobExecutor.getAppname(), xxlJobExecutor.getAddress());
for (AdminBiz adminBiz: xxlJobExecutor.getAdminBizList()) {
try {
Response<String> registryResult = adminBiz.registryRemove(registryParam);

@ -102,7 +102,10 @@ public class JobLogFileCleanThreadHelper {
public void stop() {
// stop logFileCleanThread
logFileCleanThread.stop();
if (logFileCleanThread != null) {
logFileCleanThread.stop();
}
}
}

@ -25,24 +25,23 @@ import java.util.concurrent.*;
public class JobThread extends Thread{
private static final Logger logger = LoggerFactory.getLogger(JobThread.class);
private int jobId;
private IJobHandler handler;
private LinkedBlockingQueue<TriggerRequest> triggerQueue;
private Set<Long> triggerLogIdSet; // avoid repeat trigger for the same TRIGGER_LOG_ID
private final int jobId;
private final IJobHandler handler;
private final LinkedBlockingQueue<TriggerRequest> triggerQueue;
private final Set<Long> triggerLogIdSet; // avoid repeat trigger for the same TRIGGER_LOG_ID
private volatile boolean toStop = false;
private volatile boolean toStop = false; // thread stop flag
private String stopReason;
private boolean running = false; // if running job
private int idleTimes = 0; // idle times
private boolean running = false; // if running job
private int idleTimes = 0; // idle times
public JobThread(int jobId, IJobHandler handler) {
this.jobId = jobId;
this.handler = handler;
this.triggerQueue = new LinkedBlockingQueue<>();
//this.triggerLogIdSet = Collections.synchronizedSet(new HashSet<Long>());
this.triggerLogIdSet = ConcurrentHashMap.newKeySet();
this.triggerLogIdSet = ConcurrentHashMap.newKeySet(); // Collections.synchronizedSet(new HashSet<Long>());
// assign job thread name
this.setName("xxl-job, JobThread-"+jobId+"-"+System.currentTimeMillis());
@ -57,8 +56,8 @@ public class JobThread extends Thread{
public Response<String> pushTriggerQueue(TriggerRequest triggerParam) {
// avoid repeat
if (!triggerLogIdSet.add(triggerParam.getLogId())) {
logger.info(">>>>>>>>>>> repeate trigger job, logId:{}", triggerParam.getLogId());
return Response.of(XxlJobContext.HANDLE_CODE_FAIL, "repeate trigger job, logId:" + triggerParam.getLogId());
logger.info(">>>>>>>>>>> repeat trigger job, logId:{}", triggerParam.getLogId());
return Response.of(XxlJobContext.HANDLE_CODE_FAIL, "repeat trigger job, logId:" + triggerParam.getLogId());
}
// push trigger queue
@ -83,20 +82,20 @@ public class JobThread extends Thread{
* is running job
*/
public boolean isRunningOrHasQueue() {
return running || triggerQueue.size()>0;
return running || !triggerQueue.isEmpty();
}
@Override
public void run() {
// init
// invoke init-method, only once
try {
handler.init();
} catch (Throwable e) {
logger.error(e.getMessage(), e);
}
// execute
// invoke job, listen schedule-center
while(!toStop){
running = false;
idleTimes++;
@ -236,7 +235,7 @@ public class JobThread extends Thread{
}
}
// destroy
// invoke destroy-method, only once
try {
handler.destroy();
} catch (Throwable e) {

@ -71,7 +71,6 @@ public class TriggerCallbackThreadHelper {
1,
50);
/**
* 2retry callback-file thread
*/
@ -130,7 +129,9 @@ public class TriggerCallbackThreadHelper {
}
// 2、stop retryCallbackThread
retryCallbackThread.stop();
if (callbackMessageQueue != null) {
retryCallbackThread.stop();
}
}

Loading…
Cancel
Save