3

I've a job parameter validator where I've mentioned compulsory and optional parameters. I run the batch and it executes correctly.

@Bean
    public JobParametersValidator validator() {

    String[] compulsoryParameters; //here I've created my compulsory parameters
    String[] optionalParams ; //here I've created my optional parameters

    return new DefaultJobParametersValidator(compulsoryParameters, optionalParams);
}

Now, If I remove an item from compulsory parameter and if I run it again. It still asks to pass the same parameter.

Caused by: org.springframework.batch.core.JobParametersInvalidException: The JobParameters contains keys that are not explicitly optional or required: [incrementerId]
        at org.springframework.batch.core.job.DefaultJobParametersValidator.validate(DefaultJobParametersValidator.java:107)
        at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:126)

Batch Configuration

Compulsory / Optional parameters are configured in application.properties

mybatch.batch.compulsoryParameters=name

mybatch.batch.optionalParameters=inputNumber

@Configuration
@EnableTransactionManagement
@EntityScan(basePackages = "com.something.*")
@EnableJpaRepositories(basePackages = "com.something.*")
@EnableBatchProcessing
@EnableCaching
@EnableConfigurationProperties
@Getter
@Setter
@ConfigurationProperties(prefix = "mybatch.batch", ignoreUnknownFields = false)
public class BatchConfig {

    /**
     * Configuration settings for the validator
     */
    private String[] compulsoryParameters;
    private String[] optionalParameters;

    /**
     * Default validator for Spring Batch
     *
     * @return
     */
    @Bean
    public JobParametersValidator validator() {

        List<String> tempList = new ArrayList<>();
        if (optionalParameters != null) {
            Collections.addAll(tempList, optionalParameters);
        }

        // Adding the run.id parameter for enabling the rerun batches
        tempList.add("run.id");
        String[] optionalParams = new String[tempList.size()];
        optionalParams = tempList.toArray(optionalParams);

        return new DefaultJobParametersValidator(compulsoryParameters, optionalParams);
    }


}

Note: All the job details are persisting in database.

Community
  • 1
  • 1
nmkyuppie
  • 1,456
  • 1
  • 14
  • 30
  • Can you remove `@EnableCaching` and then run it? Also i would say make `ignoreUnknownFields` as `true` and then check if you are getting a old record somehow? – Tarun Lalwani Aug 13 '19 at 03:48
  • Nope ! I've tried it. I'm getting the same problem – nmkyuppie Aug 13 '19 at 06:52
  • What about old record check? – Tarun Lalwani Aug 13 '19 at 07:14
  • What do you mean by old record check? My question is, 1. I've defined JobParameter Validator with 3 compulsory/optional paramters. 2. I ran the job with the 3 parameters - successful 3. I've removed 1 parameter and now I've 2 parameters in JPValidator. 4. I ran the job with 2 parameters - job failed It expects the third parameter which I've not mentioned in my project. – nmkyuppie Aug 14 '19 at 06:47
  • @nmkyuppie did you find the solution for this one now ? – shazakham Jan 22 '20 at 06:57
  • @shazakham Nope. I just changed it as a REST Service and attached the job parameters with request. – nmkyuppie Jun 18 '20 at 07:57

1 Answers1

-2

As the error states clearly every parameter either has to be part of compulsoryParameters or optionalParams.

For example for my job pass three parameters START_DATE, RUN_DATE and END_DATE. I have included all in compulsoryParameters if i remove one from this list and continue to pass then it error out saying there is one key in parameters that is not explicitly added to either of the list.

@Bean
    public JobParametersValidator validator() {
        String[] compulsoryParameters = {"START_DATE", "RUN_DATE", "END_DATE"}; //here I've created my compulsory parameters
        String[] optionalParams = {""}; //here I've created my optional parameters

        return new DefaultJobParametersValidator(compulsoryParameters, optionalParams);
    }

So try adding all the parameters passed by adding either to compulsoryParameters or optionalParams. This should resolve the issue.

Amit Naik
  • 983
  • 1
  • 5
  • 16
  • Thanks for your answer. But it doesn't answer my question. In the above example previously I had incrementerId as Compulsory/Optional parameter. After the first run I removed it from code. I got that exception after removing it from compulsory/optional Params.@amit – nmkyuppie Aug 05 '19 at 10:33
  • @nmkyuppie Did you continue to pass it in `JobParameters` after removing from the compulsory/optional Params? – Amit Naik Aug 05 '19 at 10:36
  • No! I've removed it in comp/opt params. So I didn't pass it. Still it asks for the incrementerId – nmkyuppie Aug 05 '19 at 11:19
  • can you upload all of the batch config? also i dont see code where you add Compulsory/Optional parameter? – Amit Naik Aug 06 '19 at 03:30