R
R
Renat Iliev2015-04-27 13:52:29
Java
Renat Iliev, 2015-04-27 13:52:29

How to use Keep-Alive in Java?

The program sends different POST requests to the same server from different threads, for example:
Thread1 sends a request to example.com/example with data key=123&value=321
Thread2 sends a request to example.com/directory with data token=qwerty&user=test
...
I'm not happy with the fact that most of the time the program connects to example.com and I want to try using Keep-Alive to speed up this process. How to do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sasha, 2015-04-27
@madmages

There are rumors that keep alive works within one thread (one open socket on the server) and this time of keeping in alive it depends on the server. if you often send requests, then you stupidly need an implementation of the http protocol with support for the keep-alive header (and these are the basics). I mean, if you just have two requests in a row coming from two different threads, nothing will work. If you often send requests, then the library that you have used to work with http most likely already works keep alive

V
vlad20012, 2015-04-27
@vlad20012

It is generally recommended to use apache http client .
Not sure if it's all about Keep-Alive. The Apache http client is simply faster than the HttpUrlConnection from the standard library. Here, for example, is an instruction from Oracle on how to make Keep-Alive work. Nothing special is written there, so it should work by default.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question