相关疑难解决方法(0)

在Spring中的步骤之间传递信息?

我正在尝试制作一个Spring Batch而我没有经验.

是否可以从每个批处理步骤传递信息,或者它们是否必须完全独立?

例如,如果我有

   <batch:step id="getSQLs" next="runSQLs">
        <batch:tasklet transaction-manager="TransactionManager"
            ref="runGetSQLs" />
    </batch:step>

    <batch:step id="runSQLs">
        <batch:tasklet transaction-manager="TransactionManager"
            ref="runRunSQLs" />
    </batch:step>
Run Code Online (Sandbox Code Playgroud)

并且getSQLs触发一个bean,该bean执行一个类,该类生成一个String类型的List.是否可以引用runSQLs触发的bean列表?("触发"可能不是正确的术语,但我想你知道我的意思)

更新:所以getSQLs步骤触发这个bean:

<bean id="runGetSQLs" class="myTask"
    scope="step">
    <property name="filePath" value="C:\Users\username\Desktop\sample.txt" />
</bean>
Run Code Online (Sandbox Code Playgroud)

它触发执行此方法的myTask类:

  @Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

    ExecutionContext stepContext = this.stepExecution.getExecutionContext();
    stepContext.put("theListKey", sourceQueries);

    return RepeatStatus.FINISHED;
}
Run Code Online (Sandbox Code Playgroud)

我是否需要以某种方式将stepExecution传递给execute方法?

java spring spring-batch

9
推荐指数
2
解决办法
2万
查看次数

我们可以在 Spring Batch 的 Tasklet 中使用 @Autowired 吗?

我的应用程序中有一个 Spring Batch tasklet,如下所示。

@Service
public class SampleTasklet implements Tasklet {
    
    @Autowired
    private UserService userService;

    @Override
    public RepeatStatus execute(StepContribution contribution,
                                ChunkContext chunkContext) throws Exception {

        System.err.println(userService.getUsers().size());
        return RepeatStatus.FINISHED;
    }

}
Run Code Online (Sandbox Code Playgroud)

我有一个服务类如下。

@Service
@Slf4j
public class UserService {
    public Map<String, String> getUsers(){
        return null
    }

}
Run Code Online (Sandbox Code Playgroud)

Spring Boot 类:

@SpringBootApplication
@Slf4j
public class SampleBatchApp {

    public static void main(String[] args) {
        log.info("Custom DAM Batch Application starting");
        SpringApplication.run(SampleBatchApp.class, args);
    }

}
Run Code Online (Sandbox Code Playgroud)

Spring Batch File:——根据评论编辑。

@Configuration
@EnableBatchProcessing
public class SampleBatch {

    @Autowired
    public …
Run Code Online (Sandbox Code Playgroud)

java spring spring-batch

9
推荐指数
1
解决办法
6891
查看次数

将数据传递给未来的步骤 - Spring Batch

正如我们在 Spring Batch 官方文档中看到的那样,“将数据传递到未来的步骤 - Spring Batch”是可能的,但我们大多数人一直在努力解决它,因为在官方文档中他们提到了两种可能性。一个台阶级别,一个工作级别。问题是如何检索步骤级别的数据?

我的解决方案与官方文档中的相关解决方案相同,但它不起作用。所以我决定做以下事情:

1-为促销监听器创建bean:

<beans:bean id="promotionListener"
                class="org.springframework.batch.core.listener.ExecutionContextPromotionListener">
        <beans:property name="keys" value="someValues"/>
</beans:bean>
Run Code Online (Sandbox Code Playgroud)

2-在您的步骤中设置监听器(您希望保存数据的步骤)

        <listeners>
            <listener ref="promotionListener"/>
        </listeners>
Run Code Online (Sandbox Code Playgroud)

3- 在写入器中的同一步骤(将保存数据的步骤)中,保存数据。

private StepExecution stepExecution;

    @BeforeStep
    public void saveStepExecution(StepExecution stepExecution) {
        this.stepExecution = stepExecution;

        ExecutionContext executionContext = stepExecution.getExecutionContext();
        Map<String, String> fieldsMap= new HashMap<>();
        executionContext.put("keys", someValues);
    }

@Override
public void write(List<? extends Map<String, String>> items) throws Exception {
    LOGGER.info(items.toString());

    Map<String, String> fieldsMap= new ConcurrentHashMap<>();
    items.forEach(item -> item.forEach(fieldsMap::put));

    ExecutionContext stepContext = this.stepExecution.getExecutionContext();
    stepContext.put("keys", fieldsMap);
}
Run Code Online (Sandbox Code Playgroud)

您可以看到,在我的例子中,我将数据保存在 Map …

spring batch-processing spring-batch

7
推荐指数
1
解决办法
1万
查看次数

Spring批处理聚合值并写入单个值

我正在使用 spring 批处理,我需要实现以下目标

  1. 读取包含日期和金额等详细信息的 csv 文件
  2. 汇总同一日期所有金额的总和
  3. 保留一个带有日期和总和的条目

我过去使用过批处理,我想到了以下方法。用 2 个步骤创建一个批次。

第1步:

  1. Reader:使用 FlatFileItemReader 遍历整个文件
  2. 处理器:使用键作为日期和值作为数量填充地图。如果存在条目,则获取该值并将其添加到新值中
  3. 作家:没有操作作家,因为我不想写

第2步:

  1. 阅读器:遍历地图的值
  2. 作家:坚持价值观

我能够实现我填充Map. 这Map已声明为@JobScope

我被困在如何为 step2 创建读取器,它只需要读取值列表。我试过了,ListItemReader但我无法MapListItemReader.

请提出解决方案,或者您是否有更好的方法来解决这个问题

谢谢

java spring spring-batch

6
推荐指数
1
解决办法
4995
查看次数

Spring Batch:在读取器和写入器之间传递数据

我想在步骤中的读取器中设置的写入器中获取数据。我通过http://docs.spring.io/spring-batch/trunk/reference/html/patterns.html#passingDataToFutureSteps了解ExecutionContexts(步骤和工作)以及ExecutionContextPromotionListener

问题是在Writer中,我正在检索'npag'的空值。

ItemWriter上的行:

LOG.info("INSIDE WRITE, NPAG: " + nPag);
Run Code Online (Sandbox Code Playgroud)

我正在做一些变通办法,没有运气,正在寻找其他类似问题的答案……有什么帮助吗?谢谢!

这是我的代码:

读者

@Component
public class LCItemReader implements ItemReader<String> {

private StepExecution stepExecution;

private int nPag = 1;

@Override
public String read() throws CustomItemReaderException {

    ExecutionContext stepContext = this.stepExecution.getExecutionContext();
    stepContext.put("npag", nPag);
    nPag++;
    return "content";
}

@BeforeStep
public void saveStepExecution(StepExecution stepExecution) {
    this.stepExecution = stepExecution;
}
}
Run Code Online (Sandbox Code Playgroud)

作家

@Component
@StepScope
public class LCItemWriter implements ItemWriter<String> {

private String nPag;

@Override
public void write(List<? extends String> continguts) throws Exception …
Run Code Online (Sandbox Code Playgroud)

spring spring-batch spring-boot

3
推荐指数
1
解决办法
2万
查看次数