当前位置: 首页 > 知识库问答 >
问题:

我们能在spring批处理的两个步骤中使用不同的对象执行相同的tasklet吗

尉迟德惠
2023-03-14

我创建了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的新对象,而tasklet之前由于@Bean而没有形成。该解释也可在https://stackoverflow.com/questions/38780796/how-does-spring-batch-step-scope-work?utm_medium=organic


共有1个答案

尉迟冯浩
2023-03-14

我不确定你想要实现什么。我把代码放在这里是为了确保我们在同一页上理解。很抱歉,我没有用于注释的示例项目,所以这里是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());
    }
}

请回顾并让我知道你想要实现什么。

谢谢

 类似资料:
  • 第2步。根据在步骤1中创建的对象列表中有多少项创建步骤列表。 第三步。尝试执行步骤2中创建的步骤列表中的步骤。 下面在executeDynamicStepsTasklet()中执行x个步骤。虽然代码运行时没有任何错误,但它似乎没有做任何事情。我在那个方法中的内容看起来正确吗?

  • Spring批处理-需要帮助以并行和多个节点运行批处理作业的独立步骤。一个spring批处理作业(JobA),包含三个步骤[步骤A(在compute1中)和步骤B(在compute2中)以及步骤C] StepA和StepB是独立的步骤,占用大量内存,因此不能在同一计算节点/JVM上并行运行。要使StepC同时启动(StepA和StepB),需要成功完成。我不想为了节省时间而依次执行步骤A和步骤B。

  • 使用Spring Batch 3.0.4.Release。 我将作业配置为使用分区步骤。从机步骤使用块大小1。任务执行器中有六个线程。我使用从六到数百的各种网格大小来运行这个测试。我的网格大小是从StepExecutions的数量,我希望==我的分区器创建的ExecutionContexts的数量。 下面是Java配置代码:

  • 我正在做一个项目,我将使用Spring批处理和Spring集成来创建工作流系统。这个工作流系统应该能够从队列中读取消息,这实际上是来自客户端的作业请求,根据作业请求类型,我需要调用一些7-8系统。 每个系统从某个位置读取输入文件(通常是一个集中存储系统,其中所有输入文件都存储在客户提交的文件中),对其进行处理,然后将其传递给下一个系统,最终我应该能够响应客户端,例如SUCCESS如果它被所有系统成

  • 我正在尝试修复Spring Batch中的一个问题,这个问题最近一直困扰着我们的系统。我们有一份工作,在大多数情况下都很好。下载和处理数据是一个多步骤的工作。 问题是有时工作会爆棚。也许我们试图连接到的服务器抛出了错误,或者我们在工作进行到一半时关闭了服务器。此时,下次我们的quartz调度程序尝试运行该作业时,它似乎什么也不做。以下是此作业定义的删节版本: 委婉地说,我是Spring Batch

  • 我配置了一个spring批处理作业,它在spring WebService中运行。这项工作有几个步骤。我已经在不同的tomcats中部署了这个webservice的两个实例(但两个实例都使用相同的mysql数据库)。 我希望用不同的参数在两个tomcats中同时运行spring批处理作业(每个tomcats中一个)。我没有使用分区,每个作业的参数是完全不同的。 我开始工作在一个汤姆猫和一切看起来很