【问题标题】:How to set Camel RESTful client timeout and HTTPClientPolicy如何设置 Camel RESTful 客户端超时和 HTTPClientPolicy
【发布时间】:2016-02-26 11:57:24
【问题描述】:

我们在 Spring 应用程序中使用 Camel 2.14,并利用 Camel CXF-RS 组件 (http://camel.apache.org/cxfrs.html) 在第 3 方服务上生成 RESTful 请求。

当他们的服务器离线并且 Camel 无法获得连接时,它不会超时 30 秒。我们希望能够调整此超时值,但正在努力了解如何做到这一点。有人可以建议吗?

我们可以看到 Camel 本身使用从 HTTPClientPolicy 对象获取的值,该对象上有一个 setConnectionTimeOut .. 但是我们如何获取这个对象呢?

我们能否以编程方式获取 HTTPClientPolicy 对象?或者我们必须在传递给 template.send() 的 Camel URI 中引用它,例如:

template.send("cxfrs://" + url + "/match/" + appId + "/" + reqId?httpClientAPI=true&http.connection.timeout=5000

【问题讨论】:

标签: java apache-camel cxf


【解决方案1】:

如果您想使用 XML 文件配置端点超时,raphaëλ 之前发布的链接是正确的。 但如果您想完全以编程方式完成,您可以使用 cxf 组件的选项之一 (cxfEndpointConfigurer),如 this camel-cxf component unit-test 所示。

在你的情况下,它会是这样的:

template.send("cxfrs://" + url + "/match/" + appId + "/" + "reqId?httpClientAPI=true&cxfEndpointConfigurer=#endpointConfigurer"

那么你需要一个“endpointConfigurer”bean,其配置如下:

@Component("endpointConfigurer")
public class TemplateEndpointConfigurer implements CxfEndpointConfigurer {

    @Override
    public void configure(AbstractWSDLBasedEndpointFactory factoryBean) {
        // Do nothing here
    }

    @Override
    public void configureClient(Client client) {

        final HTTPConduit conduit = (HTTPConduit) client.getConduit();

        final HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setConnectionTimeout(webServiceConnectionTimeout);
        policy.setReceiveTimeout(webServiceReadTimeout);
        policy.setConnection(ConnectionType.CLOSE);
        conduit.setClient(policy);

    }

    @Override
    public void configureServer(Server server) {
        // Do nothing here
    }
}

尽管这个答案来的有点晚,但我希望它可以帮助你完成你的项目。

【讨论】:

  • 这似乎不适用于 cxfrs 端点。您需要设置实现 CxfRsEndpointConfigurer 的 cxfRsEndpointConfigurer 属性。 CxfRsEndpointConfigurer 接口与 CxfEndpointConfigurer 几乎相同,只是 jaxrs 客户端没有 getConduit() 方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-11
  • 1970-01-01
  • 1970-01-01
  • 2011-03-09
  • 2021-06-06
相关资源
最近更新 更多