Page 2 of 2 FirstFirst 12
Results 11 to 15 of 15

Thread: How to pass data from one step to next step?

  1. #11
    Join Date
    Nov 2009
    Posts
    5

    Default

    Thanks for replying.

    I know its sound weird but I tried to make this work for 4 hours but suddenly now its started working after adding 'strict' property to the promotionlistener for sometime. then again stopped working.

    Here is my code.

    In the first step of my job I am loading the values in the context.

    Code:
    public class MyMultiResourceItemReader extends MultiResourceItemReader {
    
    .
    .
    .	
    	@Override
    	public void open(ExecutionContext executionContext) throws ItemStreamException {
    
    	  super.open(executionContext);
    
    	  try {
    		  if (getCurrentResource()!=null) 
    		  {
    			  executionContext.put("current.resource.dir.path", getCurrentResource().getFile().getParentFile());
    			  executionContext.put("current.resource.name", getCurrentResource().getFilename());
    		  }
    		  else
    		  {
    			  executionContext.put("current.resource.dir.path", null);
    			  executionContext.put("current.resource.name", null);
    		  }
    	  } catch (Exception e) {
    	    logger.error(e.getMessage(), e);
    	  }
    	}
    	
    	  @Override
    	  public void update(ExecutionContext executionContext) throws ItemStreamException {
    	 
    	    super.update(executionContext);
    	    
    	 
    	    try {
    	 
    	      if (getCurrentResource()!=null) {
    			  executionContext.put("current.resource.dir.path", getCurrentResource().getFile().getParentFile());
    	          executionContext.put("current.resource.name", getCurrentResource().getFilename());
    	      } else {
    	    	  executionContext.put("current.resource.dir.path", null);
    	          executionContext.put("current.resource.name", null);
    	      }
    	 
    	    } catch (Exception e) {
    	    	logger.error(e.getMessage(), e);
    	    }
    	 }
    }
    In the second step , am trying to get it from jobcontext

    Code:
    public class ProcessorTasklet implements Tasklet {
    	
    	
    	private String fileName = null;
    	
    
    public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
    			throws Exception {
    		
    		StepContext stepContext = arg1.getStepContext();
    		StepExecution stepExecution = stepContext.getStepExecution();
    	    JobExecution jobExecution = stepExecution.getJobExecution();
    		ExecutionContext jobContext = jobExecution.getExecutionContext();
            this.fileName = (String) jobContext.get("current.resource.name");
    	.
    	.
    	.
    		return null;
    	}
    
        @BeforeStep
        public void getData(StepExecution stepExecution) {
            JobExecution jobExecution = stepExecution.getJobExecution();
            ExecutionContext jobContext = jobExecution.getExecutionContext();
            this.fileName = (String) jobContext.get("current.resource.name");
        }
    }
    this is my step 3,

    Code:
    public class FileMoveTasklet implements Tasklet {
    
    	@Override
    	public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    	
    		File dirParent;
    		
    		StepContext stepContext = chunkContext.getStepContext();
    		StepExecution stepExecution = stepContext.getStepExecution();
    	       JobExecution jobExecution = stepExecution.getJobExecution();
    		ExecutionContext jobContext = jobExecution.getExecutionContext();
    	        dirParent = (File) jobContext.get("current.resource.dir.path");
    		String fileNem = (String) jobContext.get("current.resource.name");
    
    .
    .
    .
    
    		return RepeatStatus.FINISHED;
    	}
    
    }
    Code:
    	<bean id="promotionListener" class="org.springframework.batch.core.listener.ExecutionContextPromotionListener" scope="step">
        	<property name="keys" >
        	 <list>
                 <value>current.resource.name</value>
    			 <value>current.resource.dir.path</value>
            </list>
        	</property>
        	<property name="strict" value="true"/>
    	</bean>
    Last edited by forgetarun; Dec 5th, 2009 at 04:55 AM.

  2. #12
    Join Date
    Feb 2008
    Posts
    488

    Default

    1) First, this is never a good idea to have in your code. You should remove it so that errors aren't swallowed.
    Code:
    } catch (Exception e) {
         logger.error(e.getMessage(), e);
    }
    2) I agree that it doesn't make sense that 'strict=true' would change the behavior since all this flag does is indicate whether or not to throw an exception.

    3) In the step 2 code, I'm not sure why you're getting the filename in execute() AND the @BeforeStep. You should remove that code from execute() and just use the @BeforeStep to retrieve values from the context. Do the same for step 3: create a @BeforeStep and remove the code from execute().

    4) The xml seems ok, but you haven't included anything about registration. Have you registered this bean as a listener on step 1? Further, have you registered steps 2 and 3 as listeners to make sure that the @BeforeStep is called?

    5) Finally, are you sure that step 1 is completing with status COMPLETED?

  3. #13
    Join Date
    Nov 2009
    Posts
    5

    Default

    thanks again.
    I will correct the code as suggested. I hav verified the step 1 is completed properly.
    am new to spring batch , can you tell me on how do I register a bean as listener?

  4. #14
    Join Date
    Feb 2008
    Posts
    488

    Default

    You use the <listener/> tag, as shown in the section on listeners (http://static.springsource.org/sprin...gStepExecution) and the section on passing data to future steps (http://static.springsource.org/sprin...aToFutureSteps).

  5. #15
    Join Date
    Nov 2009
    Posts
    5

    Default

    thank you. I could nt get the listener thing but its working now.

Posting Permissions

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