users@saaj.java.net

Q: Setting HTTP client socket timeouts for SAAJ client?

From: Andreas Loew <Andreas.Loew_at_Sun.COM>
Date: Mon, 04 Jun 2007 19:18:56 +0200

All,

I am wondering whether it is possible to set a connect and read timeout
on the HTTP client connection created by SAAJ, i.e. provide values to
the methods

HttpURLConnection#setConnectTimeout(int connectTimeout)
HttpURLConnection#setReadTimeout(int readTimeout)

Looks like this is not supported by the SAAJ implementation...!?

I then tried to register an URLStreamHandlerFactory on my target URL
which extends class

com.sun.xml.messaging.saaj.client.p2p.http.handler.HttpURLConnection

and sets these parameters (see attached code), but again: no luck!

What can I do in order to set these socket timeouts?

Thanks in advance & best regards,

Andreas

-- 
Andreas Loew
Java Architect
Sun Microsystems (Germany)


package com.sun.xml.messaging.saaj.client.p2p.http.handler;

import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;

public class HttpTimeoutHandlerFactory implements URLStreamHandlerFactory {

    protected int connectTimeout = -1;
    protected int readTimeout = -1;
    
    public int getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public int getReadTimeout() {
        return readTimeout;
    }

    public void setReadTimeout(int readTimeout) {
        this.readTimeout = readTimeout;
    }
    
    public HttpTimeoutHandlerFactory() {
    }
    
    public HttpTimeoutHandlerFactory(int connectTimeout, int readTimeout) {
        System.err.println("in Factory constructor");
        this.connectTimeout = connectTimeout;
        this.readTimeout = readTimeout;
    }
    
    public URLStreamHandler createURLStreamHandler(String protocol) {
        System.err.println("in Factory createURLStreamHandler, " + connectTimeout + "/" + readTimeout);
        return new HttpTimeoutHandler(connectTimeout, readTimeout);
    }
    
}


package com.sun.xml.messaging.saaj.client.p2p.http.handler;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class HttpTimeoutHandler extends Handler {
    
    protected int connectTimeout = -1;
    protected int readTimeout = -1;
    
    public HttpTimeoutHandler(String proxy, int port, int connectTimeout, int readTimeout) {
        super(proxy, port);
        System.err.println("in Handler constructor, " + connectTimeout + "/" + readTimeout);
        this.connectTimeout = connectTimeout;
        this.readTimeout = readTimeout;
    }
    
    public HttpTimeoutHandler(int connectTimeout, int readTimeout) {
        super();
        System.err.println("in Handler constructor, " + connectTimeout + "/" + readTimeout);
        this.connectTimeout = connectTimeout;
        this.readTimeout = readTimeout;
    }
    
    public HttpTimeoutHandler(String proxy, int port) {
        super(proxy, port);
    }
    
    public HttpTimeoutHandler() {
        super();
        System.err.println("in Handler no-args constructor");
    }
    
    public URLConnection openConnection(URL u) throws IOException {
        System.err.println("in Handler openConnection, " + connectTimeout + "/" + readTimeout);
        return new HttpTimeoutURLConnection(u, this, connectTimeout, readTimeout);
    }
    
}


package com.sun.xml.messaging.saaj.client.p2p.http.handler;

import java.io.IOException;
import java.net.URL;

public class HttpTimeoutURLConnection extends HttpURLConnection {
    
    protected int connectTimeout = -1;
    protected int readTimeout = -1;
    
    public HttpTimeoutURLConnection(URL u, String host, int port) throws IOException {
        super(u, host, port);
    }
    
    protected HttpTimeoutURLConnection(URL u, Handler handler) throws IOException {
        super(u, handler);
    }
    
    public HttpTimeoutURLConnection(URL u, String host, int port, int connectTimeout, int readTimeout) throws IOException {
        super(u, host, port);
        System.err.println("in HttpTimeoutURLConnection constructor " + connectTimeout + "/" + readTimeout);
        this.connectTimeout = connectTimeout;
        this.readTimeout = readTimeout;
    }
    
    public HttpTimeoutURLConnection(URL u, Handler handler, int connectTimeout, int readTimeout) throws IOException {
        super(u, handler);
        System.err.println("in HttpTimeoutURLConnection constructor " + connectTimeout + "/" + readTimeout);
        this.connectTimeout = connectTimeout;
        this.readTimeout = readTimeout;
    }
    
    public void connect() throws IOException {
        System.err.println("in HttpTimeoutURLConnection connect() " + connectTimeout + "/" + readTimeout);
        super.setConnectTimeout(connectTimeout);
        super.setReadTimeout(readTimeout);
        super.connect();
    }
    
}