Results 1 to 5 of 5

Thread: Accessing the currently processing filename

  1. #1
    Join Date
    Mar 2006
    Location
    Sydney
    Posts
    6

    Question Accessing the currently processing filename

    Hi there,

    I'm using a MultiResourceItemReader to load files into a FlatFileItemReader which are then run through a ItemProcessor before being written out to FlatFileItemWriter.

    The ItemProcessor is updating a database. What I want to do for operational purposes it write a separate record to the database to log each file that is processed.

    I've tried using a ChunkListener and a StepExecutionListener, but don't seem to be able to find a handle to get the current resource.

    I've also injected the MultiResourceItemReader itself into a ChunkListener, but the getCurrentResource.getFilename() seems to always null..

    Any suggestions on the best way to do this?

    Thanks,
    Alex.
    "We should move forwards, not backwards, upwards, not sideways, and always twirling, twirling towards victory!"

  2. #2

    Default

    as long as you don't use the MultiResourceItemReader with step scope it should work, can you please post the relevant spring batch configuration?

  3. #3
    Join Date
    Mar 2006
    Location
    Sydney
    Posts
    6

    Default

    Quote Originally Posted by michael.lange View Post
    as long as you don't use the MultiResourceItemReader with step scope it should work, can you please post the relevant spring batch configuration?
    Thanks for the reply. My Job looks like this:

    Code:
    <batch:job id="jobProcessReconcilationFiles">
      		<batch:step id="step1" next="step2">
    			<batch:tasklet>
    				<!-- Read in the CAR files and update database -->
    				<batch:chunk reader="carMultiReader" processor="carProcessor" writer="carWriter" commit-interval="100"/>
            		<batch:listeners>
                		<batch:listener ref="carChunkListener"/>
            		</batch:listeners>				
    			</batch:tasklet>
      		</batch:step>
      		<batch:step id="step2" next="step3">
    			<batch:tasklet>
    				<!-- Read in the EPAR files and update database -->
    				<batch:chunk reader="eparMultiReader" processor="eparProcessor" writer="eparWriter" commit-interval="1"/>
    			</batch:tasklet>
      		</batch:step>
      		<batch:step id="step3">
    			<batch:tasklet>
    				<!-- Generate the MSR report from the database -->
    				<batch:chunk reader="msrHibernateReader" processor="msrProcessor" writer="msrWriter" commit-interval="1"/>
    			</batch:tasklet>
      		</batch:step>  	
      	</batch:job>
    
            <bean id="carChunkListener" class="au.com.westpac.telecom.reconciliation.listeners.ChunkLogger"/>
    And the listener like this:

    Code:
    public class ChunkLogger implements ChunkListener {
    
    	private static Logger log = Logger.getLogger(ChunkLogger.class);
    	@Autowired
    	private MultiResourceItemReader carMultiReader;
    	
    	public void beforeChunk() {
    		log.info("beforeChunk: " + this.carMultiReader.getCurrentResource());
    	}	
    	
    	public void afterChunk() {
    		log.info("afterChunk");
    	}
    
    	public void setCarMultiReader(MultiResourceItemReader carMultiReader) {
    		this.carMultiReader = carMultiReader;
    	}
    }
    What I'd ideally like to have is something that can detect when the resource changes that doesn't rely on chunks.
    "We should move forwards, not backwards, upwards, not sideways, and always twirling, twirling towards victory!"

  4. #4

    Default

    How is your carMultiReader configured and did you check that @Autowire works as intended?


    What I'd ideally like to have is something that can detect when the resource changes that doesn't rely on chunks.
    well you could wrap the MultiResourceItemReader and force it to give the current.resource

    another way which builds on that, is to store the file names in a Spring Bean (e.g. HashMap) and read that out in StepExecutionListener.afterStep


    [EDITh says]

    Dave Syer mentioned another way by accessing the step scoped proxy directly

    examples:



    works only if StepScope is configured to make targets accessible

    Code:
     <bean class="org.springframework.batch.core.scope.StepScope" p:proxyTargetClass="true" />
    Last edited by michael.lange; Jan 9th, 2012 at 03:45 AM.

  5. #5
    Join Date
    Mar 2006
    Location
    Sydney
    Posts
    6

    Default

    Quote Originally Posted by michael.lange View Post
    well you could wrap the MultiResourceItemReader and force it to give the current.resource
    That did exactly what I want! Many thanks!

    "We should move forwards, not backwards, upwards, not sideways, and always twirling, twirling towards victory!"

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
  •