Java Spring Boot加Spring Batch创建Jar,仅运行特定的作业。

huangapple go评论61阅读模式
英文:

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

问题

我正在尝试创建一个带有Spring Batch应用的Spring Boot应用程序,其中将包含多个作业。

当我尝试构建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=

我尝试在application.properties中将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 trying to have a spring boot with spring batch app, which would have multiple jobs.

When I am trying to build the jar (assuming only one job) it still starts the app and actually runs the available job.

mvn clean package

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

Later, once the Jar is available, I can pass some arguments to run a specific Job. For now I have only one job in my code, will have more later. But I am unable to run even one job.

This is what I tried so far based on all online suggestions.

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(&quot;My Job&quot;)
                .incrementer(new RunIdIncrementer())
                .flow(step)
                .end()
                .build();
    }

    @Bean
    public Step getData() {
        return stepBuilderFactory
                .get(&quot;get data&quot;)
                .&lt;FileDTO, FileDTO&gt;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=

I tried to give blank here for spring.batch.job.names property.

Ran the mvn clean package, which helped me compile and build jar without running the job.

Now, I am trying to run giving the parameter,

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

It does not start the job at all.

Please note that if I remove the property spring.batch.job.names from application.properties and follow the steps, mvn clean package => will run the job and create the Jar. Which is what I don't want.
As later I will have more jobs, which I do not want to run while creating the Jar.

What am I missing here?

答案1

得分: 0

Sure, here's the translated content:

> 我只是在尝试构建一个 Jar 文件,在这里,并不想真正运行任何作业。

不要添加空属性 spring.batch.job.names=,你需要设置属性 spring.batch.job.enabled=false

> 它根本不会启动作业。

如果你移除空属性 spring.batch.job.names= 并且使用以下命令运行作业:

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

那么它应该可以工作。

英文:

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

Instead of adding the empty property spring.batch.job.names=, you need to set the property spring.batch.job.enabled=false for that.

> It does not start the job at all.

If you remove the empty property spring.batch.job.names= and run your job with:

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

then it should work.

huangapple
  • 本文由 发表于 2020年6月6日 01:15:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/62220797.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定