apache camle template , Adding Routes dynamically

template 使用参考链接  

Java Code Examples fororg.apache.camel.ProducerTemplate

Introduction to the Producer 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  原文链接

Loading routes from XML files

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 onExceptionintercept 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
    <!-- here we define the bar route -->
    <routeid="bar">
        <fromuri="direct:bar"/>
        <touri="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.

Adding Routes dynamically

@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"); 
    } 




posted @ 2015-09-21 17:08  mendel  阅读(510)  评论(0编辑  收藏  举报