Announcement Announcement Module
No announcement yet.
Need to assert that chunk commit-interval is 1 Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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?


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


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



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

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

        Maybe, you can implement the StepListener as follows.

        public class CheckCommitIntervalStepListener extends StepExecutionListenerSupport {
        	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'.");


        • #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 ' icy.SimpleCompletionPolicy#0': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
          at torResolver.autowireConstructor(ConstructorResolve
          at AutowireCapableBeanFactory.autowireConstructor(Abs
          at AutowireCapableBeanFactory.createBeanInstance(Abst
          at AutowireCapableBeanFactory.doCreateBean(AbstractAu
          at AutowireCapableBeanFactory.createBean(AbstractAuto
          at BeanFactory$2.getObject( 29)
          at org.springframework.batch.core.scope.StepScope.get (
          at BeanFactory.doGetBean( )
          at BeanFactory.getBean(
          at rce.getTarget(
          at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(
          at $Proxy40.start(Unknown Source)
          at plate.start(
          at plate.executeInternal(
          at plate.iterate(
          at org.springframework.batch.core.step.item.SimpleChu nkProvider.provide(
          at org.springframework.batch.core.step.item.ChunkOrie ntedTasklet.execute(
          at org.springframework.batch.core.step.tasklet.Taskle tStep$ChunkTransactionCallback.doInTransaction(Tas
          at nTemplate.execute(
          at org.springframework.batch.core.step.tasklet.Taskle tStep$2.doInChunkContext(
          at org.springframework.batch.core.scope.context.StepC ontextRepeatCallback.doInIteration(StepContextRepe
          at plate.getNextResult(
          at plate.executeInternal(
          at plate.iterate(
          at org.springframework.batch.core.step.tasklet.Taskle tStep.doExecute(
          at org.springframework.batch.core.step.AbstractStep.e xecute(
          at org.springframework.batch.core.job.SimpleStepHandl er.handleStep(
          at org.springframework.batch.core.job.flow.JobFlowExe cutor.executeStep(
          at ate.StepState.handle(
          at mpleFlow.resume(
          at mpleFlow.start(
          at Execute(
          at org.springframework.batch.core.job.AbstractJob.exe cute(
          at leJobLauncher$

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

          public SimpleCompletionPolicy(int chunkSize) {
          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,



          • #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:
                <job id="BGD1001F" xmlns="">
                    <step id="FTFGuide1-01">
                        <tasklet transaction-manager="transactionManager">
                            <chunk commit-interval="#{jobParameters['commit.interval']}" reader="BGD1001R01" processor="BGD1001P01" writer="BGD1001W01"/>
                                <listener ref="checkDateStepListener"/>
                <bean id="checkDateStepListener" class="sample.batch.gd1001.listener.CheckDateStepListener"/>
            * When executing Testcase, 'commit.interval' parameter is created like this:
                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 =, 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.



            • #7
              Hi Chori,

              Thank you very much for your reply.

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