繁体   English   中英

自动路线发现骆驼

[英]Automatic routes discovery Camel

在我的应用程序中,我将一些xml文件存储在一个文件夹中,这些文件声明了我的路线。 我想在应用程序引导程序中上载所有路由,并将它们存储在骆驼上下文中。 换句话说,我想自动发现存储在那些xml文件中的路由。

这是包含路由的文件的示例

<?xml version="1.0" encoding="UTF-8"?>
<routeContext id="myRoute" xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="file:C:/LocalFTPServer" />
        <log message="Got a file!" loggingLevel="INFO" loggerRef="myLogger" />
        <choice>
            <when>
                <simple>${file:ext} == 'csv'</simple>
                <log message="I'm going to email you!" loggingLevel="INFO"
                    loggerRef="myLogger" />
            </when>
            <otherwise>
                <log message="File extension wrong." loggingLevel="WARN"
                    loggerRef="myLogger" />
            </otherwise>
        </choice>
    </route>
</routeContext>

这是我的应用程序上下文

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

    <import resource="beans/beans.xml" />

    <camelContext xmlns="http://camel.apache.org/schema/spring">
    </camelContext>

</beans>

如果要在某些事件上添加路由,请使用context.addRouteDefinitions方法

例如:

    public class ContextStartEventListener extends EventNotifierSupport implements ApplicationContextAware {

        private ApplicationContext applicationContext;

        @Override
        public void setApplicationContext(ApplicationContext applicationContext) {
            this.applicationContext = applicationContext;
        }

        @Override
        public void notify(EventObject event) throws Exception {
            if (event instanceof CamelContextStartedEvent) {
                try {
                    CamelContextStartedEvent startedEvent = (CamelContextStartedEvent) event;
                    DefaultCamelContext context=(DefaultCamelContext)startedEvent.getContext();
                    Resource[] xmlResources=applicationContext.getResources("classpath*:net/**/route.xml");
                    for (int i=0;i<xmlResources.length;i++) {
                        InputStream is = xmlResources[i].getInputStream();
                        RoutesDefinition routes = context.loadRoutesDefinition(is);
                        context.addRouteDefinitions(routes.getRoutes());
                    }       
                } catch (Throwable ex) {
                    // do something on error
                }
            }
        }
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM