Results 1 to 10 of 11

Thread: Spring batch : MultiResourceItemReader

Hybrid View

  1. #1
    Join Date
    Feb 2007
    Posts
    3

    Default Spring batch : MultiResourceItemReader

    I want to use a org.springframework.batch.item.file.MultiResourceI temReader to read multiple XML files in a directory.

    When I declare my bean of type MultiResourceItemReader, I have to reference a "ressources" property. So I reference the folder where my XML are stored with a wildcard to include all files, this is OK. But I also have to reference a ItemReader, which I do be referencing a StaxEventItemReader. Here is my problem: When I declare my StaxEventItemReader bean, I have to reference a "ressource" property, if not Spring won't instantiate the bean. Is there a way to reference a ItemReader prototype without "resource" attribute that will be assigned by the MultiResourceItemReader when iterating over his "ressources" attribute?

    Here's a snippet:

    <bean id="multiResourceReader" class="org.springframework.batch.item.file.MultiRe sourceItemReader">
    <property name="resources" value="file:///mydata/data/*.xml" />
    <property name="delegate" ref="xmlFileReader" />
    </bean>

    <bean id="xmlFileReader" class="org.springframework.batch.item.xml.StaxEven tItemReader">
    <property name="fragmentRootElementName" value="orderNotification"/>
    <!--<property name="resource" ref="iDontKnow"/>-->
    <property name="fragmentDeserializer">
    <bean class="org.springframework.batch.item.xml.oxm.Unma rshallingEventReaderDeserializer">
    <constructor-arg>
    <bean class="org.springframework.oxm.xmlbeans.XmlBeansMa rshaller"/>
    </constructor-arg>
    </bean>
    </property>
    </bean>

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

    Default

    I think what you have here is (http://jira.springframework.org/browse/BATCH-734). It was fixed in trunk, but not in 1.1.x yet. If it's a showstopper make a comment there and we can get a fix into the branch (maybe too late for 1.1.2 now).

  3. #3
    Join Date
    May 2008
    Posts
    9

    Default

    Dave,

    Per your request, I commented in Jira 734 on the availability of the fix. I sure do hope you can squeeze it into V1.1.2. My workaround is kludgey. Your fix is much more comprehensive.

    Thanks.

    GregA

  4. #4
    Join Date
    Feb 2007
    Posts
    3

    Default

    Thanks for the reply.

    My problem is not exactly the one described in Jira 734.

    Maybe I'm not using MultiResourceItemReader correctly? When I declare my MultiResourceItemReader bean and reference a folder with XML files in the "ressources" attribute it is OK. The problem is when I reference the "delegate" attribute. This attribute has to be an ItemReader and this ItemReader bean has to be declared with a "ressource" attribute.

    Here is what I don't understand: If I declare the "ressources" attribute in my MultiResourceItemReader bean, why do I have to assign a specific "ressource" in my ItemReader? I thought this would be done dynamically by the MultiResourceItemReader?

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

    Default

    BATCH-734 is the problem, trust me, and you are using MultiResourceItemReader correctly. The MultiResourceItemReader does indeed set the resource dynamically, but it never gets a chance up to 1.1.1 because of the null check on inistialisation of StaxEventItemReader.

  6. #6
    Join Date
    Feb 2007
    Posts
    3

    Default

    Thanks, looking forward to V1.1.2!

  7. #7
    Join Date
    May 2009
    Posts
    4

    Default

    Just a little question, I tried to modify the sample XmlFunctionalTests in the spring batch package to make it work with my own xml files

    So I modified :

    Code:
    	<bean id="itemReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
    		<property name="fragmentRootElementName" value="customer" />
    		<property name="resource" value="data/iosample/input/input.xml" />
    		<property name="unmarshaller" ref="customerCreditMarshaller" />
    	</bean>
    by

    Code:
      <bean id="itemReader" class="org.springframework.batch.item.file.MultiResourceItemReader">
        <property name="resources" value="data/iosample/input/input*.xml" />
        <property name="delegate" ref="xmlItemReader" />
      </bean>
    
    
    	<bean id="xmlItemReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
    		<property name="fragmentRootElementName" value="Cash" />
    		<property name="unmarshaller" ref="apsysCashMarshaller" />
    	</bean>
    but the file describing the job is
    Code:
    	<job id="ioSampleJob">
    		<step id="step1">
    			<tasklet>
    				<chunk reader="itemReader" writer="itemWriter" commit-interval="6"/>
    			</tasklet>
    		</step>
    	</job>
    and of course I get the error :

    Code:
    NoSuchBeanDefinitionException: No unique bean of type [org.springframework.batch.item.ItemReader] is defined: expected single matching bean but found 2: [itemReader, xmlItemReader]
    I have two beans with the same type so the autowire is not possible.
    I'm not familiar with xbeans, I'm used to the old notation bean = ...

    It is possible to use the previous notation but without autowiring ?

Posting Permissions

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