捕获apache camel route cxf web响应

时间:2014-04-28 11:16:10

标签: xml web-services maven components apache-camel

我是Stack Overflow和Apache Camel路由的新手,我遇到了一些麻烦。我创建了一个非常简单的SOAP Web服务,它接受包含一些随机文本的XML请求,然后在响应中返回相同的字符串。

使用Camel我能够将请求发送到Web服务并获得响应(如果我使用日志组件来捕获请求/响应。

理想情况下,我想将XML消息(message1.xml,message2.xml,message3.xml)发送到Web服务,然后处理它们并将响应存储在文件中:src / data / responses /(message1.xml, message2.xml,message3.xml)等。我认为这可能就像在我的路线中添加一个额外的步骤一样简单(见下文)。

<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="file:src/data?noop=true" />
<log loggingLevel="INFO" message="${body}"/>
<to uri="cxf://http://localhost:8181/cxf/webservice?wsdlURL=src/main/resources/META-INF/webservice.wsdl&amp;serviceName={http://carose.com/}EchoServiceImplService&amp;portName={http://carose.com/}EchoServiceImplPort&amp;dataFormat=MESSAGE"/>
<log loggingLevel="INFO" message="${body}"/>
<to uri="file:src/data/responses" />
</route>
</camelContext>

但是,当我使用mvn camel:run运行路由时,它会在/ responses /文件夹中为每条消息创建一个空白XML文件。执行路径时会显示以下错误消息:

[           default-workqueue-1] route1                         INFO  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:echoTextResponse xmlns:ns2="http://carose.com/"><return>Hello this is a test 123</return></ns2:echoTextResponse></soap:Body></soap:Envelope>
[           default-workqueue-2] route1                         INFO  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:echoTextResponse xmlns:ns2="http://carose.com/"><return>this is another test!</return></ns2:echoTextResponse></soap:Body></soap:Envelope>
[           default-workqueue-3] route1                         INFO  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:echoTextResponse xmlns:ns2="http://carose.com/"><return>and another test!</return></ns2:echoTextResponse></soap:Body></soap:Envelope>
[           default-workqueue-2] DefaultErrorHandler            ERROR Failed delivery for (MessageId: ID-christophersair-lan-53992-1398682968504-0-9 on ExchangeId: ID-christophersair-lan-53992-1398682968504-0-4). Exhausted after delivery attempt: 1 caught: org.apache.camel.component.file.GenericFileOperationFailedException: Cannot store file: src/data/responses/message2.xml

消息历史记录

RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
[route1            ] [route1            ] [file://src/data?noop=true                                                     ] [        26]
[route1            ] [log1              ] [log                                                                           ] [         1]
[route1            ] [to1               ] [cxf://http://localhost:8181/cxf/webservice?wsdlURL=src/main/resources/META-INF] [        15]
[route1            ] [log2              ] [log                                                                           ] [         3]
[route1            ] [to2               ] [file:src/data/responses                                                       ] [         5]

交换

Exchange[
Id                  ID-christophersair-lan-53992-1398682968504-0-4
ExchangePattern     InOnly
Headers             {breadcrumbId=ID-christophersair-lan-53992-1398682968504-0-3, CamelCxfMessage={org.apache.cxf.client=true, org.apache.cxf.message.inbound=true, org.apache.cxf.message.Message.PROTOCOL_HEADERS={content-type=[text/xml;charset=UTF-8], Server=[Jetty(7.6.7.v20120910)], transfer-encoding=[chunked]}, Content-Type=text/xml;charset=UTF-8, org.apache.cxf.transport.Conduit=conduit: class org.apache.cxf.transport.http.URLConnectionHTTPConduit2125068082target: http://localhost:8181/cxf/webservice, org.apache.cxf.ws.policy.AssertionInfoMap={}, org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@48a43495, org.apache.cxf.message.Message.RESPONSE_CODE=200, org.apache.cxf.message.Message.ENCODING=UTF-8}, CamelFileAbsolute=false, CamelFileAbsolutePath=/Users/christopherrose/RedHat/SG Project/webservice-client/camel-wsclient/src/data/message2.xml, CamelFileLastModified=1398259868000, CamelFileLength=337, CamelFileName=message2.xml, CamelFileNameConsumed=message2.xml, CamelFileNameOnly=message2.xml, CamelFileParent=src/data, CamelFilePath=src/data/message2.xml, CamelFileRelativePath=message2.xml, CamelRedelivered=false, CamelRedeliveryCounter=0, content-type=text/xml;charset=UTF-8, ResponseContext={org.apache.cxf.client=true, org.apache.cxf.message.Message.PROTOCOL_HEADERS={content-type=[text/xml;charset=UTF-8], Server=[Jetty(7.6.7.v20120910)], transfer-encoding=[chunked]}, org.apache.cxf.message.inbound=true, Content-Type=text/xml;charset=UTF-8, org.apache.cxf.message.Message.ENCODING=UTF-8, org.apache.cxf.message.Message.RESPONSE_CODE=200, org.apache.cxf.binding.soap.SoapVersion=org.apache.cxf.binding.soap.Soap11@48a43495, org.apache.cxf.ws.policy.AssertionInfoMap={}, org.apache.cxf.transport.Conduit=conduit: class org.apache.cxf.transport.http.URLConnectionHTTPConduit2125068082target: http://localhost:8181/cxf/webservice}, Server=Jetty(7.6.7.v20120910), transfer-encoding=chunked}
BodyType            sun.net.www.protocol.http.HttpURLConnection.HttpInputStream
Body                [Body is instance of java.io.InputStream]

堆栈跟踪

org.apache.camel.component.file.GenericFileOperationFailedException: Cannot store file: src/data/responses/message2.xml
at org.apache.camel.component.file.FileOperations.storeFile(FileOperations.java:269)
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:113)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:398)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:118)
at org.apache.camel.processor.Pipeline.access$100(Pipeline.java:43)
at org.apache.camel.processor.Pipeline$1.done(Pipeline.java:136)
at org.apache.camel.processor.CamelInternalProcessor$InternalCallback.done(CamelInternalProcessor.java:251)
at org.apache.camel.processor.RedeliveryErrorHandler$1.done(RedeliveryErrorHandler.java:410)
at org.apache.camel.management.InstrumentationProcessor$1.done(InstrumentationProcessor.java:81)
at org.apache.camel.processor.SendProcessor$1.done(SendProcessor.java:123)
at org.apache.camel.component.cxf.CxfClientCallback.handleResponse(CxfClientCallback.java:63)
at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:856)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1614)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream$1.run(HTTPConduit.java:1129)
at org.apache.cxf.workqueue.AutomaticWorkQueueImpl$3.run(AutomaticWorkQueueImpl.java:428)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.cxf.workqueue.AutomaticWorkQueueImpl$AWQThreadFactory$1.run(AutomaticWorkQueueImpl.java:353)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.io.IOException: stream is closed
at sun.net.www.http.ChunkedInputStream.ensureOpen(ChunkedInputStream.java:174)
at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:673)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3053)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3047)
at org.apache.camel.component.file.FileOperations.writeFileByStream(FileOperations.java:375)
at org.apache.camel.component.file.FileOperations.storeFile(FileOperations.java:263)
... 24 more
[           default-workqueue-3] DefaultErrorHandler            ERROR Failed delivery for (MessageId: ID-christophersair-lan-53992-1398682968504-0-7 on ExchangeId: ID-christophersair-lan-53992-1398682968504-0-6). Exhausted after delivery attempt: 1 caught: org.apache.camel.component.file.GenericFileOperationFailedException: Cannot store file: src/data/responses/message3.xml

我假设它可能无法检索响应并将其存储在文件中,或者可能有一些东西可以指向我的方向?

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

我不确定message2.xml是否调用单向服务。 你能仔细检查你的src / data目录中的message2.xml吗? 如果是单向消息或无效的soap请求,您可能无法获得所需的正确输入流。

答案 1 :(得分:0)

使用Camel v2.12.2,我没有得到异常,但在记录正文内容时该文件为空。这是因为流只能读取一次,请参阅here。因此,在使用CXF Web服务后删除日志:

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="file:src/data?noop=true" />
        <log loggingLevel="INFO" message="${body}"/>
        <to uri="cxf://http://localhost:8181/cxf/webservice?wsdlURL=src/main/resources/META-INF/webservice.wsdl&amp;serviceName={http://carose.com/}EchoServiceImplService&amp;portName={http://carose.com/}EchoServiceImplPort&amp;dataFormat=MESSAGE"/>
        <!-- do not log the body or the file written will be empty
        <log loggingLevel="INFO" message="${body}"/>
        -->
        <to uri="file:src/data/responses?fileName=output.txt" />
    </route>
</camelContext>

除此之外,我添加了fileName选项以获得一个漂亮的文件名。

答案 2 :(得分:0)

我想我已经成功解决了这个问题。

我认为这可能是因为路由期间没有缓存或存储任何响应,导致它创建空白文件(message1.xml,message2.xml和message3.xml)。我发现你可以设置一个流缓存设置,所以我打开它,它现在正在创建所有三个文件,其中包含正确的XML响应。请参阅下面的路由XML:

<camelContext xmlns="http://camel.apache.org/schema/spring">
<route streamCache="true">
    <from uri="file:src/data?noop=true" />
    <log loggingLevel="INFO" message="${body}"/>
    <to uri="cxf://http://localhost:8181/cxf/webservice?wsdlURL=src/main/resources/META-INF/webservice.wsdl&amp;serviceName={http://carose.com/}EchoServiceImplService&amp;portName={http://carose.com/}EchoServiceImplPort&amp;dataFormat=MESSAGE"/>
    <log loggingLevel="INFO" message="${body}"/>
    <to uri="file:src/data/responses" />
</route>

感谢您的建议:)