Hans Vermeiren
Hans Vermeiren

Reputation: 13

How to reduce the timeout of HTTP requests send by odata4j

I'm trying to use the odata4j library in a mobile Android app. Sometimes a HTTP request doesn't get a response because the device lost it's mobile connection just after sending the request. After a few attempts to reproduce it and timing it, I noticed it takes roughly 27 minutes for the library to throw a "java.net.SocketException: Connection timed out". Is there any way to reduce this timeout, so I can take care and handle the failure more quickly? Thanks.

Upvotes: 1

Views: 1031

Answers (2)

Sergii Pechenizkyi
Sergii Pechenizkyi

Reputation: 22232

I would like to provide code snippet for @John Spurlock answer. By analogy of this code http://code.google.com/p/odata4j/source/browse/odata4j-jersey/src/main/java/org/odata4j/jersey/consumer/behaviors/AllowSelfSignedCertsBehavior.java :

private static ODataJerseyConsumer createOdataConsumer(final String odataServerUrl) {

    Builder builder = ODataJerseyConsumer.newBuilder(odataServerUrl);
    builder.setClientBehaviors(TimeoutBehavior.reduceTimeout());
    return builder.build();
}

private enum TimeoutBehavior implements JerseyClientBehavior {
    INSTANCE;

    @Override
    public ODataClientRequest transform(final ODataClientRequest request) {

        return request;
    }

    @Override
    public void modify(final ClientConfig clientConfig) {

        Map<String, Object> properties = clientConfig.getProperties();
        properties.put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, 10 * 1000);
        properties.put(ClientConfig.PROPERTY_READ_TIMEOUT, 10 * 1000);
    }

    @Override
    public void modifyClientFilters(final Filterable filterable) {}

    @Override
    public void modifyWebResourceFilters(final Filterable filterable) {}

    public static OClientBehavior reduceTimeout() {

        return TimeoutBehavior.INSTANCE;
    }
}

Upvotes: 3

John Spurlock
John Spurlock

Reputation: 1723

Use the jersey consumer implementation (which is the default). Add a JerseyClientBehavior [1] when you construct your consumer. This allows you to modify the ClientConfig [2] - you can modify both the connect + read timeout.

[1] http://code.google.com/p/odata4j/source/browse/odata4j-jersey/src/main/java/org/odata4j/jersey/consumer/behaviors/JerseyClientBehavior.java

[2] http://jersey.java.net/nonav/apidocs/1.1.5.1/jersey/com/sun/jersey/api/client/config/ClientConfig.html

Upvotes: 3

Related Questions