Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson JSON how to set http connection and read timeout

(jersey-common=2.21.0, jackson-core=2.6.1)

How do I set http connection timeouts (connect, read) if createParser(URL url) was invoked? What is the default values used?

    JsonFactory jsonF = new JsonFactory();
    jsonF.enable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
    JsonParser jsonP = jsonF.createParser(url); // URL instance
    try {
        JsonToken token;
        while ( (token=jsonP.nextToken()) != null) {
           if (token == JsonToken.START_OBJECT)
           ..rest "json sax" parser code...
        }
    } finally {
        jsonP.close();
    }

I have few times a week recurring problem when webapp stops reading json sources, task is autorun every 30 minutes. I suspect this http call stalls and starts piling up until JVM goes down.

Should I not use createParser(URL) function in production apps?

like image 258
Whome Avatar asked Dec 08 '15 21:12

Whome


People also ask

What is HTTP read timeout?

The HTTP read timeout determines how long the IBM® Watson IoT Platform - Message Gateway Web UI waits before it times out when it is waiting for an HTTP read request to complete. By default, this value is set to 20 seconds.

What is the default HTTP connection timeout?

The default value is 100,000 milliseconds (100 seconds).

What is the difference between connection timeout and socket timeout?

connection timeout — a time period in which a client should establish a connection with a server. socket timeout — a maximum time of inactivity between two data packets when exchanging data with a server.


1 Answers

You could hack the values

System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");

More on the settings can be found on https://docs.oracle.com/javase/7/docs/technotes/guides/net/properties.html

Or you can instead do a proper call and then pass the result into the json parser. I would go with the latter.

So to put it simply while URL is great light weight alternative I suggest switching to apache http client or some high level solution.

like image 172
Mite Mitreski Avatar answered Oct 14 '22 14:10

Mite Mitreski