Results 1 to 2 of 2

Thread: ItemProcessor receiving one item returning more than one

Hybrid View

  1. #1
    Join Date
    Dec 2011
    Posts
    3

    Question ItemProcessor receiving one item returning more than one

    Hi all,

    I have a question regarding ItemProcessor, I need to transform an input element into different elements according to what is stored in a mapping table.

    The mapping table contains a simple mapping logic:

    A --> 1
    A --> 2
    B --> 1
    B --> 3

    So in case the ItemReader receives an "A" element, two different elements have to be returned from ItemProcessor: "1" and "2" and therefore two elements will be passed to ItemWritter (1 and 2).

    I think that the best place to include the mapping logic is an ItemProcessor, but I do not know if it is even possible, as I should return a List element from the "process" method.

    Should this logic be included in the ItemProcessor? Or if it is not possible (returning more than one element from the ItemProcessor) it has to be included directly in the ItemWritter?

    Thanks a lot and best regards.

  2. #2

    Default

    no there can only be returned one item, but you have the full Java possibilities at hand:

    • encapsulate two elements as properties of an "item" transferobject
    • return a list which contains the two elements (eventually with an abstract parent which provides discriminator mechanism)


    it might be more clever to implement the "split" inside the reader, depends on your requirements

    i would go with the "split-transformation" into a list with an itemProcessor

    read -> A -> processor -> list(A1,A2) -> writer (handles list)

    this works because the processor can return any Java class you like it to return, as long as the writer(s) can work with it

    some code examples:


    Code:
    public class ListItemProcessor implements ItemProcessor<Object, List<Object>> {
    
        @Override
        public List<Object> process(Object item) throws Exception {
            // transform item to list of objects
            return list;
        }
        
    }
    Code:
    public class ListItemWriter implements ItemWriter<List<Object>> {
    
        @Override
        public void write(List<? extends List<Object>> items) throws Exception {
            for (List<Object> list : items) {
                for (Object object : list) {
                    // if object of type A, do that
                    // if object of type B, do this
                }
            }
        }
    }
    Last edited by michael.lange; Dec 29th, 2011 at 08:31 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •