超时无法使用apache.cxf进行同步调用

时间:2015-08-03 22:27:12

标签: web-services soap timeout cxf

我们正在使用Apache CXF v3.1。我正在对基于SOAP的服务器进行API调用。我以前做过同步调用,以下逻辑适用于超时:

    URL url = MyClient.class.getClassLoader().getResource("service.wsdl");

    // create service
    PaymentPortTypeService service = new PaymentPortTypeService(url);

    // Set executor for async response
    service.setExecutor(threadPoolExecutor);

    /*
     * Handlers are used to set the security headers on the SOAP envelope.
     */
    PaymentsHeaderHandlerResolver hResolver = new PaymentsHeaderHandlerResolver(userId, password);
    service.setHandlerResolver(hResolver);

    // create client
    client = service.getPaymentPortTypeSoap11();

    BindingProvider bp = ((BindingProvider) client);
    bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, vBaseUrl);

    org.apache.cxf.endpoint.Client c = ClientProxy.getClient(client);

    ((ClientImpl)c).setSynchronousTimeout(30000);

    AsyncHTTPConduit httpConduit = (AsyncHTTPConduit) c.getConduit();
    httpConduit.getClient().setConnectionTimeout(1000);
    httpConduit.getClient().setReceiveTimeout(30000);
    httpConduit.getClient().setAllowChunking(false);

在我们转移到异步HTTP之后,超时似乎不再起作用了。你能发现我可能做错了吗?

请注意,我们在pom.xml中有以下内容来帮助生成异步接口:

        <plugin>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-codegen-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>generate-sources</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <wsdlOptions>
                            <wsdlOption>
                                <wsdl>${basedir}/src/main/resources/service.wsdl</wsdl>
                                <extraargs>
                                    <extraarg>-asyncMethods</extraarg>
                                </extraargs>
                            </wsdlOption>
                        </wsdlOptions>
                    </configuration>
                    <goals>
                        <goal>wsdl2java</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>xerces</groupId>
                    <artifactId>xercesImpl</artifactId>
                    <version>2.8.1</version>
                </dependency>
            </dependencies>
        </plugin>

1 个答案:

答案 0 :(得分:0)

我相信您不需要使用CXF特定代码,您可以通过绑定属性设置超时:

bp.getRequestContext().put("javax.xml.ws.client.receiveTimeout", 30000);
bp.getRequestContext().put("javax.xml.ws.client.connectionTimeout", 10000);

请注意,javax.xml.ws.client.receiveTimeoutjavax.xml.ws.client.connectionTimeoutBindingProviderProperties.REQUEST_TIMEOUTBindingProviderProperties.CONNECT_TIMEOUT不同