Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JobParameters - java.lang.String cannot be cast to java.lang.Long [BATCH-2803] #803

Open
spring-issuemaster opened this issue Apr 4, 2019 · 0 comments

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Apr 4, 2019

PolkovnikMedved opened BATCH-2803 and commented

Hello, 

 

I've recently started to use Spring Batch and I have a small problem with the [JobParameters|https://github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/main/java/org/springframework/batch/core/JobParameters.java] class.

 

When you have a job that uses RunIdIncrementer and you start your job from command line using following parameter:

run.id=4 // The value doesn't matter

then Spring Batch is writing this value in database in the BATCH_JOB_EXECUTION_PARAMS.STRING_VAL field. 

 

When I have a failed job and I try to start a new one with a new run.id (let us say 6), Spring Batch is looking for last execution and last parameters. It finds the last job with a parameter run.id=4 (String from database) but Its trying to convert it into a Long value in the JobParameters class by doing this: 

 

public Long getLong(String key){
   if (!parameters.containsKey(key)) {
      return 0L;
   }
   Object value = parameters.get(key).getValue();
   return value==null ? 0L : ((Long)value).longValue();
}

but, parameters.get(key).getValue() return "4",  and then when we try to make ((Long)value).longValue() we have the following error:

java.lang.String cannot be cast to java.lang.Long

So, I feel that Object value = "4" and then, in the next line value is interpreted as a String. 

 

Something like this would work:

public Long getLong(String key){
   if (!parameters.containsKey(key)) {
      return 0L;
   }
   Object value = parameters.get(key).getValue();
   return value==null ? 0L : Long.parseLong((String)value);
}

 

But I'm not sure that it's the best solution as I don't know all the parts from Spring Batch. 

Another simple solution is to pass parameter like this: 

run.id(long)=4 // The value doesn't matter

 

This way of doing exists since this [commit|https://github.com/spring-projects/spring-batch/commit/fc37dc27c98b64830c62b1c0f82a1d93491db654#diff-58934d3d42d1247ae1936991da749faf] and before. It affects more than the 3 versions that I've set here. 


Affects: 4.1.1, 4.0.3, 4.1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.