【问题标题】:Apache Camel tesing. Remove RoutePolicyApache Camel 测试。删除 RoutePolicy
【发布时间】:2015-04-29 14:29:20
【问题描述】:

这是 Apache Camel 路线:

ZooKeeperRoutePolicy routePolicy = new ZooKeeperRoutePolicy("zookeeper:localhost:2181/fuse-example/routePolicy", 1);
from("file:camelInpit").routeId("systemARoute")
                .routePolicy(routePolicy)
                .log(LoggingLevel.ERROR, "Starting route")
                [...]

我想在测试中删除 routePolicy,因为测试环境中没有 ZooKeeper,但这并不像看起来那么容易

    context.getRouteDefinition("systemARoute").adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            replaceFromWith("direct:aaa");
            weaveByType(RouteDefinition.class).selectIndex(1).remove();
        }
    });

weaveById("policy") 和设置 id routePolicy(...).id("policy") 没有帮助。

如何在测试时动态删除RoutePolicies

【问题讨论】:

    标签: java apache-camel apache-zookeeper


    【解决方案1】:

    难道就不能这样吗?

    from("file:camelInpit").routeId("systemARoute")
                    .choice()
                      .when(prodEnvironmentExpression)
                        .routePolicy(routePolicy)
                      .endChoice()
                    .end()
                    .log(LoggingLevel.ERROR, "Starting route")
    

    【讨论】:

    • 我正在使用RouteBuilder。是否可以将测试中的参数传递给RouteBuilders?
    • 对不起,我不太明白你的意思。
    • 据我了解,prodEnvironmentExpression 应该是一些在测试开始时自动启用的配置参数。如何将测试中的参数传递给路由以使此表达式评估为真?
    • 如果您有一个 Web 应用程序,例如,您可以使用 vm 参数或环境变量作为 prod/test 环境。以 PredicateBuilder 为例,您可以创建来自 RouteBuilder 外部的“正常”值的表达式。
    • 我在 OSGI 容器中运行这个应用程序,所以 JVM 参数使用起来不是很愉快的解决方案
    【解决方案2】:

    您可以访问原始路由并将其路由策略设置为空

        context.getRouteDefinition("systemARoute").adviceWith(context, new AdviceWithRouteBuilder() {
            @Override
            public void configure() throws Exception {
                getOriginalRoute().setRoutePolicies(null);
            }
        });
    

    但我们或许应该为此添加流利的 DSL 构建器以使其脱颖而出?

    【讨论】:

      【解决方案3】:

      如果您将其绑定到上下文,您可以通过使用轻松地在测试中模拟策略,其中 myPolicy 是模拟或不执行任何操作的策略。

      如果你创建一个抽象的 MyCamelTestSupport 来覆盖它,然后所有需要模拟它的测试都扩展 MyCamelTestSupport,那就更容易了

      @Override
      protected JndiRegistry createRegistry() throws Exception {
          JndiRegistry jndi = super.createRegistry();
          jndi.bind("myPolicy", myPolicy);
          return jndi;
      }
      

      【讨论】:

      • ZooKeeperRoutePolicy在生产环境中如何创建?如您所见,目前我在代码中创建了它。我应该使用camelContext.getRegistry() 并将我的策略绑定到 Camel JNDI 注册表吗?
      • 取决于你如何创建它,作为一个独立的,它将是 main.bind() 并且如果要使用 ServletContextListener 你需要创建一个 SimpleRegistry (put) 或 InitialContext (bind) 和创建一个新的 DefaultCamelContext(arg),其中 arg 是 SimpleRegistry 或 InitialContext。
      • 我正在使用蓝图在 Apache Karaf OSGI 容器中运行 Camel。
      • 可能其他bean的创建方式一样,
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2017-03-25
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      相关资源
      最近更新 更多