Issue Details (XML | Word | Printable)

Key: BATCH-300
Type: Sub-task Sub-task
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Lucas Ward
Reporter: Douglas C. Kaminsky
Votes: 1
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
Spring Batch
BATCH-670

Improve typesafety of ItemProvider/Processor paradigm

Created: 18/Jan/08 02:31 PM   Updated: 23/Sep/08 07:10 PM
Component/s: None
Affects Version/s: 1.0-m3
Fix Version/s: 2.0.0.M1

Time Tracking:
Original Estimate: 2d
Original Estimate - 2d
Remaining Estimate: 2d
Remaining Estimate - 2d
Time Spent: Not Specified
Remaining Estimate - 2d

File Attachments: 1. Text File AbstractTypesafeItemWriter.patch (2 kB)
2. Text File AbstractTypesafeItemWriterTests.patch (2 kB)
3. Text File ItemTypesafetyException.patch (2 kB)



 Description  « Hide
Every item processor I have written has started with logic such as:

if ! (data instanceof ClassProcessorExpects)
  throw exception

Or

assert data instanceof ClassProcessorExpects : "Data of incorrect type passed to processor"



This seems like something that the ItemProviderProcessTasklet / RestartableItemProviderTasklet should handle. Short of making ItemProcessor a generic class, this would require the following changes:

1.) add to ItemProcessor interface a new method:

   canProcessType( Class class )

2.) add type check to above mentioned tasklets

   e.g.
   while( item = itemProvider.next() )
       if( ! itemProcessor.canProcessType( item.getClass() )
           (throw exception, check if skippable, etc)

3.) update samples, etc.

 All   Comments   Work Log   Change History   FishEye   Builds      Sort Order: Ascending order - Click to sort in descending order
Douglas C. Kaminsky added a comment - 18/Jan/08 02:39 PM
component should be execution. thanks

Robert Kasanicky added a comment - 21/Jan/08 03:25 AM
Alternative way to improve ItemProcessor type-safety is to add an AbstractTypeSafeItemProcessor that would contain the type-checking logic.

I think the ClassCastException you get without implementing type-checks is informative enough in general, so I guess a non-invasive solution makes sense.

Douglas C. Kaminsky added a comment - 21/Jan/08 09:24 AM
The problem with catching a ClassCastException is that such an exception is not necessarily because the wrong type of data was passed to the processor. You could potentially get a ClassCastException from anywhere in your process() code. I'm specifically looking to isolate the condition where you simply have a bad configuration with incompatible item provider / processor - e.g. provider gives you String, processor wants to operate on Integer.

Of course, you could always add a call to Integer.valueOf to your processor to handle this but that's your choice. If that were the case, your processor would then be a String processor, so the same type checking would make sense.

Douglas C. Kaminsky added a comment - 27/Feb/08 08:24 PM
Attached patches that potentially address this issue. Feedback gratefully accepted.

Ben Hale added a comment - 20/Mar/08 04:32 AM
Java 5 generics will help with development time enforcement of this (test cases will need to match), but any API change like this will need to be in a major rev.

Kyle Goodwin added a comment - 03/Apr/08 01:23 PM
I'd offer my vote for generic-based interfaces, especially for things like ItemTransformer. A generic ItemTransformer interface could be offered in addition to the current API as an alternative to waiting for a major rev. If this is an option I would not be adverse to contributing to such an effort.

Douglas C. Kaminsky added a comment - 03/Apr/08 02:05 PM
I have a generic-enabled version of core already locally that's already tested and working - that's not the issue. Spring Source as a rule (if I remember properly) promises compatibility with up to two major versions past, which means since Java is on 6.0 right now that all their code must be 1.4.2 compatible.

(Apparently, Spring Integration is the exception to this rule? -- seems they already use generics as part of their core API)

That's why I contributed the 1.4.2-friendly AbstractTypesafeItemWriter as the proposed solution instead of saying "you're out of luck until we use Java 5, and since that's a major API change, you're out of luck until Spring Batch 2.0"

Personally, I am of the belief that new frameworks shouldn't have to adhere to the back-compatibility rule, but what are you going to do? Once Spring Core / Spring Batch goes full Java 5.0 you know someone's going to request a 1.4.2 compatible backport version anyway, so it's kind of a no-win situation no matter how you cut it.

Kyle Goodwin added a comment - 03/Apr/08 02:10 PM
I've seen other projects with a structure that has something like a spring-batch-core-tiger project that adds Java 5 only features to supplement the API of spring-batch-core. If this isn't going to be part of the official release structure I'd still be really interested in working on it as a 3rd-party extension. Would you mind sharing your generic-enabled version of the core code?

Douglas C. Kaminsky added a comment - 03/Apr/08 02:59 PM
I have no problem contributing it to the Spring Batch project, but the general rule has been that Spring Source does not accept code that they are unwilling or unable to maintain, since they sell service contracts. Until Spring Source as a company moves forward to Java 5.0 (which will, if I understand properly, most likely happen when Java 7.0 is released) it's pointless for me to post it - however, I will definitely put it forward when the time comes.

Kyle Goodwin added a comment - 03/Apr/08 03:19 PM
Sorry I wasn't clear, I'm basically asking if I could use it personally until such time as they consider the issue again. It will save me rewriting something very similar for my use on my current project since I would want to use a generics-based approach.

Douglas C. Kaminsky added a comment - 03/Apr/08 04:16 PM
Unfortunately that's not an option - my code belongs to my employer until such time as it is contributed to Spring Batch. If I were to contribute it now, knowing it wouldn't be accepted, it would be tantamount to sending it to you personally.

Now don't get the wrong impression -- I didn't mention I had a working version locally to try and lure you in to a false sense of hope or anything :)

I was just trying to explain that not having something written in Java 5 was not really the issue, it's the back-compatibility promise and the software support responsibilities of Spring Source. If someone from Spring Source comes forward and says "contribute it and we'll support it as an add-on or extension" or says "we're adopting Java 5.0, send it to us" then I'll go ahead and do it. Until then, I'm sorry to say it's proprietary.

Robert Kasanicky added a comment - 24/Jul/08 07:12 AM
I suppose this issue can be closed since ItemReaders and ItemWriters have been 'generified'?

Douglas C. Kaminsky added a comment - 24/Jul/08 10:36 AM
Sounds good to me. Java 5 generics solve this problem nicely.

Lucas Ward added a comment - 24/Jul/08 05:54 PM
Resolving as fixed (since parameterization technically fixed the issue)