我创建了fileAllocationTasklet,其目的是将文件从一个路径移动到另一个路径。此文件以fromPath和To Path的形式输入。所以我试图在创建新对象的两个步骤中使用相同的tasklet。我知道tasklet只运行一次。
@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;
}
但是,tasklet仅在第1步中运行,但未在第3步中运行。我这样做是为了使代码中没有冗余。如果有其他最好的方法那么它将是可观的。
实际上我的答案是使用了@StepScope,这意味着对象每个步骤都是唯一的,但不是单例。每次执行时,它都将形成tasklet的新对象,由于@Bean,该对象通常不会形成。 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
答案 0 :(得分:1)
我不能悄悄地确定你想要达到的目标。我在这里放置代码以确保我们在同一页面上理解。抱歉,我没有Annotation的示例项目,所以这里是XML配置。
<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 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>
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());
}
}
请查看并告诉我您想要达到的目标。
谢谢,