Apache camel Camel 2.21.0-如何处理流式处理异常

Apache camel Camel 2.21.0-如何处理流式处理异常,apache-camel,spring-camel,Apache Camel,Spring Camel,我想在异常中记录一个错误,然后在下一个记录/拆分中继续,但它不起作用 我厌倦了onexceptan(),doTry()DSL,但它不工作,转到ErrorHandler onException(IOException.class) .handled(true).process(exchange -> log.error("error!!")); from("file:" + rootDir + "/" + account + "/inbox/?move=.done") .unmarshal

我想在异常中记录一个错误,然后在下一个记录/拆分中继续,但它不起作用

我厌倦了
onexceptan()
doTry()
DSL,但它不工作,转到ErrorHandler

onException(IOException.class)
.handled(true).process(exchange -> log.error("error!!"));

from("file:" + rootDir + "/" + account + "/inbox/?move=.done")
.unmarshal(csvDataFormat)
.split(body()).shareUnitOfWork().parallelProcessing().streaming()
.process(fileService)
.end()
日志:


@贝德拉,谢谢你的意见,我发现这对我的用例有用

  • 仍在使用
    onException()
    将exchange发送到
    死信频道
    ,因此必须使用
    doTry()
  • CasvFormat
    使用
    映射
    -我无法在
    过程
    中修改
    csvFormat
    ,因此必须 使用
    setBody
完整路线定义:

CsvDataFormat csvDataFormat = new CsvDataFormat().setUseMaps(true);

from("file:" + rootDir + "/test/")
                .log(LoggingLevel.INFO,"Start processing ${file:name}")
                .unmarshal().pgp(pgpFileName,pgpUserId,pgpPassword)
                .process(exchange -> { /* just to get csv header */
                    InputStream inputStream = exchange.getIn().getBody(InputStream.class);
                    try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))){
                        String header = bufferedReader.readLine();
                        exchange.getIn().setHeader("CSV_HEADER",header);
                        csvDataFormat.setHeader(header.split(",")); //<- this does not work, so had to add in body below!
                        System.out.println("csvHeader is : " + header);// + " ? " + Arrays.asList(csvDataFormat.getHeader()));
                    }
                })
                .split(body().tokenize("\n")).shareUnitOfWork()
                .parallelProcessing().streaming()
                .setBody(exchange -> exchange.getIn().getHeader("CSV_HEADER") + "\n" + exchange.getIn().getBody())
                .doTry()
                  .unmarshal(csvDataFormat)
                  .process(requestFileService)
                .doCatch(IOException.class)
                  //TODO: custom processing here...
                  .process(exchange -> log.error("caught in dotry: " + exchange.getIn().getBody())).stop()
                .end()//end try/catch
                .choice()
                    .when(simple("${property." + Exchange.SPLIT_COMPLETE + "} == true"))
                    .log(LoggingLevel.INFO, "Finished processing ${file:name}")
                .end();
CsvDataFormat CsvDataFormat=new CsvDataFormat().setUseMaps(true);
来自(“文件:“+rootDir+”/test/”)
.log(LoggingLevel.INFO,“开始处理${file:name}”)
.unmarshal().pgp(pgpFileName、pgpUserId、pgpPassword)
.process(exchange->{/*仅用于获取csv标头*/
InputStream InputStream=exchange.getIn().getBody(InputStream.class);
try(BufferedReader BufferedReader=new BufferedReader(new InputStreamReader(inputStream))){
字符串头=bufferedReader.readLine();
exchange.getIn().setHeader(“CSV_头”,头);
csvDataFormat.setHeader(header.split(“,”);//exchange.getIn().getHeader(“CSV_header”)+“\n”+exchange.getIn().getBody())
多特里先生()
.unmarshal(csvDataFormat)
.process(requestFileService)
.doCatch(IOException.class)
//TODO:此处自定义处理。。。
.process(exchange->log.error(“在dotry中捕获:+exchange.getIn().getBody())).stop()
.end()//结束try/catch
.choice()
.when(简单(“${property.+Exchange.SPLIT_COMPLETE+”}==true”))
.log(LoggingLevel.INFO,“已完成处理${file:name}”)
.end();

是在
fileService
中抛出的
IOException
,还是在
file2
组件中抛出的?如果要捕获从
file2
组件抛出的异常,需要将URI属性
consumer.bridgeErrorHandler
设置为true
IOException
是在
中抛出的。unmarshal(csvDataFormat)
-当记录未被无效添加日志时-似乎异常是在并行处理中引发的我用一些无效的CSV尝试过它,并且在Camel 2.21.0中没有直接引发
IOException
,它是在
org.apache.commons.CSV.CSVParser\getNextRecord
中用
RuntimeException
包装的(RuntimeException.class)
work?我试过了,它不会继续处理下一个rec(拆分):
OneException(RuntimeException.class).log(“error!!!”).handled(true);
CsvDataFormat csvDataFormat = new CsvDataFormat().setUseMaps(true);

from("file:" + rootDir + "/test/")
                .log(LoggingLevel.INFO,"Start processing ${file:name}")
                .unmarshal().pgp(pgpFileName,pgpUserId,pgpPassword)
                .process(exchange -> { /* just to get csv header */
                    InputStream inputStream = exchange.getIn().getBody(InputStream.class);
                    try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))){
                        String header = bufferedReader.readLine();
                        exchange.getIn().setHeader("CSV_HEADER",header);
                        csvDataFormat.setHeader(header.split(",")); //<- this does not work, so had to add in body below!
                        System.out.println("csvHeader is : " + header);// + " ? " + Arrays.asList(csvDataFormat.getHeader()));
                    }
                })
                .split(body().tokenize("\n")).shareUnitOfWork()
                .parallelProcessing().streaming()
                .setBody(exchange -> exchange.getIn().getHeader("CSV_HEADER") + "\n" + exchange.getIn().getBody())
                .doTry()
                  .unmarshal(csvDataFormat)
                  .process(requestFileService)
                .doCatch(IOException.class)
                  //TODO: custom processing here...
                  .process(exchange -> log.error("caught in dotry: " + exchange.getIn().getBody())).stop()
                .end()//end try/catch
                .choice()
                    .when(simple("${property." + Exchange.SPLIT_COMPLETE + "} == true"))
                    .log(LoggingLevel.INFO, "Finished processing ${file:name}")
                .end();