0
votes

I am using spring batch integration to poll for a file and process it and was looking for some guidance on the job parameters aspect of it. I am using the following to create a job launch request and turn a file into the request

@Transformer
public JobLaunchRequest toRequest(Message<File> message) {
    JobParametersBuilder jobParametersBuilder =
            new JobParametersBuilder();

    jobParametersBuilder.addString(fileParameterName,
            message.getPayload().getAbsolutePath());
    jobParametersBuilder.addLong("time", new Date().getTime());

    return new JobLaunchRequest(job, jobParametersBuilder.toJobParameters());
}

On starting up the application for the first time there is only one parameter run.id. If i add a file to repository that the file poller is looking in it creates 2 parameters in the db: fileParameterName and time. If I start the application again it will use the previous values for parameters fileParameterName and time and add a new run.id. The message on the initial start up is :

Job: ... launched with the following parameters: [{run.id=1}]

If I add a file my application handles the file correctly:

Job: ... launched with the following parameters:[{input.file.name=C:\Temp\test.csv, time=1472051531556}]

but if I stop and start the application again I get the following message:

Job: ... launched with the following parameters: [{time=1472051531556, run.id=1, input.file.name=C:\Temp\test.csv}]

My question is why on this start up it is looking at the previous parameters? Is there a way to add the current time as a parameter on start up instead of the previous time so I dont get "A job instance already exists and is complete for parameters={}"? Or to stop the jobs running on start up?

Also if the application is running and I add a file it will enter the toRequest method but it does not on start up.

Any help would be great. Thanks

1

1 Answers

1
votes

We should have a parameter as 'run.id' with 'current timestamp' to where we kick off Spring Batch Job. This is how we kick off a Spring Batch job from shell script.

RUN_ID=$(date +"%Y-%m-%d %H:%M:%S") JOB_PARAMS="filename=XXX"

$JAVA_HOME org.springframework.batch.core.launch.support.CommandLineJobRunner springbatch_XXX.xml SpringBatchJob run.id="$RUN_ID" ${JOB_PARAMS}