Home > other >  Spring Batch 5.0 with Spring Boot -- Tasklet job not starting automatically
Spring Batch 5.0 with Spring Boot -- Tasklet job not starting automatically

Time:02-01

I have a very simple Spring Boot project with Spring Batch 5.0 and a CommmandLineRunner. There is one Job, one step, and one tasklet that simply prints "Running". I followed directions and would expect the job to start and complete.

No errors; the Batch database table are being created; the Job and Step beans are being created.

It just doesn't run.

I would be grateful for any insights or assistance!

NOTE: If I launch the job explicitly from the CommandLineRunner using an autowired JobLauncher, it works fine. It just doesn't launch the job automatically (as promised).

NOTE 2: Removing @EnableBatchProcessing makes no difference.

Code follows

application.properties:

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost/postgres
spring.batch.jdbc.initialize-schema=always
spring.batch.job.enabled=true

Application.java:

@SpringBootApplication
@EnableBatchProcessing
public class Application implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }


    @Override
    public void run(String... args) throws Exception {
        System.out.println("Starting...");
        Thread.sleep(2000);
    }
}

Job configuration:

@Configuration
public class AbacusJobConfiguration {

    Logger logger = LoggerFactory.getLogger(AbacusJobConfiguration.class);

    @Bean
    public Step step(JobRepository jobRepository, PlatformTransactionManager transactionManager, Tasklet tasklet1) {
        logger.info("Building step");
        return new StepBuilder("myTasklet", jobRepository)
                .tasklet(tasklet1, transactionManager).allowStartIfComplete(true)
                .build();

    }
    @Bean
    public Job job(JobRepository jobRepository, PlatformTransactionManager transactionManager, Step step1) {
        return new JobBuilder("myJob", jobRepository)
                .incrementer(new RunIdIncrementer())
                .start(step1)
                .build();
    }

    @Bean
    public MyTasklet myTasklet() {
        logger.info("Building tasklet");
        var tasklet = new MyTasklet();
        return tasklet;
    }

}

Tasklet:

public class MyTasklet implements Tasklet {
    @Override
    public RepeatStatus execute(StepContribution  stepContribution, ChunkContext chunkContext) throws Exception {
        System.out.println("Running MyTasklet");
        return RepeatStatus.FINISHED;
    }
}

Runtime log:

2023-01-30T10:54:07.407-05:00  INFO 72774 --- [           main] c.optionmetrics.abacuscli.Application    : Starting Application using Java 17.0.6 with PID 72774 (/Users/dhait/IdeaProjects/abacus-cli/target/classes started by dhait in /Users/dhait/IdeaProjects/abacus-cli)
2023-01-30T10:54:07.410-05:00  INFO 72774 --- [           main] c.optionmetrics.abacuscli.Application    : No active profile set, falling back to 1 default profile: "default"
2023-01-30T10:54:07.626-05:00  INFO 72774 --- [           main] o.s.b.c.c.annotation.BatchRegistrar      : Finished Spring Batch infrastructure beans configuration in 2 ms.
2023-01-30T10:54:07.836-05:00  INFO 72774 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-01-30T10:54:07.918-05:00  INFO 72774 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection org.postgresql.jdbc.PgConnection@6ae7deac
2023-01-30T10:54:07.919-05:00  INFO 72774 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2023-01-30T10:54:07.928-05:00  INFO 72774 --- [           main] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: POSTGRES
2023-01-30T10:54:07.940-05:00  INFO 72774 --- [           main] c.o.abacuscli.AbacusJobConfiguration     : Building tasklet
2023-01-30T10:54:07.942-05:00  INFO 72774 --- [           main] c.o.abacuscli.AbacusJobConfiguration     : Building step
2023-01-30T10:54:07.954-05:00  INFO 72774 --- [           main] .c.a.BatchObservabilityBeanPostProcessor : No Micrometer observation registry found, defaulting to ObservationRegistry.NOOP
2023-01-30T10:54:07.959-05:00  INFO 72774 --- [           main] .c.a.BatchObservabilityBeanPostProcessor : No Micrometer observation registry found, defaulting to ObservationRegistry.NOOP
2023-01-30T10:54:07.963-05:00  INFO 72774 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
2023-01-30T10:54:08.040-05:00  INFO 72774 --- [           main] c.optionmetrics.abacuscli.Application    : Started Application in 0.882 seconds (process running for 1.246)
Starting...
2023-01-30T10:54:10.050-05:00  INFO 72774 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2023-01-30T10:54:10.054-05:00  INFO 72774 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

CodePudding user response:

With Spring Boot 3, there is no need for @EnableBatchProcessing. If you add it, the auto-configuration of Spring Batch (including the automatic launching of jobs at startup) will back off.

This is mentioned in the migration guide of Spring Boot 3.

EDIT: Not able to reproduce the issue. Things are working as expected with Spring Batch 5 and Spring Boot 3.

$>curl https://start.spring.io/starter.zip -d dependencies=h2,batch -d type=maven-project -o my-batch-job.zip
$>unzip my-batch-job.zip
$> # Paste AbacusJobConfiguration and MyTasklet code in the demo package   add imports
$>./mvnw package
$>./mvnw spring-boot:run

This prints:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.0.2)

2023-01-31T07:48:24.206 01:00  INFO 91983 --- [           main] com.example.demo.DemoApplicationTests    : Starting DemoApplicationTests using Java 17.0.4 with PID 91983 (started by mbenhassine in /Users/mbenhassine/Downloads/so75287102)
2023-01-31T07:48:24.207 01:00  INFO 91983 --- [           main] com.example.demo.DemoApplicationTests    : No active profile set, falling back to 1 default profile: "default"
2023-01-31T07:48:24.625 01:00  INFO 91983 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-01-31T07:48:24.779 01:00  INFO 91983 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:0b237f7c-fec8-446f-b2ac-086801ddb7fd user=SA
2023-01-31T07:48:24.780 01:00  INFO 91983 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2023-01-31T07:48:24.858 01:00  INFO 91983 --- [           main] c.example.demo.AbacusJobConfiguration    : Building tasklet
2023-01-31T07:48:24.860 01:00  INFO 91983 --- [           main] c.example.demo.AbacusJobConfiguration    : Building step
2023-01-31T07:48:24.957 01:00  INFO 91983 --- [           main] com.example.demo.DemoApplicationTests    : Started DemoApplicationTests in 0.91 seconds (process running for 1.585)
2023-01-31T07:48:24.958 01:00  INFO 91983 --- [           main] o.s.b.a.b.JobLauncherApplicationRunner   : Running default command line with: []
2023-01-31T07:48:24.990 01:00  INFO 91983 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=myJob]] launched with the following parameters: [{'run.id':'{value=1, type=class java.lang.Long, identifying=true}'}]
2023-01-31T07:48:25.009 01:00  INFO 91983 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [myTasklet]
Running MyTasklet
2023-01-31T07:48:25.017 01:00  INFO 91983 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [myTasklet] executed in 8ms
2023-01-31T07:48:25.025 01:00  INFO 91983 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=myJob]] completed with the following parameters: [{'run.id':'{value=1, type=class java.lang.Long, identifying=true}'}] and the following status: [COMPLETED] in 23ms
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.556 s - in com.example.demo.DemoApplicationTests
2023-01-31T07:48:25.312 01:00  INFO 91983 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2023-01-31T07:48:25.315 01:00  INFO 91983 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
  • Related