2015-11-02 118 views
1

我有这段代码用于从使用wicket的java Tomcat web开发执行脚本shell。骆驼exec从shell脚本返回始终为空字符串

public class CallingScript extends RouteBuilder { 
    String result; 

    @Override 
    public void configure() throws Exception { 
     from("direct:exec") 
       .to("exec:ls?args=/home/foo/") 
       .process(new Processor() { 
        public void process(Exchange exchange) throws Exception { 
         ExecResult execresult = exchange.getIn().getBody(ExecResult.class); 
         result = execesult.toString(); 
        } 
       }); 
    } 
    public String getResult() { 
     return result; 
    } 
} 

我用它在检票7.0.0的onclick方法,没有产生错误消息,但始终是一个空字符串由exec路线返回。

CamelContext camelContext = new DefaultCamelContext(); 
    CallingScript call = new CallingScript(); 
    try { 
     camelContext.addRoutes(call); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    try { 
     camelContext.start(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    try { 
     camelContext.stop(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    String res = call.getResult(); 
    getSession().info("directory contents " + res + " !"); 

回答

0

刚开始骆驼上下文不足以触发路线。如果您想手动触发路线,则可以使用camelContext.startRoute()方法。

顺便说一下,恕我直言,这是一个最佳做法,为每个应用程序启动一个骆驼上下文。