我的要求如下:我需要解压缩文件并从解压缩的xml文件中读取元数据信息。这将在中完成xmlToObjectMapper
。现在,我需要使用位于zip文件中的多个其他xml的数据来丰富元数据。这意味着我必须将它们拆包阅读,然后将信息附加到我的MetadataHolder
对象上。我想在注释行中实现这一点pollEnrich
。
from("file://{{first.directory}}?noop=true&idempotent=true")
.split(new ZipSplitter())
.streaming()
.choice()
.when(header("zipFileName").isEqualTo("metadata.xml"))
.multicast()
.to("file://{{first.directory}}/Working?fileName=$simple{file:onlyname.noext}/${header.zipFileName}")
.pipeline()
.convertBodyTo(String.class, StandardCharsets.ISO_8859_1.name())
.bean("xmlToObjectMapper", "readAllRequiredMetadata")
//.pollEnrich("", 0, new MetadataHolderAggregationStrategy())
.end()
.end()
.end()
.to("file://{{first.directory}}/Working?fileName=$simple{file:onlyname.noext}/${header.zipFileName}")
.end()
.end()
;
但是现在我被卡住了,因为我得到了以下堆栈跟踪:
org.apache.camel.InvalidPayloadException: No body available of type: java.lang.String but has value: org.apache.camel.dataformat.zipfile.ZipInputStreamWrapper@2c52254b of type: org.apache.camel.dataformat.zipfile.ZipInpu
tStreamWrapper on: Message[id]. Caused by: Error during type conversion from type: java.lang.String to the required type: java.lang.String with value [Body is instance of java.io.I
nputStream] due java.io.IOException: Stream closed. Exchange[id]. Caused by: [org.apache.camel.TypeConversionException - Error during type conversion from type: java.lang.String to
the required type: java.lang.String with value [Body is instance of java.io.InputStream] due java.io.IOException: Stream closed]
我以为多播会隐式缓存流并将其发送到所有端点...
有人能告诉我为什么会这样吗?我对需求的解决方案是有效的方法还是必须将需求分为多条路线?
编辑1:
我通过添加.streamCaching()
到路由解决了异常的问题。由于其他问题,我不得不稍微更改原始路线。
from("file://{{first.directory}}?noop=true&idempotent=true")
.streamCaching()
.split(new ZipSplitter())
.streaming()
.choice()
.when(header("zipFileName").isEqualTo("metadata.xml"))
.multicast()
.to("file://{{first.directory}}/Working?fileName=$simple{file:onlyname.noext}/${header.zipFileName}")
.pipeline()
.convertBodyTo(String.class, StandardCharsets.ISO_8859_1.name())
.bean("xmlToObjectMapper", "readAllRequiredMetadata")
.pollEnrich("file://{{second.directory}}?&noop=true&idempotent=true", 0, new MetadataHolderAggregationStrategy())
.end()
.end()
.endChoice()
.otherwise()
.to("file://{{first.directory}}/Working?fileName=$simple{file:onlyname.noext}/${header.zipFileName}")
.end()
.end()
.end()
;
但是现在我遇到了问题pollEnrich
。我的newExchangeMetadataHolderAggregationStrategy
始终为null。我该如何解决?
编辑2:
终于我找到了解决方案。我必须使用enrich
代替pollEnrich
并在java(readAllRequiredMetadata2
)中实现整个I / O。我的最终解决方案如下所示:
from("file://{{first.directory}}?noop=true&idempotent=true")
.streamCaching()
.split(new ZipSplitter())
.streaming()
.choice()
.when(header("zipFileName").isEqualTo("metadata.xml"))
.multicast()
.to("file://{{first.directory}}/Working?fileName=$simple{file:onlyname.noext}/${header.zipFileName}")
.pipeline()
.convertBodyTo(String.class, StandardCharsets.ISO_8859_1.name())
.bean("xmlToObjectMapper", "readAllRequiredMetadata")
.enrich("direct:readResources", 0, new MetadataHolderAggregationStrategy())
//Go on processing the enriched metadata
.end()
.end()
.endChoice()
.otherwise()
.to("file://{{first.directory}}/Working?fileName=$simple{file:onlyname.noext}/${header.zipFileName}")
.end()
.end()
.end()
;
from("direct:readResources")
.bean("xmlToObjectMapper", "readAllRequiredMetadata2")
.end()
;
对于类似流的数据,您可能需要使用流缓存。默认情况下它是关闭的,因此第二个端点将不具有它,因为第一个端点已经读取了它。您可以在此处查看更多详细信息:http : //camel.apache.org/stream-caching.html
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句