有以下processorfactory
- BuiltInDefaultProcessorFactory
- BuiltInSpringProcessorFactory
- BuildInSpringMethodProcessorFactory
- JarContainerProcessorFactory
BuiltInDefaultProcessorFactory
可以动态加载ShellProcessor、PythonProcessor、HttpProcessor、FileCleanupProcessor、SpringDatasourceSqlProcessor、DynamicDatasourceSqlProcessor
其中最核心的代码
LightTaskTracker.create -> PowerJobProcessorLoader.load -> BuiltInDefaultProcessorFactory.build
LightTaskTracker.create
/**
* 静态方法创建 TaskTracker
*
* @param req 服务端调度任务请求
* @return LightTaskTracker
*/
public static LightTaskTracker create(ServerScheduleJobReq req, WorkerRuntime workerRuntime) {
try {
return new LightTaskTracker(req, workerRuntime);
} catch (Exception e) {
reportCreateErrorToServer(req, workerRuntime, e);
}
return null;
}
PowerJobProcessorLoader.load
@Override
public ProcessorBean load(ProcessorDefinition definition) {
return def2Bean.computeIfAbsent(definition, ignore -> {
final String processorType = definition.getProcessorType();
log.info("[ProcessorFactory] start to load Processor: {}", definition);
for (ProcessorFactory pf : processorFactoryList) {
final String pfName = pf.getClass().getSimpleName();
if (!Optional.ofNullable(pf.supportTypes()).orElse(Collections.emptySet()).contains(processorType)) {
log.info("[ProcessorFactory] [{}] can't load type={}, skip!", pfName, processorType);
continue;
}
log.info("[ProcessorFactory] [{}] try to load processor: {}", pfName, definition);
try {
ProcessorBean processorBean = pf.build(definition);
if (processorBean != null) {
log.info("[ProcessorFactory] [{}] load processor successfully: {}", pfName, definition);
return processorBean;
}
} catch (Throwable t) {
log.error("[ProcessorFactory] [{}] load processor failed: {}", pfName, definition, t);
}
}
throw new PowerJobException("fetch Processor failed, please check your processorType and processorInfo config");
});
}
}
这里的processorFactoryList,通过PowerJobSpringWorker.setApplicationContext和PowerJobWorker.init初始化的
BuiltInDefaultProcessorFactory.build
@Override
public ProcessorBean build(ProcessorDefinition processorDefinition) {
String className = processorDefinition.getProcessorInfo();
try {
// 这里的className,就是上面的默认的processor
Class<?> clz = Class.forName(className);
BasicProcessor basicProcessor = (BasicProcessor) clz.getDeclaredConstructor().newInstance();
return new ProcessorBean()
.setProcessor(basicProcessor)
.setClassLoader(basicProcessor.getClass().getClassLoader());
}catch (Exception e) {
log.warn("[ProcessorFactory] load local Processor(className = {}) failed.", className, e);
}
return null;
}