当前位置: 首页 > 知识库问答 >
问题:

捕获阿帕奇骆驼路线cxf网页响应

师成弘
2023-03-14

我是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个答案

何涵忍
2023-03-14

我想我已经设法修好了。。

我认为这可能是由于响应没有被缓存或存储在路由中的任何地方,导致它创建空白文件(Meas1.1.xml,Mease2.xml和MexAd3.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>

谢谢你的建议:)

夏炎彬
2023-03-14

我不确定你是否message2.xml调用单向服务。你能仔细检查你的src/data目录中的message2.xml吗?如果是单向消息或残缺的肥皂请求,你可能得不到你想要的正确输入流。

苗阳文
2023-03-14

使用Camel v2。12.2,我没有得到一个例外,但当记录正文内容时,文件是空的。这是因为流只能读取一次,请参见此处。因此,在使用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选项,以便获得一个不错的文件名。

 类似资料:
  • 我有一个spring boot应用程序,我正在向其中添加一个camel路由。定义路由的类扩展了FatJarRouter,并用@component注释。当应用程序作为spring boot应用程序运行时,不会识别路由。但是,如果我在主类中使用@SpringBootApplication注释编写路由,则会标识路由。这是它目前在日志中的显示方式: 请告诉我如何在将路由作为一个单独的类而不是在主类中编写时

  • 我有一个Camel route,它使用队列中的消息并将消息存储到数据库中。现在,我想以一种优雅的方式手动关闭运行骆驼路线。我有一个RestEndpoint,每当我需要停止Camel route时就会被触发。此endpoint应停止路由。但是,如果在关闭期间有任何正在运行的消息或事务,则必须在不消耗来自camel route的("")endpoint的任何新消息的情况下成功完成,并在完成正在运行的消

  • 我试图使用Apache Camel Quartz2实现一个调度器,它每分钟执行一次路由,并按预期执行一些任务。我使用spring DSL实现与apache camel相关联的路由,如下所示: 根据日志,它不会记录为路由记录的消息,例如Direct:DomainsWithFTPUsers等等。请指导如何实现同样的目标。

  • 遵循官方文件(https://camel.apache.org/manual/component-dsl.html#_using_component_dsl)我创建了以下代码: 但是中的告诉我: 并且中的特性不建议导入相应的库。 有人能给我指出正确的方向吗? 我必须理解的概念才能做到这一点吗?

  • 我们需要的是直接的API来设置和使用集群消息队列。我们最初的计划是使用Camel在集群JMS或ActiveMQ队列上进行消费/生产。Kafka如何使这项任务变得更容易?在任何一种情况下,应用程序本身都将在WebLogic服务器上运行。 消息传递将是点对点类型,其中有多个相同服务的实例在运行,但根据负载平衡策略,只有一个实例应该处理消息并发出结果。消息队列也是群集的,因此服务实例或队列实例的失败都不

  • 考虑到apache Camel,我有一个问题:是否可以通过代码来创建全局拦截器,例如AOP?拦截器应该跳过endpoint还是模仿endpoint? 提前致谢