Jackson JSON how to establish http connection and read timeout

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

How to set HTTP timeouts (connect, read) if createParser (url url) was called? What are the default values?

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 a recurring problem several times a week, when webapp stops reading json sources, the autorun task runs every 30 minutes. I suspect that this http call stops and starts to accumulate until the JVM drops.

Should the createParser function (URL) be used in production applications?

+5
source share
2 answers

You can hack values

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

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

Or you can instead make the correct call and then pass the result to the json parser. I will go with the latter.

So, just when the url is an easy alternative, I suggest switching to apache http or some high-level solution.

+3
source

This reading method is intended for convenience, but is not customizable and, as a rule, is not suitable for most uses.

Instead, you can get the InputStream endpoints from the URL using other means, then pass it to ObjectMapper / ObjectReader ; this allows full control of connection details, timeouts, etc. It allows you to use other HTTP clients than the one provided by default in the JDK.

+3
source

Source: https://habr.com/ru/post/1237692/


All Articles