Results 1 to 4 of 4

Thread: Calling ItemWriter from Item Processor

Hybrid View

  1. #1

    Default Calling ItemWriter from Item Processor

    Hi,

    I know the way chunk based processing works is that it reads a specified amount in the ItemReader, passes this to ItemProcessor who then works on this list one by one and then passes the list to ItemWriter. I was just wondering if there was a way to call ItemWriter from Item Processor before the chunk is completed or irrespective of chunk size.

    Basically in the code below I want to call the ReportBuilder bean (ItemWriter) when I see a new Customer (notice how I am ordering by this field in the Reader). So when Processor sees a new value for this field it should write out the data it has built so far. This way each separate Customer will be a separate report.

    Code:
    <job id="settlementReportJob" xmlns="http://www.springframework.org/schema/batch">
    		<step id="createFilesStep">
    			<tasklet transaction-manager="jpaTransactionManager">
    				<chunk reader="ReportDataLoader" processor="ReportDataProcessor"
    					writer="ReportBuilder" commit-interval="-1" />
    			</tasklet>
    		</step>
    	</job>
    
    	<bean id="ReportDataLoader"
    		class="org.springframework.batch.item.database.JpaPagingItemReader"
    		scope="step">
    		<property name="entityManagerFactory" ref="AS400entityManagerFactory"/>
    		<property name="queryString" value="select a from Sdjphy01 a order by customer"/>
    	</bean>
    Thanks in advance for any help.

  2. #2

    Default

    Not sure if I understand your request, but a commit-interval=1 will send every customer to the writer after processor handles it.

  3. #3
    Join Date
    Oct 2012
    Location
    Madrid
    Posts
    20

    Default

    Commit-interval property must be a positive value. Of course, you can use the delegate pattern for the Writers.

    First, define the writer bean in your context file (bean id="writerBean".... /> and then, use this bean in your ItemProcessor by creating a FlatFileItemProcessor<T> writerName (with @Autowired), but you need to define the ItemWriter as non-transactional (there is a property in the FlatFileItemWriter) and open and close the file manually.

    Hope to help you.

  4. #4
    Join Date
    Sep 2008
    Location
    Chicagoland, IL
    Posts
    366

    Default

    I'm wondering if you have the wrong thing defined as the "Item" you are passing through the chunk. What is returned by the ItemReader (or ItemProcessor if it is different)? I would expect that to be a customer object. That way you could set the commit count to 1 and have each one written separately via normal chunk processing.

    You don't want to call the writer directly.
    Michael Minella
    Spring Batch Lead
    Author - Pro Spring Batch
    http://www.michaelminella.com
    Twitter: @MichaelMinella

Posting Permissions

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