Spring JAX-WS timeout

When building up a reliable predictable solution you need to manage a “response time” as one of the core principles. This is accomplished by implementing time out policy. You don’t wanna clients hanging on connection forever. Nowadays very common approach to integration is taking advantage of Spring framework.

Spring JAX-WS web service proxies (JaxWsPortProxyFactoryBean) doesn’t offer a direct possibility to set a service timeout via one of their properties. Following lines documents one of the possibilities how to cope with that requirement.

Java implementation:

public class AbstractJaxWsPortProxyFactoryBean extends JaxWsPortProxyFactoryBean {

    public static final int CONNECT_TIMEOUT = 2500;

    public void setTimeout(final int timeout) {
        // JAX WS
        addCustomProperty("com.sun.xml.ws.connect.timeout", CONNECT_TIMEOUT);
        addCustomProperty("com.sun.xml.ws.request.timeout", timeout);
        // Sun JAX WS
        addCustomProperty("com.sun.xml.internal.ws.connect.timeout", CONNECT_TIMEOUT);
        addCustomProperty("com.sun.xml.internal.ws.request.timeout", timeout);
    }

    @Override
    public void afterPropertiesSet() {
        super.afterPropertiesSet();
    }
}

Spring configuration:

     <bean id="abstractWsClient" class="AbstractJaxWsPortProxyFactoryBean" abstract="true">
        <property name="timeout" value="10000"/>
    </bean>

    <bean id="LocalServiceBinding" parent="abstractWsClient">
        <description>Client</description>
        <property name="serviceInterface" value="cz.Sample.Example.ServiceInterface"/>
        <property name="wsdlDocumentUrl" value="classpath:ServiceInterface.wsdl"/>

        <property name="namespaceUri" value="http://cz/Sample/Example/ServiceInterface"/>

        <property name="serviceName" value="ServiceInterface"/>
        <property name="endpointAddress" value="${localservice.url}"/>
        <property name="timeout" value="${localservice.timeout}"/>
        <property name="username" value="${localservice.user}"/>
        <property name="password" value="${localservice.password}"/>
    </bean>

Advertisements