template 使用参考链接
1在代码中如何获取 camleContext 原文链接
CamelContextAware If you want to be injected with the CamelContext in your POJO just implement the CamelContextAware interface; then when Spring creates your POJO the CamelContext will be injected into your POJO. Also see the Bean Integration for further injections.
You can create a bean in Spring that implements CamelContextAware, something like this:
import org.springframework.stereotype.Service; @Service public class RouteManager implements CamelContextAware { protected static CamelContext camelContext; public CamelContext getCamelContext() { return camelContext; } public void setCamelContext(CamelContext camelContext) { this.camelContext = camelContext; } } If you don't use the annotation you can use:
<bean id="RouteManager " class="myPackage.RouteManager" />2,在代码中加载 route.xml 原文链接
Available as of Camel 2.6
This cookbook shows how to load and add routes from XML files into an existing CamelContext.
When adding routes as shown on this page, the routes is isolated and cannot re-use any existing onException, intercept etc.
You can define Camel routes in XML files using the <routes> tag with the namespace "http://camel.apache.org/schema/spring". Suppose we have the bar route as shown below in the barRoute.xml XML file.
barRoute.xml < routes xmlns="http://camel.apache.org/schema/spring"> <!-- here we define the bar route --> < route id="bar"> < from uri="direct:bar"/> < to uri="mock:bar"/> </ route > <!-- we could add more routes if we like, but in this example we stick to one route only --> </ routes >We can then load this route and add to the existing CamelContext using the following lines of code:
Java code // load route from XML and add them to the existing camel context InputStream is = getClass().getResourceAsStream("barRoute.xml"); RoutesDefinition routes = context.loadRoutesDefinition(is); context.addRouteDefinitions(routes.getRoutes());If you are using older versions of Camel, you can do this as well but it requires a bit more work. See this commit log.
或@Component public class RssRouter implements CamelContextAware { protected CamelContext camelContext; RssRouteBuilder rssRouteBuilder; final Logger log = Logger.getLogger(this.getClass()); public void addFeed(String uri) throws Exception { log.info("Adding feed '" + uri + "'"); uri = "rss://" + uri; camelContext.addRouteDefinition(rssRouteBuilder.getRouteDefinition(uri)); } public void removeFeed(String uri) throws Exception { log.info("Removing feed '" + uri + "'"); uri = "rss://" + uri; camelContext.removeRoute(uri); } public CamelContext getCamelContext() { return camelContext; } public void setCamelContext(CamelContext camelContext) { this.camelContext = camelContext; } public void setRssRouteBuilder(RssRouteBuilder rssRouteBuilder) { this.rssRouteBuilder = rssRouteBuilder; } } public class RssRouteBuilder extends RouteBuilder { @Override public void configure() throws Exception { // Do nothing here since we are creating routes dynamically } RouteDefinition getRouteDefinition(String uri) { from(uri + "?splitEntries=false&consumer.initialDelay=0"). marshal().rss(). to("mock:result").routeId("routeId"); } }
转载于:https://www.cnblogs.com/mendeliang/p/5070832.html