Java Spring 引导加上 Spring 批量创建 Jar 和 运行 仅限特定作业

Java Spring Boot plus Spring Batch create Jar and run specific job only

我正在尝试使用 spring 批处理应用启动 spring,这将有多个作业。

当我尝试构建 jar(假设只有一项工作)时,它仍然会启动应用程序并且实际上 运行 是可用的工作。

mvn clean package

我只是想在这里构建一个 Jar,实际上不想 运行 任何工作。

稍后,一旦 Jar 可用,我就可以将一些参数传递给 运行 特定的作业。现在我的代码中只有一个工作,以后会有更多。但是我连一份工作都运行做不到。

这是我目前根据所有在线建议尝试的方法。

BatchConfiguration.java

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(BatchConfiguration.class);

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    public FileDTO fileDTO;


    @Bean
    public Job fetchJob(Step step) {
        return jobBuilderFactory
                .get("My Job")
                .incrementer(new RunIdIncrementer())
                .flow(step)
                .end()
                .build();
    }

    @Bean
    public Step getData() {
        return stepBuilderFactory
                .get("get data")
                .<FileDTO, FileDTO>chunk(1)
                .reader(FileReader())
                .processor(new FileProcessor())
                .writer(new FileWriter())
                .build();
    }
}

application.properties

spring.datasource.url=jdbc:postgresql://dbhost:1000/db
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.platform=postgresql
spring.batch.job.names=

我试图在此处为 spring.batch.job.names 属性 留空。

运行 mvn clean package,它帮助我在没有 运行 工作的情况下编译和构建 jar。

现在,我正在尝试 运行 给出参数,

java -jar target\mytest-0.0.1-SNAPSHOT.jar --spring.batch.job.names=fetchJob

它根本没有启动作业。

请注意,如果我从 application.properties 中删除 属性 spring.batch.job.names 并按照步骤操作,mvn clean package => 将 运行 作业并创建 Jar。这是我不想要的。 以后我会有更多的工作,我不想在创建 Jar 时 运行。

我在这里错过了什么?

I am just trying to build a Jar here, do not want to actually run any Job.

您需要为此设置 属性 spring.batch.job.enabled=false,而不是添加空 属性 spring.batch.job.names=

It does not start the job at all.

如果您删除空的 属性 spring.batch.job.names= 和 运行 您的作业:

java -jar target\mytest-0.0.1-SNAPSHOT.jar --spring.batch.job.names=fetchJob

那么应该可以了。