Announcement Announcement Module
Collapse
No announcement yet.
Need to assert that chunk commit-interval is 1 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need to assert that chunk commit-interval is 1

    Hi all,

    I need to assert as early as possible at runtime that the commit-interval of a chunk is equal to one. I'm thinking that the best place to test for that is in StepExecutionListener#beforeStep(StepExecution).

    In StepExecutionListener, I don't see a path from the StepExecution to a method that returns to me the commit-interval configured in the chunk in the configuration.

    How do I get the commit-interval before any actual work is done in a step?

    Thanks,
    Matthew

  • #2
    If you use Spring Batch of '2.1.7' version, you can easily check the commit-interval that is set as a JobParameter using Late binding in beforeStep() method of your StepExecutionListener or JobParametersValidator.

    In lower version of Spring Batch, there is no way to check that if the commit-interval is set as 'commit-interval' property on 'chunk' tag.
    If you use StepExecutionSimpleCompletionPolicy, it is also possible to assert the commit-interval because this way sets commit interval as a JobParameter as well. However, if you want to check the commit interval in StepExecutionListener, you should set the higher order of the CompletionPolicy than your StepExecutionListener in order to perform ChunkCompletionPolicy earlier then checking StepListener.
    Last edited by chori; May 22nd, 2011, 10:27 AM.

    Comment


    • #3
      Originally posted by chori View Post
      If you use Spring Batch of '2.1.7' version, you can easily check the commit-interval that is set as a JobParameter
      Are you saying that Spring Batch adds the commit-interval as a JobParameter? That's news to me.

      Can you show me a code snippet of how you get the commit-interval of a chunk at runtime? I'd be very interested to see that.

      Thanks,
      Matthew

      Comment


      • #4
        Hi, Matthew

        Refer to BATCH-1396 on Changelog 2.1.6-2.1.7 and I will explain you how to check the commit interval below.

        You can set commit interval as a JobParameter as follows and also implement and set CheckCommitIntervalStepListener to assert the commit-interval.

        Code:
        	<job id="job01">
        		<step id="step01">
        			<tasklet>
        				<chunk commit-interval="#{jobParameters['step01_commitInterval']}" reader="itemReader" writer="itemWriter"/>
        				<listeners>
        					<listener ref="checkCommitIntervalStepListener"/>
        				</listeners>
        			</tasklet>
        		</step>
        	</job>
        
        	<bean id="checkCommitIntervalStepListener" class="sample.batch.listener.CheckCommitIntervalStepListener"/>
        
                ....

        Maybe, you can implement the StepListener as follows.

        Code:
        public class CheckCommitIntervalStepListener extends StepExecutionListenerSupport {
        
        	@Override
        	public void beforeStep(StepExecution stepExecution) {
        		
        		long commitInterval = stepExecution.getJobParameters().getLong("step01_commitInterval", 1L);
        		if(commitInterval != 1) {
        			throw new RuntimeException("step01_commitInterval should be set only '1'.");
        		}
        	}
        }
        Chori.

        Comment


        • #5
          Hi Chori and Matthew,

          Did the code snippet in the previous post work ?
          I am not able to configure it as you specified.

          I am getting an exception as given below.

          org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'scopedTarget.org.springframework.batch.repeat.pol icy.SimpleCompletionPolicy#0': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
          at org.springframework.beans.factory.support.Construc torResolver.autowireConstructor(ConstructorResolve r.java:250)
          at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireConstructor(Abs tractAutowireCapableBeanFactory.java:1003)
          at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:907)
          at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:485)
          at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
          at org.springframework.beans.factory.support.Abstract BeanFactory$2.getObject(AbstractBeanFactory.java:3 29)
          at org.springframework.batch.core.scope.StepScope.get (StepScope.java:150)
          at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:325 )
          at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:190)
          at org.springframework.aop.target.SimpleBeanTargetSou rce.getTarget(SimpleBeanTargetSource.java:33)
          at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:182)
          at $Proxy40.start(Unknown Source)
          at org.springframework.batch.repeat.support.RepeatTem plate.start(RepeatTemplate.java:461)
          at org.springframework.batch.repeat.support.RepeatTem plate.executeInternal(RepeatTemplate.java:168)
          at org.springframework.batch.repeat.support.RepeatTem plate.iterate(RepeatTemplate.java:143)
          at org.springframework.batch.core.step.item.SimpleChu nkProvider.provide(SimpleChunkProvider.java:103)
          at org.springframework.batch.core.step.item.ChunkOrie ntedTasklet.execute(ChunkOrientedTasklet.java:68)
          at org.springframework.batch.core.step.tasklet.Taskle tStep$ChunkTransactionCallback.doInTransaction(Tas kletStep.java:386)
          at org.springframework.transaction.support.Transactio nTemplate.execute(TransactionTemplate.java:130)
          at org.springframework.batch.core.step.tasklet.Taskle tStep$2.doInChunkContext(TaskletStep.java:264)
          at org.springframework.batch.core.scope.context.StepC ontextRepeatCallback.doInIteration(StepContextRepe atCallback.java:76)
          at org.springframework.batch.repeat.support.RepeatTem plate.getNextResult(RepeatTemplate.java:367)
          at org.springframework.batch.repeat.support.RepeatTem plate.executeInternal(RepeatTemplate.java:214)
          at org.springframework.batch.repeat.support.RepeatTem plate.iterate(RepeatTemplate.java:143)
          at org.springframework.batch.core.step.tasklet.Taskle tStep.doExecute(TaskletStep.java:250)
          at org.springframework.batch.core.step.AbstractStep.e xecute(AbstractStep.java:195)
          at org.springframework.batch.core.job.SimpleStepHandl er.handleStep(SimpleStepHandler.java:135)
          at org.springframework.batch.core.job.flow.JobFlowExe cutor.executeStep(JobFlowExecutor.java:61)
          at org.springframework.batch.core.job.flow.support.st ate.StepState.handle(StepState.java:60)
          at org.springframework.batch.core.job.flow.support.Si mpleFlow.resume(SimpleFlow.java:144)
          at org.springframework.batch.core.job.flow.support.Si mpleFlow.start(SimpleFlow.java:124)
          at org.springframework.batch.core.job.flow.FlowJob.do Execute(FlowJob.java:135)
          at org.springframework.batch.core.job.AbstractJob.exe cute(AbstractJob.java:281)
          at org.springframework.batch.core.launch.support.Simp leJobLauncher$1.run(SimpleJobLauncher.java:120)
          at java.lang.Thread.run(Thread.java:735)

          When setting up the commit-interval, chunk is calling SimpleCompletionPolicy and the following constructor is invoked.

          public SimpleCompletionPolicy(int chunkSize) {
          super();
          this.chunkSize = chunkSize;
          }

          The commit-interval is passed as a long value but the constructor expects an integer value.

          could you please let me know the resolution.

          Many thanks,

          Ajith

          Comment


          • #6
            Hi Ajith,

            As soon as I read this post, I have tested my batch sample, sending 'commit.interval' as a JobParameter, which is based on '2.1.8' version of spring batch.
            My sample works fine. As the result of my test, it is possible to send 'commit.interval' parameter as 'Long' and even 'String' type to the job.

            Here is the code snippet of My sample batch.

            * sample job configuration is like this:
            Code:
                <job id="BGD1001F" xmlns="http://www.springframework.org/schema/batch">
                    <step id="FTFGuide1-01">
                        <tasklet transaction-manager="transactionManager">
                            <chunk commit-interval="#{jobParameters['commit.interval']}" reader="BGD1001R01" processor="BGD1001P01" writer="BGD1001W01"/>
                            <listeners>
                                <listener ref="checkDateStepListener"/>
                            </listeners>
                        </tasklet>
                    </step>
                </job>
            
                <bean id="checkDateStepListener" class="sample.batch.gd1001.listener.CheckDateStepListener"/>
            * When executing Testcase, 'commit.interval' parameter is created like this:
            Code:
                @Test
                public void testLaunchJob() throws Exception {
            
                    JobParametersBuilder builder = new JobParametersBuilder();
            
                    // both are possible.
                    //builder.addLong("commit.interval", 10L);
                    builder.addString("commit.interval", "10");
                    .. set other job parameters...
            
                    JobParameters jobParameters = builder.toJobParameters();
            
                    Job job = jobRegistry.getJob("BGD1001F");
            
                    JobExecution jobExecution = jobLauncher.run(job, jobParameters);
            
                    System.out.println("jobExecution : " + jobExecution);
            
                }

            As you mentioned, SimpleCompletionPolicy has a constructor with int type argument named 'chunkSize'. I guess BeanDefinition of Spring Framework would adjust to bind argument to that constructor when parsing chunk element. I am not sure this part.

            Check your spring batch version and try again.

            Chori.

            Comment


            • #7
              Hi Chori,

              Thank you very much for your reply.

              My code is working now.
              The issue was with setting up the JobParamaters.


              Thanks,

              Ajith

              Comment

              Working...
              X