Results 1 to 4 of 4

Thread: ValidationEventHandler via Jaxb2Marshaller via StaxEventItemReader

  1. #1
    Join Date
    Sep 2006
    Location
    Work in New York City
    Posts
    127

    Default ValidationEventHandler via Jaxb2Marshaller via StaxEventItemReader

    What I need is a suggestion for how to pass a List between a StaxEventItemReader and the "handleEvent(ValidationEvent event)" of a Jaxb2Marshaller. The only solution I can think of is to use a ThreadLocal (I'm using SimpleAsyncTaskExecutor). Does that seem like the best way to do this?.

    The step is in a job I configured that is reading XML via org.springframework.batch.item.xml.StaxEventItemRe ader.

    I configured the "SetUnmarshaller " method of StaxEventItemReader to unmarshall via org.springframework.oxm.jaxb.Jaxb2Marshaller, and I configured that Jaxb2Marshaller to use an implementation of the javax.xml.bind.ValidationEventHandler interface that I wrote.

    When there is an XML schema violation in an xml input fragment, the "handleEvent(ValidationEvent event)" method is invoked by org.springframework.oxm.jaxb.Jaxb2Marshaller.

    Within that "handleEvent(...)" I accumulate information about the XML errors into a List.

    I want to make that List accessible to the StaxEventItemReader so that the StaxEventItemReader can put the List into the Execution context.

    Is a ThreadLocal the best way to do this?
    Last edited by RobertGloverJr; Jan 9th, 2010 at 02:54 PM.
    Java Developer with all the usual Sun Java certifications.

  2. #2
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    ThreadLocal is a last resort I would say. When you say the StaxEventItemReader is going to stash the list in the ExecutionContext, I assume you mean your custom item reader is going to do that? Can't you just inject the ValidationEventHandler (or a common component between the two) into the reader?

  3. #3
    Join Date
    Sep 2006
    Location
    Work in New York City
    Posts
    127

    Default

    Quote Originally Posted by Dave Syer View Post
    Can't you just inject the ValidationEventHandler (or a common component between the two) into the reader?
    Wow! I never thought of the approach you suggested. It sounds perfect. I will try doing it that way.

    I wonder though if my plan to use "scope=step" on both "myCustomReader" as well as on "myCustCommonComponent"
    is the right way to use the "step" scope. (see proposed code example below)

    Code:
    <bean id="myCustomReader" scope="step"
    class="foo.MyCustomReader">
        <property name="myCustCommonComponent"  ref="myCustCommonComponent" />
        ...other configs...
    </bean>
    
    <bean id="myCustCommonComponent" class="foo.MyCustCommonComponent" scope="step"
        ... configs ....
    </bean>
    Java Developer with all the usual Sun Java certifications.

  4. #4
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    Anything stateful should be step-scoped. You can do that by explicitly declaring scope="step" as in your example, or by declaring a component as an inner bean of a step-scoped component which is not relevant here.

Posting Permissions

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