Soap: Set TimeOut using WSDL2Java

假装没事ソ 提交于 2020-04-19 05:51:15

问题


I'm trying to send some data to a SOAP service. Now we have created some classes with WSDL2Java based on a WSDL. Now the call is taking more than 1 minute, while the time out is set (I think) on 1 minute. How can I change the timeout?

Caused by: javax.xml.ws.WebServiceException: Could not send Message.
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:145)
    at com.sun.proxy.$Proxy164.salesOrderProcess(Unknown Source)
    at com.project.b2b.cq.shimanoexportorder.SalesOrderPrepareInterceptor.onPrepare(SalesOrderPrepareInterceptor.java:133)
    at com.project.b2b.cq.shimanoexportorder.SalesOrderPrepareInterceptor.onPrepare(SalesOrderPrepareInterceptor.java:1)
    at de.cq.platform.servicelayer.internal.model.impl.wrapper.ModelWrapper.invokePrepareInterceptors(ModelWrapper.java:251)
    ... 124 more
Caused by: java.net.SocketTimeoutException: SocketTimeoutException invoking {URL}: Read timed out
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.mapException(HTTPConduit.java:1422)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1407)
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:640)
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:519)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:449)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:352)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:304)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:88)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)
    ... 128 more
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:152)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:687)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1323)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1527)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1485)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1393)
    ... 138 more

I already tried the following, but it seems like that hasn't any effect

    final SalesOrderServiceSoap salesOrderServiceSoap = new SalesOrderService(url).getSalesOrderServiceSoap();
    final Map<String, Object> requestContext = ((BindingProvider) salesOrderServiceSoap).getRequestContext();
    requestContext.put(BindingProviderProperties.REQUEST_TIMEOUT, Integer.valueOf(120000)); // Timeout in millis
    requestContext.put(BindingProviderProperties.CONNECT_TIMEOUT, Integer.valueOf(120000)); // Timeout in millis

回答1:


I solved the issue by using the following code:

Import:

    import org.apache.cxf.endpoint.Client;
    import org.apache.cxf.frontend.ClientProxy;
    import org.apache.cxf.transport.http.HTTPConduit;
    import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

Code:

    final SalesOrderServiceSoap salesOrderServiceSoap = new SalesOrderService(url).getSalesOrderServiceSoap(); //The service you need to use
    final Client cl = ClientProxy.getClient(salesOrderServiceSoap);
    final HTTPConduit httpConduit = (HTTPConduit) cl.getConduit();
    final HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();

    httpClientPolicy.setConnectionTimeout(CONNECTION_TIMEOUT); //Time in milliseconds
    httpClientPolicy.setReceiveTimeout(RECEIVE_TIMEOUT); //Time in milliseconds
    httpConduit.setClient(httpClientPolicy);

You will need the following libraries:

    cxf-core-3.0.1.jar
    cxf-rt-frontend-simple-3.0.1.jar
    cxf-rt-transports-http.3.0.1.jar



回答2:


for those who have the following exception:

SEIStub cannot be cast to org.apache.cxf.frontend.ClientProxy

just change the dependency from

cxf-rt-frontend-simple

to

cxf-rt-frontend-jaxws


来源:https://stackoverflow.com/questions/25784402/soap-set-timeout-using-wsdl2java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!