获取java.lang.IllegalStateException:无法在Spring Batch中执行CommandLineRunner

时间:2018-11-28 14:21:49

标签: java spring-boot

尝试运行spring batch时出现以下错误。

java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:779) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at com.amhi.care.claims.query.batch.QueryBatchApplication.main(QueryBatchApplication.java:15) [classes/:na]


Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:657) ~[na:1.8.0_192]
at java.util.ArrayList.get(ArrayList.java:433) ~[na:1.8.0_192]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.getNextJobParameters(JobLauncherCommandLineRunner.java:143) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:212) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:231) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:123) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:117) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
... 4 common frames omitted

代码:

@Configuration
@EnableBatchProcessing
@EnableScheduling
@EnableTransactionManagement
@ComponentScan(QueryBatchConstants.COMPONENT_SCAN_PACKAGE)
@Import(DataSourcecConfiguration.class)
public class QueryBatchConfiguration {

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;


@Bean
public Job reminderJob() {
    return jobBuilderFactory.get("reminderJob").flow(step()).next(closureStep()).end().build();
}

@Bean
public Step step() {
    return stepBuilderFactory.get("step").tasklet(tasklet()).build();
}
@Bean
public Step closureStep() {     
    return stepBuilderFactory.get("closureStep").tasklet(closureTasklet()).build();
}

@Bean
public Tasklet tasklet(){
    return new QueryBatchTasklet();
}

@Bean
public Tasklet closureTasklet(){
    return new ClosureBatchTasklet();
}


@Bean 
@JobScope
public JobParameters jobParamater(){
    return new JobParametersBuilder()
            .addDate("date", new Date())
            .toJobParameters();
}

/**
 * This method is used to configure the Dozer Mapper
 * 
 * @return Mapper
 * @throws IOException
 */
@Bean(name = "mapper")
public Mapper configDozerMapper() throws IOException {

DozerBeanMapper mapper = new DozerBeanMapper();
return mapper;
}

/**
 * This method is used to create RIDC client manager
 * 
 * @return IdcClientManager
 */
@Bean(name = "idcClientmanager")
public IdcClientManager idcClientmanager() {
return new IdcClientManager();
}

}

@Configuration
@EnableTransactionManagement
@ComponentScan(QueryBatchConstants.COMPONENT_SCAN_PACKAGE)
@PropertySource(QueryBatchConstants.CLASSPATH_APPLICATION_PROPERTIES)
public class DataSourcecConfiguration {

@Resource
private Environment env;

@Autowired
public DataSource dataSource;

  /**
 * This method is used to configure a data source
 * 
 * @return DataSource
 * @throws SQLException
 */
@Bean
public DataSource dataSource() throws SQLException {
DriverManagerDataSource dataSource = new DriverManagerDataSource();

dataSource.setDriverClassName(env.getRequiredProperty(QueryBatchConstants.DATABASE_DRIVER));
dataSource.setUrl(env.getRequiredProperty(QueryBatchConstants.DATABASE_URL));
dataSource.setUsername(env.getRequiredProperty(QueryBatchConstants.DATABASE_USERNAME));
dataSource.setPassword(env.getRequiredProperty(QueryBatchConstants.DATABASE_PSWRD));

return dataSource;
}

/**
 * This method is used to configure a entity manager factory
 * 
 * @return LocalContainerEntityManagerFactoryBean
 * @throws SQLException
 */
@Autowired
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws SQLException {
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setDataSource(dataSource());
entityManager.setPersistenceProviderClass(HibernatePersistence.class);
entityManager.setPackagesToScan(env.getRequiredProperty(QueryBatchConstants.PACKAGES_TO_SCAN));
entityManager.setJpaProperties(jpaProperties());

return entityManager;
}

/**
 * This method is used to configure the JPA properties
 * 
 * @return JPA Properties
 */
private Properties jpaProperties() {
Properties properties = new Properties();
properties.put(QueryBatchConstants.HIBERNATE_DIALECT, env.getRequiredProperty(QueryBatchConstants.HIBERNATE_DIALECT));
properties.put(QueryBatchConstants.HIBERNATE_SHOW_SQL, env.getRequiredProperty(QueryBatchConstants.HIBERNATE_SHOW_SQL));
properties.put(QueryBatchConstants.HIBERNATE_JDBC_META_DATA, env.getRequiredProperty(QueryBatchConstants.HIBERNATE_FALSE));
return properties;
}

/**
 * This method is used to configure the transaction manager
 * 
 * @param emf
 * @return JpaTransactionManager
 */
@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);

return transactionManager;
}

@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    jdbcTemplate.setResultsMapCaseInsensitive(true);
    return jdbcTemplate;
}

}

@Component
public class QueryBatchTasklet implements Tasklet{

@Autowired
private QueryBatchService autoClosureService;


@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {

    autoClosureService.updateStatusAndTriggerComm();
    return null;
}

}

@Component
public class ClosureBatchTasklet implements Tasklet{

@Autowired
private ClosureBatchService closureBatchService;

@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {

    closureBatchService.updateStatusAndTriggerComm();
    return null;
}

}

1 个答案:

答案 0 :(得分:0)

我突然有了这个错误。检查BATCH_JOB_INSTANCE表中您连接的数据库中是否有记录,而BATCH_JOB_EXECUTION表中没有匹配的记录。也许Spring批处理元数据表中存在其他数据不一致的情况。如果可能,请拖放并重新创建它们。

此错误是由于数据库清除作业而导致的,而该清除作业并未删除所需的所有内容。

请查看我的答案on this similar question,这可能会有所帮助。