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 -->
     < 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.

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) { 
      return  from(uri + "?splitEntries=false&consumer.initialDelay=0"). 
                marshal().rss(). 
                to("mock:result").routeId("routeId"); 
    } 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值