Spring Batch 2.0: Repeat a tasklet

In some scenarios, such as downloading by FTP the same file from different servers, following tasklet repeat pattern can be useful.

Job definition

< ?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:batch="http://www.springframework.org/schema/batch"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/batch
    http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Repeat job -->
    <batch_job id="repeatJob" job-repository="jobRepository">
        <batch_step id="repeatTasklet">
            <batch_tasklet ref="taskletRepeat" />
        </batch_step>
    </batch_job>

    <!-- Repeat tasklet definition -->
    <bean id="taskletRepeat"
        class="foo.tasklet.RepeatTasklet">
<property name="repeatPolicy" ref="repeatPolicy" />
    </bean>

    <bean id="repeatPolicy"
        class="foo.policy.RepeatPolicy">
<property name="elements">
	<list>
                <value>element1</value>
                <value>element2</value>
                <value>element3</value>
            </list>
        </property>
    </bean>

    <!-- AOP repeat configuration -->
    <aop_config>
        <aop_pointcut id="transactional"
            expression="execution(* foo..RepeatTasklet.execute(..))" />
        <aop_advisor pointcut-ref="transactional"
            advice-ref="repeatAdvice"
            order="-1"/>
    </aop_config>

    <bean id="repeatAdvice"
        class="org.springframework.batch.repeat.interceptor.RepeatOperationsInterceptor">
<property name="repeatOperations">
          <bean class="org.springframework.batch.repeat.support.RepeatTemplate">
<property name="completionPolicy" ref="repeatPolicy" />
          </bean>
      </property>
    </bean>

</beans>

Repeat Tasklet

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

import es.eds.springbatch.test.repeat.policy.RepeatPolicy;

public class RepeatTasklet implements Tasklet {

  private RepeatPolicy repeatPolicy;

  public RepeatStatus execute(
      StepContribution stepContribution,
      ChunkContext chunkContext)
      throws Exception {

    System.out.println("Current element: " + repeatPolicy.getCurrentElement());
    return RepeatStatus.FINISHED;

  }

  public RepeatPolicy getRepeatPolicy() {
    return repeatPolicy;
  }

  public void setRepeatPolicy(RepeatPolicy repeatPolicy) {
    this.repeatPolicy = repeatPolicy;
  }

}

Repeat Policy

import java.util.List;
import org.springframework.batch.repeat.CompletionPolicy;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.RepeatStatus;

public class RepeatPolicy implements CompletionPolicy {

  private List<string> elements;
  private String currentElement;

  public boolean isComplete(RepeatContext repeatContext) {
    return elements.size() == 0;
  }

  public boolean isComplete(RepeatContext repeatContext,
      RepeatStatus repeatStatus) {
    return elements.size() == 0;
  }

  public RepeatContext start(RepeatContext repeatContext) {
    return repeatContext;
  }

  public void update(RepeatContext repeatContext) {
    currentElement = elements.remove(0);
    }

  public List</string><string> getElements() {
    return elements;
  }

  public void setElements(List</string><string> elements) {
    this.elements = elements;
  }

  public String getCurrentElement() {
    return currentElement;
  }

}

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s