Harry Coder
Harry Coder

Reputation: 2740

Spring Batch JobInstance to run more than once

I am new to Spring Batch and trust me I have read a lot those day about it to try to be familiar with its concepts. I am a bit confused about how JobInstance, RunIdIncrementer, JobParamaters work and I would like to understand some aspects :

Edit : See the code below

@Bean
public Job batchExecution() {

    return jobs
            .get("BatchJob")
            .incrementer(new JobIdIncrementer())
            .start(downloadFile())
            .next(archiveFile())
            .next(readFile())
            .build();
}

The JobIdIncrementer :

public class JobIdIncrementer implements JobParametersIncrementer {

    private static String RUN_ID_KEY = "run.id";
    private String key;

    public JobIdIncrementer() {
        this.key = RUN_ID_KEY;
    }

    public void setKey(String key) {
        this.key = key;
    }

    @Override
    public JobParameters getNext(JobParameters parameters) {
        JobParameters params = parameters == null ? new JobParameters() : parameters;
        long id = new Date().getTime();
        return (new JobParametersBuilder(params)).addLong(this.key, Long.valueOf(id)).toJobParameters();
    }
}

When I start the Batch the fist time, I have this log (it works fine) :

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.0.RELEASE)

"2018-08-08 15:36:03 - Starting Application on MA18-012.local with PID 39543 
""2018-08-08 15:36:05 - HikariPool-1 - Starting...
""2018-08-08 15:36:05 - HikariPool-1 - Start completed.
""2018-08-08 15:36:06 - HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
""2018-08-08 15:36:06 - HHH000412: Hibernate Core {5.2.14.Final}
""2018-08-08 15:36:06 - HHH000206: hibernate.properties not found
""2018-08-08 15:36:06 - HHH80000001: hibernate-spatial integration enabled : true
""2018-08-08 15:36:06 - HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
""2018-08-08 15:36:06 - HHH000400: Using dialect: org.hibernate.spatial.dialect.mysql.MySQL5SpatialDialect
""2018-08-08 15:36:06 - HHH000400: Using dialect: org.hibernate.spatial.dialect.mysql.MySQLSpatialDialect
""2018-08-08 15:36:09 - Started Application in 6.294 seconds (JVM running for 6.812)
""2018-08-08 15:36:09 - Loading the file name
""2018-08-08 15:36:23 - Downloading the file
""2018-08-08 15:36:24 - Archiving the file 
""2018-08-08 15:36:24 - Unzipping the file
""2018-08-08 15:36:24 - Removing the file
""2018-08-08 15:36:51 - Reading the file
""2018-08-08 15:36:52 - HHH000397: Using ASTQueryTranslatorFactory
""2018-08-08 15:36:54 - HikariPool-1 - Shutdown initiated...
""2018-08-08 15:36:54 - HikariPool-1 - Shutdown completed.

The second time when start the Batch, I have this one (no error, but it starts and closes immediatly):

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.0.RELEASE)

"2018-08-08 15:38:28 - Starting Application on MA18-012.local with PID 39638
""2018-08-08 15:38:28 - No active profile set, falling back to default profiles: default
""2018-08-08 15:38:30 - HikariPool-1 - Starting...
""2018-08-08 15:38:30 - HikariPool-1 - Start completed.
""2018-08-08 15:38:30 - HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
""2018-08-08 15:38:30 - HHH000412: Hibernate Core {5.2.14.Final}
""2018-08-08 15:38:30 - HHH000206: hibernate.properties not found
""2018-08-08 15:38:30 - HHH80000001: hibernate-spatial integration enabled : true
""2018-08-08 15:38:30 - HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
""2018-08-08 15:38:31 - HHH000400: Using dialect: org.hibernate.spatial.dialect.mysql.MySQL5SpatialDialect
""2018-08-08 15:38:31 - HHH000400: Using dialect: org.hibernate.spatial.dialect.mysql.MySQLSpatialDialect
""2018-08-08 15:38:33 - Started Application in 6.376 seconds (JVM running for 6.873)
""2018-08-08 15:38:34 - HikariPool-1 - Shutdown initiated...
""2018-08-08 15:38:34 - HikariPool-1 - Shutdown completed.

Upvotes: 0

Views: 2692

Answers (1)

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31600

When you run a Job and the JobInstance name is already in the BATCH_JOB_INSTANCE table, the Job is not launched. So, what is the best way to generate a new name for my JobInstance?

You don't need to change the job name each time. Your job can have multiple job instances, each one is identified by a key which is the hash of the (identifying) job parameters used to run your job.

Is it a good practice to always generate a new name when I want to launch my job?

No, you don't need to generate a new name (it is the same job so the name should not change). What you need to do is to create a new job instance each time by specifying different job parameters.

As job is supposed to be scheduled to run many times. What is the best practice to create a Batch (Job) to be scheduled to run many times without generating a new name?

Depending on the frequency of your job, you can add the date as a job parameter. For example, if your job is scheduled to run daily, the current date is a good parameter. Check this section of the documentation: https://docs.spring.io/spring-batch/4.0.x/reference/html/domain.html#job it provides an example.

Does the RunIdIncrementer() is supposed to create an id to generate a new JobName?

The RunIdIncrementer increments the run.id job parameter so you get a new instance of JobParameters which will result in a new job instance. But the job name will still the same. Here is how it works: https://github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/RunIdIncrementer.java#L44

Upvotes: 1

Related Questions