Camel消耗用base64编码的大文件

时间:2015-01-22 14:27:40

标签: file stream base64 apache-camel

我试图用apache camel读取一个大文件。该文件的内容是base64编码,我想解码它。读取一个基于ASCII的大文本文件没问题,但是这个......

在这种情况下,我无法在一行的末尾拆分它,因为base64行末尾不是我的数据的行尾(数据是csv)。后来我想操纵数据。

使用以下camel路由它可以工作,但是当路由开始时文件必须存在并且没有被锁定,否则我得到IllegalArgumentException。但我不知道该文件何时存在。

编辑:我忘了:骆驼版:2.14.1 | java:1.7

我需要一个解决方案,我可以帮助我吗?

from("stream:file?fileName=/path/to/file&scanStream=true")
                .routeId(ROUTE_ID_BASE64)
                .unmarshal().base64()
                .setHeader("foo", constant("foo"))
                .aggregate(header("foo"), new StringBodyAggregator())
                .completionSize(1000)
                .completionTimeout(1500) 
                .to("file:" + this.tempPfad + "?fileName=" + this.tempDateiname + "&charset=utf-8&fileExist=Append");

我也试过了:

from("file:" + this.eingabePfad + "?filter=#dateinamenFilter&maxDepth=1&readLock=changed&noop=true&charset=utf-8")
                .routeId(ROUTE_ID_BASE64)
                .unmarshal().base64()
                .setHeader("foo", constant("foo"))
                .aggregate(header("foo"), new StringBodyAggregator())
                .completionSize(1000)
                .completionTimeout(1500)
                .to("file:" + this.tempPfad + "?fileName=" + this.tempDateiname + "&charset=utf-8&fileExist=Append");

然后我得到以下错误:

ERROR Failed delivery for (MessageId: ID-smith-52872-1421933301634-0-1 on ExchangeId: ID-smith-52872-1421933301634-0-3). Exhausted after delivery attempt: 2 caught: org.apache.camel.component.file.GenericFileOperationFailedException: Cannot store file: /path/to/temp/tempDatei.csv. Processed by failure processor: FatalFallbackErrorHandler[Channel[DelegateSync[yy.xxxxx.myproject.routes.DateiPolling$1@5734ea]]]

Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
[routeBase64       ] [routeBase64       ] [file:///path/to/?charset=utf-8&filter=%23datein] [      1998]
[routeBase64       ] [unmarshal2        ] [unmarshal[org.apache.camel.model.dataformat.Base64DataFormat@153097e]         ] [         5]
[routeBase64       ] [setHeader1        ] [setHeader[foo]                                                                ] [         0]
[routeBase64       ] [aggregate1        ] [aggregate[header(foo)]                                                        ] [         0]
[routeBase64       ] [to1               ] [file:/path/to/temp/?fileName=tempDatei.csv&char] [        24]
[                  ] [process1          ] [yy.xxxxx.myproject.routes.DateiPolling$1@5734ea                     ] [        15]

Exchange
---------------------------------------------------------------------------------------------------------------------------------------
Exchange[
    Id                  ID-smith-52872-1421933301634-0-3
    ExchangePattern     InOnly
    Headers             {breadcrumbId=ID-smith-52872-1421933301634-0-1, CamelFileAbsolute=true, CamelFileAbsolutePath=/path/to/base64File.txt, CamelFileLastModified=1417777182000, CamelFileLength=267439971, CamelFileName=base64File.txt, CamelFileNameConsumed=base64File.txt, CamelFileNameOnly=base64File.txt, CamelFileParent=/path/to, CamelFilePath=/path/to/base64File.txt, CamelFileRelativePath=base64File.txt, CamelRedelivered=true, CamelRedeliveryCounter=1, CamelRedeliveryMaxCounter=1, foo=foo}
    BodyType            org.apache.commons.codec.binary.Base64InputStream
    Body                [Body is instance of java.io.InputStream]
]

Stacktrace
     ---------------------------------------------------------------------------------------------------------------------------------------
    org.apache.camel.component.file.GenericFileOperationFailedException: Cannot store file: /path/to/temp/tempDatei.csv
        at org.apache.camel.component.file.FileOperations.storeFile(FileOperations.java:276)
        at org.apache.camel.component.file.GenericFileProducer.writeFile(GenericFileProducer.java:277)
        at org.apache.camel.component.file.GenericFileProducer.processExchange(GenericFileProducer.java:165)
        at org.apache.camel.component.file.GenericFileProducer.process(GenericFileProducer.java:79)
        at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
        at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:120)
        at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:416)
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:105)
        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:87)
        at org.apache.camel.processor.aggregate.AggregateProcessor$1.run(AggregateProcessor.java:548)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at org.apache.camel.util.concurrent.SynchronousExecutorService.execute(SynchronousExecutorService.java:62)
        at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:110)
        at org.apache.camel.processor.aggregate.AggregateProcessor.onSubmitCompletion(AggregateProcessor.java:540)
        at org.apache.camel.processor.aggregate.AggregateProcessor.access$900(AggregateProcessor.java:82)
        at org.apache.camel.processor.aggregate.AggregateProcessor$AggregationTimeoutMap.onEviction(AggregateProcessor.java:853)
        at org.apache.camel.processor.aggregate.AggregateProcessor$AggregationTimeoutMap.onEviction(AggregateProcessor.java:814)
        at org.apache.camel.support.DefaultTimeoutMap.purge(DefaultTimeoutMap.java:212)
        at org.apache.camel.processor.aggregate.AggregateProcessor$AggregationTimeoutMap.purge(AggregateProcessor.java:826)
        at org.apache.camel.support.DefaultTimeoutMap.run(DefaultTimeoutMap.java:162)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: java.io.IOException: Stream closed
        at java.io.BufferedReader.ensureOpen(BufferedReader.java:115)
        at java.io.BufferedReader.read(BufferedReader.java:172)
        at org.apache.camel.converter.IOConverter$1.read(IOConverter.java:83)
        at java.io.InputStream.read(InputStream.java:170)
        at java.io.InputStream.read(InputStream.java:101)
        at org.apache.commons.codec.binary.BaseNCodecInputStream.read(BaseNCodecInputStream.java:158)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
        at java.io.BufferedReader.read1(BufferedReader.java:203)
        at java.io.BufferedReader.read(BufferedReader.java:279)
        at java.io.Reader.read(Reader.java:140)
        at org.apache.camel.util.IOHelper.copy(IOHelper.java:224)
        at org.apache.camel.component.file.FileOperations.writeFileByReaderWithCharset(FileOperations.java:402)
        at org.apache.camel.component.file.FileOperations.storeFile(FileOperations.java:266)
        ... 29 more
    2015-01-22 14:28:26,113 [eTimeoutChecker] AggregateProcessor                            WARN  Error processing aggregated exchange. Exchange[base64File.txt]. Caused by: [org.apache.camel.component.file.GenericFileOperationFailedException - Cannot store file: /path/to/temp/tempDatei.csv]
    org.apache.camel.component.file.GenericFileOperationFailedException: Cannot store file: /path/to/temp/tempDatei.csv
        at org.apache.camel.component.file.FileOperations.storeFile(FileOperations.java:276)
        at org.apache.camel.component.file.GenericFileProducer.writeFile(GenericFileProducer.java:277)
        at org.apache.camel.component.file.GenericFileProducer.processExchange(GenericFileProducer.java:165)
        at org.apache.camel.component.file.GenericFileProducer.process(GenericFileProducer.java:79)
        at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
        at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:120)
        at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:416)
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
        at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:105)
        at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:87)
        at org.apache.camel.processor.aggregate.AggregateProcessor$1.run(AggregateProcessor.java:548)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at org.apache.camel.util.concurrent.SynchronousExecutorService.execute(SynchronousExecutorService.java:62)
        at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:110)
        at org.apache.camel.processor.aggregate.AggregateProcessor.onSubmitCompletion(AggregateProcessor.java:540)
        at org.apache.camel.processor.aggregate.AggregateProcessor.access$900(AggregateProcessor.java:82)
        at org.apache.camel.processor.aggregate.AggregateProcessor$AggregationTimeoutMap.onEviction(AggregateProcessor.java:853)
        at org.apache.camel.processor.aggregate.AggregateProcessor$AggregationTimeoutMap.onEviction(AggregateProcessor.java:814)
        at org.apache.camel.support.DefaultTimeoutMap.purge(DefaultTimeoutMap.java:212)
        at org.apache.camel.processor.aggregate.AggregateProcessor$AggregationTimeoutMap.purge(AggregateProcessor.java:826)
        at org.apache.camel.support.DefaultTimeoutMap.run(DefaultTimeoutMap.java:162)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: java.io.IOException: Stream closed
        at java.io.BufferedReader.ensureOpen(BufferedReader.java:115)
        at java.io.BufferedReader.read(BufferedReader.java:172)
        at org.apache.camel.converter.IOConverter$1.read(IOConverter.java:83)
        at java.io.InputStream.read(InputStream.java:170)
        at java.io.InputStream.read(InputStream.java:101)
        at org.apache.commons.codec.binary.BaseNCodecInputStream.read(BaseNCodecInputStream.java:158)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
        at java.io.BufferedReader.read1(BufferedReader.java:203)
        at java.io.BufferedReader.read(BufferedReader.java:279)
        at java.io.Reader.read(Reader.java:140)
        at org.apache.camel.util.IOHelper.copy(IOHelper.java:224)
        at org.apache.camel.component.file.FileOperations.writeFileByReaderWithCharset(FileOperations.java:402)
        at org.apache.camel.component.file.FileOperations.storeFile(FileOperations.java:266)
        ... 29 more

1 个答案:

答案 0 :(得分:1)

添加行

.split().tokenize(LINE_SEPARATOR).streaming()
现在可以使用了。重要的是在路线的末尾
.end()
,而不是在解组后直接。

我还将Aggregatorstrategy从String更改为byte [],以防止UTF-8编码出错。

完整路线:

from("file:" + this.eingabePfad + "?filter=#dateinamenFilter&maxDepth=1&readLock=changed&noop=true")
                .routeId(ROUTE_ID_BASE64)
                .split().tokenize(LINE_SEPARATOR).streaming()
                .unmarshal().base64()
                .setHeader("foo", constant("foo"))
                .aggregate(header("foo"), new ByteArrayBodyAggregator())
                .completionSize(1000)
                .completionTimeout(1500)
                .to("file:" + this.tempPfad + "?fileName=" + this.tempDateiname + "&charset=utf-8&fileExist=Append")
                .end();