在这里插入图片描述
springboot整合elaticjob报错 Invocation of init method failed; nested exception is java.lang.NullPointerException
application.properties文件配置
elaticjob.zookeeper.server-lists=10.10.80.51:2181
elaticjob.zookeeper.namespace=my-project
server.port=8766
spring.application.name=scheduler-service
zookeeper注册中心
spring.elasticjob.serverList = 10.10.80.51:2181
spring.elasticjob.namespace = elastic-job-lite-springboot
stockJob.cron = 0/5 * * * * ?
stockJob.shardingTotalCount = 2
stockJob.shardingItemParameters = 0=Chengdu0,1=Chengdu1
stockSimpleJob.java文件
package com.example.elasticjob.job;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import org.springframework.stereotype.Component;
@Component
public class StockSimpleJob implements SimpleJob {
@Override
public void execute(ShardingContext shardingContext) {
System.out.println(String.format("------Thread ID: %s, 任务总片数: %s, " +
"当前分片项: %s.当前参数: %s,"+
"当前任务名称: %s.当前任务参数: %s"
,
Thread.currentThread().getId(),
shardingContext.getShardingTotalCount(),
shardingContext.getShardingItem(),
shardingContext.getShardingParameter(),
shardingContext.getJobName(),
shardingContext.getJobParameter()
));
}
}
config配置类JobRegistryCenterConfig
package com.example.elasticjob.config;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnExpression("’${spring.elasticjob.serverList}’.length() > 0") //判断是否配置了zookeeper 地址
public class JobRegistryCenterConfig {
@Bean(initMethod = "init")
public ZookeeperRegistryCenter regCenter(@Value("${spring.elasticjob.serverList}") final String serverList, @Value("${spring.elasticjob.namespace}") final String namespace) {
return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
}
}
SpringJobScheduler.java
package com.example.elasticjob.config;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.example.elasticjob.job.StockSimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
class StockJobConfig {
@Autowired
private JobRegistryCenterConfig jobRegistryCenterConfig;
@Autowired
private ZookeeperRegistryCenter regCenter;
public StockJobConfig() {
}
@Bean(initMethod = “init”)
public JobScheduler simpleJobScheduler(final StockSimpleJob simpleJob, @Value("{stockJob.cron}") final String cron, @Value("stockJob.cron")finalStringcron,@Value("{stockJob.shardingTotalCount}") final int shardingTotalCount,
@Value("${stockJob.shardingItemParameters}") final String shardingItemParameters) {
return new SpringJobScheduler(simpleJob, regCenter, simpleJobConfigBuilder(simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters));
}
/**
*@Description 任务配置类
*/
private LiteJobConfiguration simpleJobConfigBuilder(final Class<? extends SimpleJob> jobClass,
final String cron,
final int shardingTotalCount,
final String shardingItemParameters){
return LiteJobConfiguration
.newBuilder(
new SimpleJobConfiguration(
JobCoreConfiguration.newBuilder(
"my-jobName",cron,shardingTotalCount)
.shardingItemParameters(shardingItemParameters).jobParameter("job-参数")
.build()
,jobClass.getCanonicalName()
)
)
.overwrite(true)
.build();
}
/**
pom
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
com.example.elasticjob
elasticjob
0.0.1-SNAPSHOT
elasticjob
Demo project for Spring Boot
<java.version>1.8</java.version>
org.springframework.boot
spring-boot-starter-web
com.github.kuhn-he
elastic-job-lite-spring-boot-starter
2.1.5
com.dangdang
elastic-job-lite-core
2.1.5
com.dangdang
elastic-job-lite-spring
2.1.5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
麻烦大神帮我看下 到底是什么错误??!!!