【发布时间】:2016-03-22 13:32:22
【问题描述】:
我正在尝试从 Camel 的路线中删除一些样板。
例如,让我们考虑两条路线,它们相似并且可以生成它们的大部分内部内容。我创建了一个组件“模板”,它创建了TemplateEndpoint,并修改了一个 XML 配置以使用模板组件。
正在从StartupListener(在TemplateEndpoint.setSuffix 中定义)调用自定义方法TemplateEndpoint.generateRoute(添加路由定义)。
因此,当 Camel 上下文启动时,路由定义出现在上下文中,但框架不会为它们创建路由服务,因此它们不会启动。
StartupListener中添加的路由如何启动?
我可能遇到了 XY 问题,您可以建议我一个更好的方法来解决这个问题(即即时添加和启动路线)。
类似的两条路线
<route id="route_A">
<from uri="restlet:/another1?restletMethods=GET" />
<to uri="first:run_A" />
<to uri="second:jump_A" />
<to uri="third:fly_A" />
</route>
<route id="route_B">
<from uri="restlet:/another2?restletMethods=GET" />
<to uri="first:run_B" />
<to uri="second:jump_B" />
<to uri="third:fly_B" />
</route>
修改后的 XML
<route id="route_A">
<from uri="restlet:/another1?restletMethods=GET" />
<to uri="template://?suffix=A" />
</route>
<route id="route_B">
<from uri="restlet:/another2?restletMethods=GET" />
<to uri="template://?suffix=B" />
</route>
端点
public class TemplateEndpoint extends DefaultEndpoint {
/* some methods omitted */
// this endpoint creates a DefaultProducer, which does nothing
public void setSuffix(final String suffix) throws Exception {
this.getCamelContext().addStartupListener(new StartupListener() {
@Override
public void onCamelContextStarted(CamelContext context, boolean alreadyStarted) throws Exception {
generateRoute(suffix)
.addRoutesToCamelContext(context);
}
});
}
private RouteBuilder generateRoute(final String suffix){
final Endpoint endpoint = this;
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from(endpoint)
.to("first:run_" + suffix)
.to("second:jump_" + suffix)
.to("third:fly_" + suffix);
}
}
}
【问题讨论】:
标签: java templates routes apache-camel boilerplate