如何在骆驼CXF端点上配置超时

时间:2020-09-18 20:21:48

标签: java http apache-camel cxf

我有一个使用apache camel cxf组件开发的肥皂客户端 我们正在调用的客户服务需要太多时间来响应,因此我被要求增加调用的超时时间

我尝试对cxf ToEndPoint使用自定义cxfEndpointConfigurer,如下所示:cxf:// http:// localhost:6025 / MyMockService?cxfEndpointConfigurer =#MyCxfConfigurer&dataFormat = MESSAGE&portName =%7Bhttp%3A%2F%2Forg.tempuri%7DMyServiceSoap11 =%7Bhttp%3A%2F%2Forg.tempuri%7DMyServiceService

这是下面的代码:

公共类TemplateEndpointConfigurer实现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(30000);
    policy.setReceiveTimeout(300000);
    policy.setConnection(ConnectionType.CLOSE);
    conduit.setClient(policy);

}

@Override
public void configureServer(Server server) {
    // TODO Auto-generated method stub

}

}

但是我仍然在60000ms之后收到超时错误,这是cxf的默认值

您是否知道如何设置成功设置此超时时间?

非常感谢您

1 个答案:

答案 0 :(得分:1)

我也遇到了同样的问题,我可以通过如下设置Client.REQUEST_CONTEXT标头来解决此问题:

这可以在调用Web服务之前在路由中定义的处理器/ bean中完成:

    public void setWebServiceTimeout(Exchange exchange) {
      Map<String, Object> requestContext = new HashMap<String, Object>();
      HTTPClientPolicy clientPolicy = new HTTPClientPolicy();
      clientPolicy.setReceiveTimeout(300000);
      requestContext.put(HTTPClientPolicy.class.getName(), clientPolicy);
      exchange.getIn().setHeader(Client.REQUEST_CONTEXT , requestContext);
}