【问题标题】:Have camel build the endpoint uri with a Map让骆驼使用 Map 构建端点 uri
【发布时间】:2019-12-03 14:10:18
【问题描述】:

有没有办法让骆驼使用包含请求参数的Map<String, String> 创建端点的 uri?

我经常遇到这样的情况,即我通过配置获得不同的参数,并且必须在运行时以编程方式构建 uri(之后是静态的)。
目前我总是必须自己构建 uri,这可以找到,但我想知道是否有更清洁的方法。

例如,这是我使用一些配置为 smtp enpoint 构建 uri 的方式(可能不是最简单的代码,但它工作正常):

private String buildSmtpUri() {
  StringBuilder sb = new StringBuilder();
  if (configuration.isEnableSslSecurity()) {
    sb.append("smtps://");
  } else {
    sb.append("smtp://");
  }
  Assert.hasText(configuration.getSmtpUrl(), "No smtp url was given");
  sb.append(configuration.getSmtpUrl());
  if (configuration.getPort() != null) {
    sb.append(":");
    sb.append(configuration.getPort());
  }
  Map<String, String> parameters = new HashMap<>();
  if (StringUtils.isNotEmpty(configuration.getUsername())) {
    parameters.put("username", configuration.getUsername());
  }
  if (StringUtils.isNotEmpty(configuration.getPassword())) {
    parameters.put("password", configuration.getPassword());
  }
  if (configuration.isBodyIsHtml()) {
    parameters.put("contentType", "text/html");
  }

  AtomicBoolean first = new AtomicBoolean(true);
  parameters.forEach((key, value) -> {
    if (first.get()) {
      first.set(false);
      sb.append("?");
    } else {
      sb.append("&");
    }
    sb.append(key)
        .append("=")
        .append(value);
  });

  return sb.toString();
}

如果这不能使用骆驼界面来简化,也许将来它可能是一个很酷的功能?

问候 克里斯

【问题讨论】:

    标签: java apache-camel


    【解决方案1】:

    您可以使用Component#createEndpoint 方法动态构建Endpoint 实例。

    public class MyRouteBuilder extends RouteBuilder {
        @Override
        public void configure() throws Exception{
            from(dynamicEndpoint("seda", "mySeda", Collections.singletonMap("size", "123")))
                    .to("log:Hello from SEDA");
        }
    
        private Endpoint dynamicEndpoint(String component, String name, Map<String, Object> params) throws Exception {
            return getContext().getComponent(component).createEndpoint(name, params);
        }
    }
    

    在 Camel 2.x 中,也有解决方案,虽然不是那么优雅

    private Endpoint dynamicEndpoint(String component, String name, Map<String, Object> params) throws Exception {
        String uri = String.format("%s://%s?%s", component, name, org.apache.camel.util.URISupport.createQueryString(params));
        return getContext().getEndpoint(uri);
    }
    

    【讨论】:

    • 这似乎是解决方案,虽然我无法测试它,因为我们仍然使用 Camel 2,其中只有带有名称的“createEndpoint”,没有参数。但是,一旦我们迁移,我将使用此方法。谢谢
    • @Chris 我已经为 Camel 2 添加了一个代码。它比 Camel 3 方法有点丑,但仍然比你原来的方法短得多。
    • 对于所有想知道 Camel 2.x 代码的人来说,camel 可以使用尾随 '?'没有参数,因此无需检查空参数映射。 @Bedla 感谢代码
    【解决方案2】:

    是的,我认为有更简单的方法。你可以使用simple language 来制作uri。您也可以在simple 表达式之后将它与process(Process process) 中的您自己的进程结合起来。例如,您可以使用simple 设置端点,然后添加process() 端点,您可以在其中更改端点或创建新的String uri,您可以使用处理器中的setHeader() 将其传递给下一个端点。

    【讨论】:

      猜你喜欢
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 2014-12-14
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多