如何基于旗帜有条件地加载骆驼路线?

时间:2015-04-09 10:07:43

标签: java caching apache-camel

我有一个使用apache camel,activemq等应用程序的应用程序。我还使用camel-cache来缓存对象。我试图并行运行所有测试类以减少构建时间。这是我使用的maven配置

 <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
     <version>2.18.1</version>
     <configuration>
        <parallel>classes</parallel>
        <useUnlimitedThreads>true</useUnlimitedThreads>
     </configuration>
</plugin>

这为每个测试类创建了一个新的camel上下文。除了缓存的问题,这将运行正常。我们有一个初始化缓存的路由,如下所示:

 <route id="xcache">
   <from uri="cache://xCache?maxElementsInMemory=1000&amp;overflowToDisk=true&amp;timeToLiveSeconds=300&amp;timeToIdleSeconds=200" />
   <log loggingLevel="INFO" logName="IFATMSTrace" message="X Cache configuration initialized." />
 </route>

多个camelContexts尝试使用相同的名称“xCache”初始化缓存并抛出异常。由于各种原因,我不能让每个上下文创建自己的缓存。为了解决这个问题,我想在测试期间完全禁用缓存,以便测试可以通过

为实现这一目标,我尝试使用autoStartup属性手动加载缓存路径

<route id="xcache" autoStartup="false">
  .....
</route>

然后在配置文件中定义一个标志,在测试环境中为false,然后使用controlbus组件手动启动它。

 <route id="startDormantRoutes">
        <from uri="timer://runOnce?repeatCount=1&amp;delay=2000" />
        <setProperty propertyName="isCacheEnabled">
            <simple>${bean:myUtils?method=isCacheEnabled}</simple>
        </setProperty>
        <log loggingLevel="DEBUG" logName="IFATMSTrace" message="Cache Enabled : ${property.isCacheEnabled}" />
        <choice>
            <when>
                <simple>${property.isCacheEnabled} == "true"</simple>
                <to uri="controlbus:route?routeId=xcache&amp;action=start" />
            </when>
        </choice>
    </route>

这应该解决了我的问题,但是在关闭camelcontext期间会发生什么,我得到一个NullPointerException

pool-1-thread-16] SpringCamelContext WARN  Error occurred while shutting down service: org.apache.camel.impl.RouteService@63442e11. This exception will be ignored.

java.lang.NullPointerException
    at org.apache.camel.component.cache.CacheConsumer.doStop(CacheConsumer.java:47)[camel-cache-2.12.0.redhat-610379.jar:2.12.0.redhat-610379]
    at org.apache.camel.support.ServiceSupport.stop(ServiceSupport.java:102)[camel-core-2.12.0.redhat-610379.jar:2.12.0.redhat-610379]

并且测试用例进一步全部失败,除了已定义xCache或某些此类事情。

在进一步调查中,我发现即使根本没有加载xcache路由,也会加载一些与路由相关的子服务

[               pool-1-thread-1] SpringCamelContext             DEBUG Using ComponentResolver: org.apache.camel.impl.DefaultComponentResolver@4de49608 to resolve component with name: cache
[               pool-1-thread-1] DefaultComponentResolver       DEBUG Found component: cache in registry: null
[               pool-1-thread-1] DefaultComponentResolver       DEBUG Found component: cache via type: org.apache.camel.component.cache.CacheComponent via: META-INF/services/org/apache/camel/component/cache
[               pool-1-thread-1] DefaultManagementAgent         DEBUG Registered MBean with ObjectName: org.apache.camel:context=myAdapterCamelContext,type=components,name="cache"
[               pool-1-thread-1] DefaultComponent               DEBUG Creating endpoint uri=[cache://xCache], path=[xCache]
[               pool-1-thread-1] SpringCamelContext             DEBUG cache://xCache converted to endpoint: Endpoint[cache://xCache] by component: org.apache.camel.component.cache.CacheComponent@7cc8ff30
[               pool-1-thread-1] DefaultManagementAgent         DEBUG Registered MBean with ObjectName: org.apache.camel:context=myAdapterCamelContext,type=endpoints,name="cache://xCache"
[               pool-1-thread-1] DefaultChannel                 DEBUG Initialize channel for target: 'To[cache://xCache]'

1 个答案:

答案 0 :(得分:1)

我将此作为答案添加:

问题是camel会扫描所有端点并初始化它找到的任何组件。

根据我的经验,如果你需要阻止一个组件初始化(可能它做了一些繁重的初始化,或占用一个端口,或者出于任何原因),我要么:

  1. 参数化组件端点以在测试中“隐藏”来自camel的组件。
  2. 或者,在您的测试中,注册组件的自定义“模拟”版本。
  3. 或者最后你可以考虑使用完全不同的上下文,然后启动你需要的上下文。
  4. 也许有一种更简单的方法可以做到这一点,但这是我的经历。

    编辑:克劳斯易卜生提出了一种简单的方法来模拟组件,为什么不试试这个:

    How to mock multiple components in camel unit test?