How to Set Timeout for JAX-WS WebService Call
Asked Answered
K

6

19

I'm working on a WebService Client and I want to set a Timeout for my WebService Call. I have tried different approaches but still I'm not able to achieve this. I'm using JAX-WS for code generation from WSDL. I'm using JBoss-eap-5.1 as App Server and JDK1.6.0_27. I found these diff approaches for setting timeout but none of them is working for me.

URL mbr_service_url = new URL(null,GlobalVars.MemberService_WSDL, new URLStreamHandler() {

            @Override
            protected URLConnection openConnection(URL url) throws IOException {
                URL clone_url = new URL(url.toString());
                HttpURLConnection clone_urlconnection = (HttpURLConnection) clone_url.openConnection();
                // TimeOut settings
                clone_urlconnection.setConnectTimeout(10000);
                clone_urlconnection.setReadTimeout(10000);
                return (clone_urlconnection);
            }
        });
        MemberService service = new MemberService(mbr_service_url);
        MemberPortType soap = service.getMemberPort();
        ObjectFactory factory = new ObjectFactory();
        MemberEligibilityWithEnrollmentSourceRequest request = factory.createMemberEligibilityWithEnrollmentSourceRequest();

        request.setMemberId(GlobalVars.MemberId);
        request.setEligibilityDate(value);

        ((BindingProvider) soap).getRequestContext().put(com.sun.xml.ws.client.BindingProviderProperties.REQUEST_TIMEOUT, 10000);
        ((BindingProvider) soap).getRequestContext().put(com.sun.xml.ws.client.BindingProviderProperties.CONNECT_TIMEOUT, 10000);
        ((BindingProvider) soap).getRequestContext().put(com.sun.xml.internal.ws.client.BindingProviderProperties.REQUEST_TIMEOUT, 10000);
        ((BindingProvider) soap).getRequestContext().put(com.sun.xml.internal.ws.client.BindingProviderProperties.CONNECT_TIMEOUT, 10000);
        ((BindingProvider) soap).getRequestContext().put(com.sun.xml.ws.developer.JAXWSProperties.REQUEST_TIMEOUT, 10000);
        ((BindingProvider) soap).getRequestContext().put(com.sun.xml.ws.developer.JAXWSProperties.CONNECT_TIMEOUT, 10000);
        ((BindingProvider) soap).getRequestContext().put(com.sun.xml.internal.ws.developer.JAXWSProperties.REQUEST_TIMEOUT, 10000);
        ((BindingProvider) soap).getRequestContext().put(com.sun.xml.internal.ws.developer.JAXWSProperties.CONNECT_TIMEOUT, 10000);
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        MemberEligibilityWithEnrollmentSourceResponse response = soap.getMemberEligibilityWithEnrollmentSource(request);
        logger.log("Call to member service finished.");

For now what I have done is, I have called my webservice method from inside an executor. I know its not a good approach, but its working for me. Guys please help me to do it in a proper way.

logger.log("Parameters set for createorUpdateContact call.\nGoing in Executor Service.");
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    response = soap.getMemberEligibilityWithEnrollmentSource(request);
                } catch (MemberServiceException ex) {
                    logger.log("Exception in call to WebService", ex.fillInStackTrace());
                }
            }
        });
        executorService.shutdown();
        try {
            executorService.awaitTermination(GlobalVars.WSCallTimeOut, TimeUnit.SECONDS);
        } catch (InterruptedException ex) {
            logger.log("Thread Interrupted!", ex);
            executorService.shutdownNow();
        }
Killjoy answered 20/12, 2012 at 7:17 Comment(0)
A
24

You could try these settings (they are paired to be used in pairs)

BindingProviderProperties.REQUEST_TIMEOUT
BindingProviderProperties.CONNECT_TIMEOUT

BindingProviderProperties should be from com.sun.xml.internal.WS.client

Or the strings for JBoss:

javax.xml.ws.client.connectionTimeout
javax.xml.ws.client.receiveTimeout

All properties to be put on getRequestContext() in milliseconds.

(BindingProvider)wsPort).getRequestContext().put(BindingProviderProperties.REQUEST_TIMEOUT, yourTimeoutInMillisec);

For JBoss specifically, you might want to use the property StubExt.PROPERTY_CLIENT_TIMEOUT from org.jboss.ws.core.StubExt. See this thread for details.

Aldwon answered 22/12, 2012 at 6:20 Comment(3)
StubExt.PROPERTY_CLIENT_TIMEOUT worked for me, but exception is thrown only after 3*timeoutMillisecond For example: If timeoutMillisecond=3000 than exception is thrown after 9000 millisecond, but Timeout after: 3000ms is written in log fileEnki
Thanks @mariami It worked for me. And exception is being thrown at proper time, not after 3*timeoutMillisecond . Although to make it to work I had to remove some jars from jboss lib related to jax otherwise it was giving NoClassDefFoundError: javax/xml/ws/spi/Provider21.Killjoy
Is there any update for Jakarta specifications as many were changed from javax to jakarta?Tuscan
V
8

Like kolossus said you should use:

com.sun.xml.internal.ws.client.BindingProviderProperties     

And String values are:

com.sun.xml.internal.ws.connect.timeout
com.sun.xml.internal.ws.request.timeout

Although internal packages shouldn't be used, this is the only way if you work with default JDK6. So, in this case setting receive and connect timeout should be done with:

bindingProvider.getRequestContext().put(BindingProviderProperties.REQUEST_TIMEOUT,requestTimeoutMs);

bindingProvider.getRequestContext().put(BindingProviderProperties.CONNECT_TIMEOUT,connectTimeoutMs);

But beware, constant values are different if you are using other JAXWS reference implementation, i.e. JAXWS-RT 2.1.4 BindingProviderProperties:

com.sun.xml.ws.client.BindingProviderProperties

you will have different String values for REQUEST_TIMEOUT and CONNECT_TIMEOUT:

com.sun.xml.ws.request.timeout
com.sun.xml.ws.connect.timeout
Volny answered 23/4, 2013 at 9:5 Comment(1)
Note that the given timeout is expected to be given as Integer!Caravaggio
M
6

For me setting javax.xml.ws.client.connectionTimeout and javax.xml.ws.client.receiveTimeout solved the problem.

((BindingProvider)port).getRequestContext().put("javax.xml.ws.client.connectionTimeout", timeout);
((BindingProvider)port).getRequestContext().put("javax.xml.ws.client.receiveTimeout", timeout);

refer link

Monochromat answered 16/4, 2014 at 4:40 Comment(0)
M
3

Setting the following options works for me. I am using the Metro JAXWS implementation.

((BindingProvider)portType).getRequestContext().put(JAXWSProperties.CONNECT_TIMEOUT, 10000);
((BindingProvider) portType).getRequestContext().put(JAXWSProperties.REQUEST_TIMEOUT, 50000);

portType is the Web Service endpoint interface.

Values of the above fields from the com.sun.xml.internal.ws.developer.JAXWSProperties

public static final java.lang.String CONNECT_TIMEOUT = "com.sun.xml.internal.ws.connect.timeout";
public static final java.lang.String REQUEST_TIMEOUT = "com.sun.xml.internal.ws.request.timeout";
Mealy answered 25/9, 2014 at 21:1 Comment(0)
E
2

Upgrade jbossws-native library and use StubExt.PROPERTY_CLIENT_TIMEOUT

For upgrading jbossws-native, follow this link.

*jbossws-native-3.4.0 is the latest supported version for Jboss 5.1.0GA. You can see JBossWS - Supported Target Containers

This worked for me

Enki answered 25/12, 2012 at 12:11 Comment(0)
H
0

I have a old installation runtime that have this environment: Jdk-1.5, Jboss-4.2.3.GA and the WSClient was created by JAX-WS specification 2.0.

to activate Soap Request Timeout I use the follow code ((BindingProvider)port).getRequestContext().put(org.jboss.ws.core.StubExt.PROPERTY_CLIENT_TIMEOUT, String.valueOf(readTimeout));

and the jar jbossws-client.jar copied in jboss-4.2.3.GA\server\default\lib\

Hoff answered 11/7, 2019 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.