Apache Camel CXF端点null回复

时间:2015-06-29 10:01:14

标签: java web-services apache-camel cxf

我遵循了Camel in Action手册的第7章并实施了CXF路线。

<cxf:cxfEndpoint 
    id="orderEndpoint" 
    address="http://localhost:9000/order/"
    serviceClass="test.order.OrderEndpoint" 
    wsdlURL="wsdl/order.wsdl" />


<camelContext xmlns="http://camel.apache.org/schema/spring">
    <route>
        <!-- Expose route as web service endpoint -->
        <from uri="cxf:bean:orderEndpoint" />
        <to uri="log:orderEndpoint_MsgIn" />
        <to uri="seda:incomingOrders" />
        <transform>
            <constant>OK</constant>
        </transform>
        <to uri="log:orderEndpoint_MsgOut" />
    </route>

    <!-- test route -->
    <route>
        <from uri="seda:incomingOrders" />
        <to uri="mock:end" />
    </route>
</camelContext>

然后我实现了一个java控制台类来测试路由

public static void main(String[] args) throws Exception{
    Main main = new Main(); 
    AbstractApplicationContext context = new ClassPathXmlApplicationContext(
            "META-INF/spring/cxfWsComponentTestContext.xml"
            );

    main.setApplicationContext(context);

    main.start();
    ProducerTemplate producer = main.getCamelTemplate();

    List<Object> params = new ArrayList<Object>();
    params.add("motor");
    params.add(1);
    params.add("honda");

    String reply = producer.requestBody("cxf:bean:orderEndpoint", params, String.class);
    LOG.info("Received reply from orderEndpoint = " + reply);

    Thread.sleep(10000);

    main.stop();

} 

预计控制台日志应显示

"Received reply from orderEndpoint = OK"

然而,我得到了

"Received reply from orderEndpoint = null" 

在控制台日志中,我可以看到条目

"orderEndpoint_MsgOut           INFO  Exchange[ExchangePattern: InOut, BodyType: String, Body: OK]"

这意味着Web服务应该生成回复消息&#34; OK&#34;但是我不确定为什么这个回复消息没有作为requestBody()方法调用的返回值继承...

我下载了Camel in Action第7章的示例代码并运行Junit测试用例,它能够按预期返回结果...我试图将路由定义和源代码与我的比较但不能到目前为止发现任何差异...

想知道是否有人可以提供一些线索。

---编辑1 ---

我已更新路线定义并仅保留以下路线

    <route>
        <from uri="cxf:bean:orderEndpoint" />
        <transform>
            <constant>OK</constant>
        </transform>
        <to uri="seda:incomingOrders" />
    </route>

并更新了主要方法

    producer.requestBody("cxf:bean:orderEndpoint", params, String.class);
    ConsumerTemplate consumer = producer.getCamelContext().createConsumerTemplate();
    Exchange reply = consumer.receive("seda:incomingOrders");
    LOG.info("Received reply from seda:incomingOrders = " + reply);

但是,它因超时异常而失败

org.apache.camel.ExchangeTimedOutException: The OUT message was not received within: 30000 millis. Exchange[Message: OK]
at org.apache.camel.component.seda.SedaProducer.process(SedaProducer.java:144)
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:129)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:448)
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.process(Pipeline.java:80)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
at org.apache.camel.component.cxf.CxfConsumer$1.asyncInvoke(CxfConsumer.java:95)
at org.apache.camel.component.cxf.CxfConsumer$1.invoke(CxfConsumer.java:75)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor$2.run(ServiceInvokerInterceptor.java:126)
at org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:131)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:251)
at org.apache.cxf.transport.http_jetty.JettyHTTPDestination.doService(JettyHTTPDestination.java:234)
at org.apache.cxf.transport.http_jetty.JettyHTTPHandler.handle(JettyHTTPHandler.java:70)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1129)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1065)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:497)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:310)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:2)

您正在请求CXF端点的正文。如果查看路线,可以看到在路径定义中的变换标记中设置了主体的内容。因此,requestBody()导致您没有成功,这是合乎逻辑的。这同样适用于您的seda端点。我敢打赌orderEndpoint_MsgIn的记录正文与OK不同。

关于orderEndpoint_MsgOut正确的日志,这是因为您在转换步骤后记录了正文。

为了接收OK,您可以使用以下路线:

<route>
    <from uri="cxf:bean:orderEndpoint" />
    <transform>
        <constant>OK</constant>
    </transform>
    <wireTap uri="seda:incomingOrders" />
</route>

ConsumerTemplate与seda端点一起使用:

ConsumerTemplate consumer = producer.getCamelContext().createConsumerTemplate();
String reply = consumer.receive("seda:incomingOrders");
LOG.info("Received reply from seda = " + reply);

您应该在requestBody()main.stop()之间使用此代码。

有关使用wireTap而不是to的说明,请参阅此post