2015-06-11 69 views
1

我有一个代码,执行正确的网络服务调用。并且它还记录返回的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服务输出?

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

在此先感谢。

回答

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符号应该更容易。

+1

我需要在相同的地方从我开始驼峰不是另一个类(处理器)的响应。实际上我需要在UI上显示响应。这就是为什么我需要在调用者程序中的响应。 – Viral