从Camel Context检索输出

时间:2015-06-11 09:12:33

标签: java xml spring apache-camel

我有一个代码,根据Web服务调用执行正确。并且它还记录返回的SOAP Header。现在我需要在UI上显示相同的xml,因为我需要从驼峰上下文中检索输出。

代码:

DefaultCamelContext context = new DefaultCamelContext();

    // append the routes to the context
    String myString = "<route id=\"my_Sample_Camel_Route_with_CXF\" xmlns=\"http://camel.apache.org/schema/spring\">"
            + "      <from uri=\"file:///home/viral/Projects/camel/cxfJavaTest/src/data?noop=true\"/>"
            + "    <log loggingLevel=\"INFO\" message=\"&gt;&gt;&gt; ${body}\"/>"
            + "   <to uri=\"cxf://http://www.webservicex.net/stockquote.asmx?wsdlURL=src/wsdl/stockquote.wsdl&amp;serviceName={http://www.webserviceX.NET/}StockQuote&amp;portName={http://www.webserviceX.NET/}StockQuoteSoap&amp;dataFormat=MESSAGE\"/>"
            + "  <log loggingLevel=\"INFO\" message=\"&gt;&gt;&gt; ${body}\"/>"
            + "    </route>";
    ;

    InputStream is =  new ByteArrayInputStream(myString.getBytes());
    RoutesDefinition routes = context.loadRoutesDefinition(is);
    context.addRouteDefinitions(routes.getRoutes());
    context.setTracing(true);

    context.start();

    Thread.sleep(3000);     

    System.out.println("Done");
    context.stop();

有没有办法使用上下文变量或任何其他方式获取使用日志语句打印的Web服务响应的Web服务输出?

如果您提供的代码对我非常有帮助。

提前致谢。

1 个答案:

答案 0 :(得分:2)

您可以创建一个自定义处理器来访问您路线中的数据。

class MyProcessor extends Processor {

  public void process(Exchange exchange) throws Exception {
    String body = exchange.getIn().getBody(String.class); //Maybe you need some other type.
   //do your logic here
  }

}

然后你可以将它添加到你的路线中(你需要将它添加到你的驼峰上下文注册表中):

<process id="myProcessor">

另外,为什么在代码中使用XML表示法。使用Java DSL表示法应该更容易。