Spring Boot同一应用启动两个ElasticJob其中一个未启动问题

原因:注入的SimpleJobJobScheduler的Bean名称必须不能相同

错误代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@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();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@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();
}
}

正确代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@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();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@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();
}
}

注册中心

1
2
3
4
5
6
7
8
9
10
11
@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));
}

}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2021 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :