0

I have created the fileAllocationTasklet whose purpose is to move file from one path to other. This file take input as fromPath and To Path. So I am trying to use this same tasklet in two step with making new object. I know the tasklet run only one time.

@Bean
    public Step step3() {
        System.out.println("******step3 executing ");
        return stepBuilderFactory.get("step3")
                .tasklet(fileAllocationTasklet("process/data.csv","output/data.csv")).build();
    }

 @Bean
    public Step step1(JdbcBatchItemWriter<TxnDetail> writer) {
        return stepBuilderFactory.get("step1")
                .tasklet(fileAllocationTasklet("initial/data.csv","process/data.csv")).build();
    }

@Bean
    public Tasklet fileAllocationTasklet(String fromPath,String toPath) {
        FileAllocationTasklet fileAllocation = new FileAllocationTasklet();
        fileAllocation.setFromPath(fromPath);
        fileAllocation.setToPath(toPath);
        return fileAllocation;
    }

But the tasklet is running only first time in step1 but not running in step 3. I made this so that i don't have redundancy in code. If there is other best approach then it will be appreciable.

Actually the answer of this I have used @StepScope which means the object will be unique for each step but not singleton. Each time step execute then it will form new object of tasklet which was prevously not forming due to @Bean. The explanation is also available in https://stackoverflow.com/questions/38780796/how-does-spring-batch-step-scope-work?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

dj Bravo
  • 198
  • 1
  • 3
  • 18

1 Answers1

1

I'm not quietly sure what you are trying to achieve. I'm putting here code to make sure we are understanding on the same page. And sorry I don't have an example project for Annotation, so here XML configuration.

Job configuration

<job id="exampleJobTask" xmlns="http://www.springframework.org/schema/batch">
        <step id="stepOne" next="stepTwo">
            <tasklet ref="performTaskTaskOne"/>
        </step>

        <step id="stepTwo">
            <tasklet ref="performTaskTaskTwo"/>
        </step>
    </job>

Bean configuration

<bean id="performTaskTaskOne" class="com.itservicesdepot.example.springbatch.tasklet.PerformTask" scope="step">
        <property name="from" value="initial/data.csv" />
        <property name="to" value="process/data.csv" />
    </bean> 

    <bean id="performTaskTaskTwo" class="com.itservicesdepot.example.springbatch.tasklet.PerformTask" scope="step">
        <property name="from" value="process/data.csv" />
        <property name="to" value="output/data.csv" />
    </bean>

Test class

package com.itservicesdepot.example.springbatch;

import java.util.Date;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.StopWatch;

import junit.framework.Assert;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:example-jobs.xml"})
public class ShowCaseTest {

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    @Qualifier(value = "exampleJobTask")
    private Job exampleJobTask;

    @Test
    public void exampleJobTask() throws Exception {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        JobExecution jobExecution = jobLauncher.run(this.exampleJobTask,
                new JobParametersBuilder()
                        .addDate("now", new Date()).toJobParameters());

        stopWatch.stop();

        Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
    }
}

Please review and let me know what you want to achieve.

Thanks,

Nghia Do
  • 2,588
  • 2
  • 17
  • 31