2017-07-07 49 views
1

我需要根據某些條件來限制添加到駱駝上下文的特定路由。試過RoutePolicy.onStart - >但是這個方法在啓動路由後被調用。基於某些條件,禁用路由被添加到駱駝上下文

我需要一種方法來完全避免生成/添加到駱駝上下文的路線。

private List<String> eligibleRoutes; 

@Override 
    public void onStart(Route route) { 
    LOGGER.info("onInit for {}", route.getId()); 
    if (isCollectionNotEmpty(eligibleRoutes)) 
    { 
     LOGGER.info("route-start eligibility for route {}", route.getId()); 
     if (eligibleRoutes.contains(route.getId())) 
     { 
     LOGGER.info("Route-start is set to ELIGiBLE for {}", route.getId()); 
     }else{ 
     LOGGER.info("Route-start is set to NOT ELIGiBLE for {}", route.getId()); 

     route.getRouteContext().getCamelContext().stopRoute(route.getId()); 
     boolean status = route.getRouteContext().getCamelContext().removeRoute(route.getId()); 
     return; 
     } 
} 
+0

您是使用Spring集成還是自己創建和管理'CamelContext'?如何將路由添加到代碼中的上下文中? –

+0

使用藍圖創建camelContext和路線。我有多條路線,只有在符合條件時才需要讓它們可見。 – Venkat

+0

[基於內容的路由器](http://camel.apache.org/content-based-router.html)如何? –

回答

2

您需要設置的自動啓動=虛假的路線,然後在onInit方法,你可以決定是否應該啓動無論如何,並調用其startRoute方法。

或者另一種方式是配置CamelContext有autoStartup=false,然後你可以有一個駱駝事件利斯特的bean偵聽CamelContextStartedEvent,然後觸發那裏找到了你要開始具體航線,並調用camelContext.startRoute("nameOfRoute")

+0

謝謝克勞斯。我用CamelContextStartedEvent嘗試了EventNotifierSupport,它的功能就像一個魅力。 – Venkat