Sti*_*ael 5 spring apache-camel
我想使用 来控制我的路线何时启动RoutePolicy。因此,我将其定义为autoStartup=false
<camel:route id="myRoute" routePolicyRef="myRoutePolicy" autoStartup="false">
<!-- details omitted -->
</camel:route>
Run Code Online (Sandbox Code Playgroud)
为了开始这条路线,我尝试了:
public class MyRoutePolicy implements RoutePolicy {
// Does NOT work!
@Override
public void onInit(Route route) {
if(shouldStartRoute(route)) {
camelContext.startRoute(route.getId());
}
}
// More stuff omitted
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,什么也没发生。JavadocCamelContext.startRoute(String)可能解释了原因:
如果给定路线之前已停止,则启动该路线
如何开始一条之前没有停止过的路线?
我正在使用 Camel 2.8.0 并且无法升级。
我可以使用 JMX 控制台启动路线,但我不想依赖 JMX。
我一直在调试骆驼。似乎CamelContext.startRoute(String)不会以 autoStartup=false 启动路线。
开始路线的正确方法是:
ServiceHelper.startService(route.getConsumer());
Run Code Online (Sandbox Code Playgroud)
但这对我来说不起作用RoutePolicy:
public class MyRoutePolicy implements RoutePolicy {
// Does NOT work either!
@Override
public void onInit(Route route) {
if(shouldStartRoute(route)) {
ServiceHelper.startService(route.getConsumer());
}
}
// More stuff omitted
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是route.getConsumer()返回null. 你必须等到Camel上下文初始化才能调用ServiceHelper.startService(consumer)。所以这是有效的:
public class MyRoutePolicy extends EventNotifierSupport implements RoutePolicy {
private final Collection<Route> routes = new HashSet<>();
@Override
public void onInit(Route route) {
routes.add(route);
CamelContext camelContext = route.getRouteContext().getCamelContext();
ManagementStrategy managementStrategy = camelContext.getManagementStrategy();
if (!managementStrategy.getEventNotifiers().contains(this)) {
managementStrategy.addEventNotifier(this);
}
}
// Works!
@Override
public void notify(EventObject event) throws Exception {
if(event instanceof CamelContextStartedEvent) {
for(Route route : routes) {
if(shouldStartRoute(route)) {
ServiceHelper.startService(route.getConsumer());
}
}
}
}
// More stuff omitted
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10102 次 |
| 最近记录: |