2015-05-22 39 views
1

我有一個如下的路由。停止apache.camel路由(RoutePolicy)

 SimpleScheduledRoutePolicy policy = new SimpleScheduledRoutePolicy(); 
    policy.setRouteStartDate(new Date()); 
    policy.setRouteStartRepeatInterval(1000); 

    from("file:data/in") 
      .routePolicy(policy) 
      .to("direct:validateInput") 
      .to("direct:changeInput") 
      .to("file:data/out"); 

因此,路線從輸入文件夾每秒鐘獲取一個文件。經過一些驗證並更改後,將其寫入輸出文件夾。

現在我想能夠關閉每一點的實際路線。所以如果在直接路由validateInput發生一些錯誤,以下兩個部分不應該被執行。

我可以用doTry()和doCatch()做到這一點,但這看起來很難看,也很難閱讀。

問題:是否可以在不停止路線完成的情況下停止主路線的循環?像這樣,實際的文件不會被打印到outfolder,但5秒鐘內的文件可以正常的方式處理。

創建一個新的進程並停止單獨的線程中的主要路徑不起作用。

  1. 的文件仍寫入日期/ OUT文件夾

  2. 路線停止完滿成功,並不會採取任何文件了。

回答

0

只是你validateInput一步拋出異常,並使用onException() clause來處理異常,即會短路的只是信息的流動和允許處理未來的文件落入「數據/在」目錄通常

+0

是的,例外。我認爲這可能會進展順利。感謝提示。好簡單。爲什麼我沒有想到:( – David

0

它運作良好,但1問題仍然存在。我實際的例子如下所示:

    SimpleScheduledRoutePolicy policy = new SimpleScheduledRoutePolicy(); 
       policy.setRouteStartDate(new Date()); 
       policy.setRouteStartRepeatInterval(1000); 

       //start main route 
       from("file:test-data/in?delete=true") 
         .routePolicy(policy) 
         .onException(Exception.class) 
         .log(LoggingLevel.ERROR, "${exception}") 
         .handled(true).useOriginalMessage() 
         .to("file://test-data/error?autoCreate=true") 
         .end() 
         .log(LoggingLevel.DEBUG, "Processing file ${file:name}") 
         .to("direct:validateInput") 
         .to("direct:validateContent") 
         .to("direct:validateOutput") 
         .to("file:test-data/out"); 



       from("direct:validateInput") 
         .doTry() 
         .to("validator:message.xsd") 
         .doCatch(ValidationException.class) 
         .process(getErrorProcessor("Could not validate import against the xsd. Message: ${exception}; File: ${body}")); 

//... 
    } 
} 

private Processor getErrorProcessor(final String message) { 
    return new Processor() { 
     @Override 
     public void process(Exchange exchange) throws Exception { 
      throw new Exception(message); 
     } 
    }; 
} 

如果我把一個不正確的文件到文件夾中,我得到了以下錯誤消息:

錯誤路徑1:145 - java.lang.Exception的:無法驗證針對xsd導入。消息:$ {exception};文件:$ {body}

正如您所看到的,駱駝一次替換了$ {...}屬性。但在此之後,我不會替換新的$ {...}元素。任何想法如何我可以告訴駱駝替換任何新的{...}部分,或者我可以如何替換他們之前我自己?

0

找到了一個很好的答案。對於在這裏發現這個問題的所有人來說,這是一個「完整的」解決方案

 public void start() { 
    try { 
     CamelContext camelContext = new DefaultCamelContext(); 
     camelContext.addRoutes(new RouteBuilder() { 
      @Override 
      public void configure() throws Exception { 
       errorHandler(loggingErrorHandler().level(LoggingLevel.ERROR)); 

       SimpleScheduledRoutePolicy policy = new SimpleScheduledRoutePolicy(); 
       policy.setRouteStartDate(new Date()); 
       policy.setRouteStartRepeatInterval(1000); 

       //Exception Handling in case the xsd validation fails 
       onException(SomeSpecialException.class) 
         .log(LoggingLevel.ERROR, "${exception}") 
         .handled(true).useOriginalMessage() 
         .to("file:test-data/error?autoCreate=true") 
         .end(); 

       //start main route 
       from("file:test-data/in?delete=true") 
         .routePolicy(policy) 
         .log(LoggingLevel.DEBUG, "Processing file ${file:name}") 
         .to("direct:validateInput") 
         .to("direct:validateContent") 
         .to("direct:validateOutput") 
         .to("file:test-data/out"); 

       //start of separate Routes 
       from("direct:validateInput") 
         .doTry() 
         .to("validator:message.xsd") 
         .doCatch(ValidationException.class) 
         .process(getErrorProcess()); 

       //... 
      } 
     }); 
     camelContext.start(); 
    } catch (Exception e) { 
     LOGGER.error("Error while processing camel route: " + e.getMessage()); 
    } 
} 

/** 
* throws an SomeSpecialException in case of calling 
* 
* @return a Processor which is supposed to be called in case an {@link org.apache.camel.ValidationException} happens 
*/ 
private Processor getErrorProcess() { 
    return new Processor() { 
     @Override 
     public void process(Exchange exchange) throws Exception { 
      StringBuilder message = new StringBuilder(); 
      String fileContent = exchange.getIn().getBody(String.class); 
      String originalErrorMessage; 

      try { 
       SchemaValidationException schemaValidationException = (SchemaValidationException) exchange.getProperties().get("CamelExceptionCaught"); 
       originalErrorMessage = schemaValidationException.getMessage(); 
      } catch (Exception e) { 
       originalErrorMessage = "Could not retrieve original error message."; 
      } 
      message.append("Could not validate import against the xsd. ") 
        .append("Original message: ").append(originalErrorMessage).append("; ") 
        .append("File:").append(fileContent); 

      throw new SomeSpecialException(message.toString()); 
     } 
    }; 
} 

感謝大家幫助我。

相關問題