第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

將 Spring Batch Tasklet 失敗消息傳遞給報(bào)告步驟。

將 Spring Batch Tasklet 失敗消息傳遞給報(bào)告步驟。

www說 2021-09-29 13:09:07
我正在使用帶有 OpenCSV 的 Spring Batch Tasklet 來讀取我的 CSV 文件。在問這個(gè)問題之前,我知道塊,但是在后面的步驟中文件之間存在交叉驗(yàn)證,所以我必須繼續(xù)使用Tasklet。我要做的是向我的報(bào)告步驟報(bào)告丟失的文件或解析錯(cuò)誤。我不確定向下一步報(bào)告失敗的正確方法應(yīng)該是什么。我有以下代碼。讀取文件的初始步驟。public class CsvBatchReader<T> implements Tasklet, StepExecutionListener {    private final Logger logger = LoggerFactory.getLogger(CsvBatchReader.class);    private List batch;    private final Class<T> clazz;    private Path path;    public CsvBatchReader(Class<T> clazz, Path path) {        this.clazz = clazz;        this.path = path;    }    @Override    public void beforeStep(StepExecution stepExecution) {        logger.info("Reader initialized - " + clazz.getSimpleName());        batch = new ArrayList();    }    @Override    public ExitStatus afterStep(StepExecution stepExecution) {        logger.info("Reader ended - " + clazz.getSimpleName());        return ExitStatus.COMPLETED;    }    @Override    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws UnexpectedJobExecutionException {        logger.info("Reader execute - " + clazz.getSimpleName());        ICsvToBean csvToBean = new CsvToBean(clazz, path);        try {            batch = csvToBean.readCsv();        } catch(IOException ex) {            // error message being caught from my csvToBean class.             throw new UnexpectedJobExecutionException("Invalid file " + ex.getMessage());        }        return RepeatStatus.FINISHED;    }}報(bào)告步驟我不確定如何傳入異常消息,或者是否定義了在不使用 Step Execution Context 的情況下傳入失敗消息的方法。public class CsvBatchReporting implements Tasklet, StepExecutionListener {    private final Logger logger = LoggerFactory.getLogger(CsvBatchCrossValidation.class);    private List errorMessages;    private List skippedInserts;
查看完整描述

3 回答

?
人到中年有點(diǎn)甜

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊

我不確定如何傳入異常消息,或者是否定義了在不使用 Step Execution Context 的情況下傳入失敗消息的方法。


您可以從作業(yè)執(zhí)行中訪問上一步中拋出的異常。下面是一個(gè)例子:


import java.util.List;


import org.springframework.batch.core.Job;

import org.springframework.batch.core.JobExecution;

import org.springframework.batch.core.JobParameters;

import org.springframework.batch.core.Step;

import org.springframework.batch.core.StepExecution;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;

import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;

import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;

import org.springframework.batch.core.launch.JobLauncher;

import org.springframework.batch.repeat.RepeatStatus;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;


@Configuration

@EnableBatchProcessing

public class MyJob {


    @Autowired

    private JobBuilderFactory jobs;


    @Autowired

    private StepBuilderFactory steps;


    @Bean

    public Step step1() {

        return steps.get("step1")

                .tasklet((contribution, chunkContext) -> {

                    System.out.println("hello");

                    throw new Exception("Boom!");

                })

                .build();

    }


    @Bean

    public Step step2() {

        return steps.get("step2")

                .tasklet((contribution, chunkContext) -> {

                    JobExecution jobExecution = chunkContext.getStepContext().getStepExecution().getJobExecution();

                    StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); // TODO properly get the stepExecution of the previous step

                    List<Throwable> failureExceptions = stepExecution.getFailureExceptions();

                    if (!failureExceptions.isEmpty()) {

                        Throwable throwable = failureExceptions.get(0);

                        System.out.println("Looks like step1 has thrown an exception: " + throwable.getMessage());

                    }

                    System.out.println("world");

                    return RepeatStatus.FINISHED;

                })

                .build();

    }


    @Bean

    public Job job() {

        return jobs.get("job")

                    .flow(step1())

                    .on("*").to(step2())

                    .build()

                .build();

    }


    public static void main(String[] args) throws Exception {

        ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class);

        JobLauncher jobLauncher = context.getBean(JobLauncher.class);

        Job job = context.getBean(Job.class);

        jobLauncher.run(job, new JobParameters());

    }


}

此示例打?。?/p>


hello

Looks like step1 has thrown an exception: Boom!

world

顯然,您需要確保在所有情況下 step1 都流向 step2(因此是流定義)。


希望這可以幫助。


查看完整回答
反對(duì) 回復(fù) 2021-09-29
  • 3 回答
  • 0 關(guān)注
  • 232 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)