Spring Boot同一应用启动两个ElasticJob其中一个未启动问题
原因:注入的
SimpleJob
与JobScheduler
的Bean名称必须不能相同
错误代码
@Configuration
public class AJobConfig {
@Resource
private ZookeeperRegistryCenter regCenter;
@Bean
public SimpleJob simpleJob() {
return new AJob();
}
@Bean(initMethod = "init")
public JobScheduler jobScheduler(final SimpleJob simpleJob, @Value("${aJob.cron}") final String cron,
@Value("${aJob.shardingTotalCount}") final int shardingTotalCount,
@Value("${aJob.shardingItemParameters}") final String shardingItemParameters,
@Value("${aJob.description}") final String description) {
return new SpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(),
cron, shardingTotalCount, shardingItemParameters, description));
}
private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters, final String description) {
return LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(JobCoreConfiguration.newBuilder(
jobClass.getSimpleName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).description(description).build(), jobClass.getCanonicalName())).overwrite(true).build();
}
}
@Configuration
public class BJobConfig {
@Resource
private ZookeeperRegistryCenter regCenter;
@Bean
public SimpleJob simpleJob() {
return new BJob();
}
@Bean(initMethod = "init")
public JobScheduler jobScheduler(final SimpleJob simpleJob, @Value("${bJob.cron}") final String cron,
@Value("${bJob.shardingTotalCount}") final int shardingTotalCount,
@Value("${bJob.shardingItemParameters}") final String shardingItemParameters,
@Value("${bJob.description}") final String description) {
return new SpringJobScheduler(simpleJob, regCenter, getLiteJobConfiguration(simpleJob.getClass(),
cron, shardingTotalCount, shardingItemParameters, description));
}
private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters, final String description) {
return LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(JobCoreConfiguration.newBuilder(
jobClass.getSimpleName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).description(description).build(), jobClass.getCanonicalName())).overwrite(true).build();
}
}
正确代码
@Configuration
public class AJobConfig {
@Resource
private ZookeeperRegistryCenter regCenter;
@Bean
public SimpleJob aJob() { //SimpleJob Bean名称必须不一致
return new AJob();
}
@Bean(initMethod = "init") //JobScheduler Bean名称必须不一致
public JobScheduler aJobScheduler(final SimpleJob aJob, @Value("${aJob.cron}") final String cron,
@Value("${aJob.shardingTotalCount}") final int shardingTotalCount,
@Value("${aJob.shardingItemParameters}") final String shardingItemParameters,
@Value("${aJob.description}") final String description) {
return new SpringJobScheduler(aJob, regCenter, getLiteJobConfiguration(aJob.getClass(),
cron, shardingTotalCount, shardingItemParameters, description));
}
private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters, final String description) {
return LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(JobCoreConfiguration.newBuilder(
jobClass.getSimpleName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).description(description).build(), jobClass.getCanonicalName())).overwrite(true).build();
}
}
@Configuration
public class BJobConfig {
@Resource
private ZookeeperRegistryCenter regCenter;
@Bean
public SimpleJob bJob() { //SimpleJob Bean名称必须不一致
return new BJob();
}
@Bean(initMethod = "init") //JobScheduler Bean名称必须不一致
public JobScheduler bJobScheduler(final SimpleJob bJob, @Value("${bJob.cron}") final String cron,
@Value("${bJob.shardingTotalCount}") final int shardingTotalCount,
@Value("${bJob.shardingItemParameters}") final String shardingItemParameters,
@Value("${bJob.description}") final String description) {
return new SpringJobScheduler(bJob, regCenter, getLiteJobConfiguration(bJob.getClass(),
cron, shardingTotalCount, shardingItemParameters, description));
}
private LiteJobConfiguration getLiteJobConfiguration(final Class<? extends SimpleJob> jobClass, final String cron, final int shardingTotalCount, final String shardingItemParameters, final String description) {
return LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(JobCoreConfiguration.newBuilder(
jobClass.getSimpleName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).description(description).build(), jobClass.getCanonicalName())).overwrite(true).build();
}
}
注册中心
@Configuration
@ConditionalOnExpression("'${regCenter.serverList}'.length() > 0")
public class RegistryCenterConfig {
@Bean(initMethod = "init")
public ZookeeperRegistryCenter regCenter(@Value("${regCenter.serverList}") final String serverList, @Value("${regCenter.namespace}") final String namespace) {
return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
}
}
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/27/spring-boot-launches-two-elasticjobs-in-the-same-application-one-of-which-is-not-started/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
THE END
0
二维码
打赏
海报
Spring Boot同一应用启动两个ElasticJob其中一个未启动问题
原因:注入的SimpleJob与JobScheduler的Bean名称必须不能相同
错误代码
@Configuration
public class AJobConfig {
@Resource
private ZookeeperR……
文章目录
关闭
共有 0 条评论