无法执行 CommandLineRunner - Spring Batch

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50190952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:35:49  来源:igfitidea点击:

Failed to execute CommandLineRunner - Spring Batch

springspring-bootspring-batch

提问by Praveen Kumar

Hi I am very new to Spring batch and I am getting the following exception which I am not able to resolve:

嗨,我对 Spring 批处理非常陌生,但出现以下异常,但无法解决:

java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:781) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at com.demo.BatchDemo.main(KnpBatchApplication.java:16) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_72]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_72]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_72]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_72]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.1.RELEASE.jar:2.0.1.RELEASE]
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653) ~[na:1.8.0_72]
at java.util.ArrayList.get(ArrayList.java:429) ~[na:1.8.0_72]
at org.springframework.batch.core.JobParametersBuilder.getNextJobParameters(JobParametersBuilder.java:265) ~[spring-batch-core-4.0.1.RELEASE.jar:4.0.1.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:162) ~[spring-boot-autoconfigure-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:179) ~[spring-boot-autoconfigure-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:134) ~[spring-boot-autoconfigure-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:128) ~[spring-boot-autoconfigure-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:797) [spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]
... 10 common frames omitted

My Code here:

我的代码在这里:

    @SpringBootApplication
    public class BatchDemo {
    public static void main(String args[])
    {
        SpringApplication.run(BatchDemo.class);
    }
}

I was able to resolve it by adding exclude = BatchAutoConfiguration.class. But I would like to find the actual cause and fix it.

我能够通过添加exclude = BatchAutoConfiguration.class. 但我想找到真正的原因并解决它。

I am not passing any commandline arguments and this exception doesn't occur all the time.

我没有传递任何命令行参数,并且这个异常不会一直发生。

pom.xml

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>demo</groupId>
<artifactId>demo_batch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo_batch</name>
<url>http://maven.apache.org</url>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.batch</groupId>
        <artifactId>spring-batch-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

回答by Ram

When you use @SpringBootApplication, Spring's autoConfiguration is enabled by default. Since you have spring batch dependencies loaded in your classpath, Spring Batch AutoConfiguration is enabled as well.

使用时@SpringBootApplication,Spring 的自动配置默认启用。由于您在类路径中加载了 Spring Batch 依赖项,因此也启用了 Spring Batch AutoConfiguration。

During application startup, Spring Batch's autoconfiguration (BatchAutoConfiguration) creates a Runner and it runs all the jobs defined in your BatchConfig.

在应用程序启动期间,Spring Batch 的自动配置 (BatchAutoConfiguration) 会创建一个 Runner 并运行您的 BatchConfig 中定义的所有作业。

You can disable this behavior by either setting spring.batch.job.enabledproperty to falsein your application properties or by simply excluding the Autoconfiguration for Batch just like as you did.

您可以通过禁用任何设置这种行为spring.batch.job.enabled属性,false在您的应用程序属性或者简单地排除自动配置批处理,就像你一样。

Please refer hereand herefor more info.

请参阅此处此处了解更多信息。

回答by Corwin Newall

For anyone who wants their batch jobs to actually execute, and is encountering this error, try checking if there is any inconsistency in the records in your Spring batch metadata tables.

对于希望批处理作业实际执行并遇到此错误的任何人,请尝试检查 Spring 批处理元数据表中的记录是否存在任何不一致之处。

To get the next JobParameters, Spring Batch searches for the latest BATCH_JOB_INSTANCEwith the same JOB_NAMEas your job, then looks for the matching BATCH_JOB_EXECUTIONrecord in the database (linked by JOB_INSTANCE_ID).

为了获得下一个JobParameters,Spring Batch 搜索BATCH_JOB_INSTANCEJOB_NAME您的工作相同的最新,然后BATCH_JOB_EXECUTION在数据库中查找匹配的记录(由 链接JOB_INSTANCE_ID)。

We had an in-house task running which deleted records from BATCH_JOB_EXECUTIONbut, due to an error, did not delete the matching record from BATCH_JOB_INSTANCE.

我们有一个内部任务正在运行,它从 中删除了记录,BATCH_JOB_EXECUTION但由于错误,没有从 中删除匹配的记录BATCH_JOB_INSTANCE

In one environment, it had been more than 3 months since we had executed any jobs, so we had no BATCH_JOB_EXECUTIONrecords anymore, but plenty of BATCH_JOB_INSTANCErecords. We repeatedly threw the stack trace listed in this question, across multiple different Spring Batch applications, and could not find a solution anywhere online.

在一个环境中,我们已经有3个多月没有执行任何作业了,所以我们已经没有BATCH_JOB_EXECUTION记录了,但是有很多BATCH_JOB_INSTANCE记录。我们反复抛出这个问题中列出的堆栈跟踪,跨越多个不同的 Spring Batch 应用程序,并且在网上的任何地方都找不到解决方案。

We resolved the issue by

我们解决了这个问题

  1. Running SQL to delete from BATCH_JOB_INSTANCEwhere there is no matching record in BATCH_JOB_EXECUTION- this unblocked the job in the short term; and,
  2. Replacing our purge task with a job using spring-batch-toolkit; it provides a RemoveSpringBatchHistoryTaskletthat deletes the records correctly.
  1. 运行 SQL 从BATCH_JOB_INSTANCE没有匹配记录的地方删除BATCH_JOB_EXECUTION- 这在短期内解除了工作的阻塞;和,
  2. 使用spring-batch-toolkit用作业替换我们的清除任务;它提供了一个RemoveSpringBatchHistoryTasklet来正确删除记录。