【问题标题】:Automatic routes discovery Camel自动路由发现骆驼
【发布时间】:2015-09-27 21:07:19
【问题描述】:

在我的应用程序中,我有一些 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>

【问题讨论】:

  • &lt;import resource="classpath*:/routes/*.xml"/&gt; 呢?
  • 以这种方式导入文件,但不会在骆驼上下文中“注册”其中包含的路由。我正在寻找可以做到这一点的东西。

标签: java xml spring apache-camel


【解决方案1】:

如果你想在某些事件上添加路由,只需使用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
                }
            }
        }
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多