Announcement Announcement Module
Collapse
No announcement yet.
Accessing the currently processing filename Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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.

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

    Comment


    • #3
      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.

      Comment


      • #4
        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, 03:45 AM.

        Comment


        • #5
          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!

          Comment

          Working...
          X