feat: optimize PowerJobSpringWorker

This commit is contained in:
tjq 2023-02-01 22:13:52 +08:00
parent 95a1f43994
commit 39893b1e92
2 changed files with 17 additions and 8 deletions

View File

@ -8,7 +8,6 @@ import org.springframework.context.annotation.Configuration;
import tech.powerjob.common.utils.CommonUtils;
import tech.powerjob.common.utils.NetUtils;
import tech.powerjob.worker.PowerJobSpringWorker;
import tech.powerjob.worker.PowerJobWorker;
import tech.powerjob.worker.common.PowerJobWorkerConfig;
import java.util.Arrays;
@ -27,7 +26,7 @@ public class PowerJobAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public PowerJobWorker initPowerJob(PowerJobProperties properties) {
public PowerJobSpringWorker initPowerJob(PowerJobProperties properties) {
PowerJobProperties.Worker worker = properties.getWorker();

View File

@ -21,16 +21,22 @@ import java.util.Optional;
* @author tjq
* @since 2023/1/20
*/
public class PowerJobSpringWorker extends PowerJobWorker implements ApplicationContextAware, InitializingBean, DisposableBean {
public class PowerJobSpringWorker implements ApplicationContextAware, InitializingBean, DisposableBean {
/**
* 组合优于继承持有 PowerJobWorker内部重新设置 ProcessorFactory 更优雅
*/
private PowerJobWorker powerJobWorker;
private final PowerJobWorkerConfig config;
public PowerJobSpringWorker(PowerJobWorkerConfig config) {
super(config);
this.config = config;
}
@Override
public void afterPropertiesSet() throws Exception {
init();
powerJobWorker = new PowerJobWorker(config);
powerJobWorker.init();
}
@Override
@ -38,12 +44,16 @@ public class PowerJobSpringWorker extends PowerJobWorker implements ApplicationC
BuiltInSpringProcessorFactory springProcessorFactory = new BuiltInSpringProcessorFactory(applicationContext);
// append BuiltInSpringProcessorFactory
PowerJobWorkerConfig workerConfig = workerRuntime.getWorkerConfig();
List<ProcessorFactory> processorFactories = Lists.newArrayList(
Optional.ofNullable(workerConfig.getProcessorFactoryList())
Optional.ofNullable(config.getProcessorFactoryList())
.orElse(Collections.emptyList()));
processorFactories.add(springProcessorFactory);
workerConfig.setProcessorFactoryList(processorFactories);
config.setProcessorFactoryList(processorFactories);
}
@Override
public void destroy() throws Exception {
powerJobWorker.destroy();
}
}