动态添加/删除路由不会重新启动/重置 camelContext。
请找到样品。
DynamicAddRouteProcessor.java
public class DynamicAddRouteProcessor implements Processor {
@Override
public void process(Exchange paramExchange) throws Exception {
final String routeId = "DYNANMIC.ROUTE.1";
Route route = paramExchange.getContext().getRoute(routeId);
if (null == route) {
System.out.println("No route exist, creating one with name ");
paramExchange.getContext().addRoutes(new RouteBuilder() {
public void configure() throws Exception {
from("direct:DYNANMIC.ROUTE.1").routeId(routeId).to("direct:myloggerRoute");
}
});
} else {
System.out.println("Route already exist, no action"+ route.getId());
}
}
}
DynamicRemoveRouteProcessor.java
公共类 DynamicRemoveRouteProcessor 实现处理器 {
@Override
public void process(Exchange paramExchange) throws Exception {
final String routeId = "DYNANMIC.ROUTE.1";
Route route = paramExchange.getContext().getRoute(routeId);
if (null != route) {
System.out.println("Route already exist, deleting it!!!" + route.getId());
paramExchange.getContext().stopRoute(routeId);
paramExchange.getContext().removeRoute(routeId);
} else {
System.out.println("No sucn route exist, no action done "
+ routeId);
}
}
}
蓝图.xml
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
<route id="timerToLog">
<from uri="timer:foo?period=5000" />
<setBody>
<method ref="helloBean" method="hello" />
</setBody>
<log message="The message contains ${body}" />
<to uri="mock:result" />
</route>
<route id="routeAddition">
<from uri="timer:foo?period=10000" />
<process ref="dynamicAddRouteProcessor" />
<log message="Added new route to context....DONE " />
<delay ><simple>5000</simple></delay>
<process ref="dynamicRemoveRouteProcessor" />
<to uri="mock:result" />
</route>
<route id="myloggerRoute">
<from uri="direct:myloggerRoute" />
<log message="Route add/removal completed - ${body}" />
<to uri="mock:result" />
</route>
</camelContext>