Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Jetty HTTPClient connection reuse


On Wed, Dec 12, 2018 at 5:13 PM Bruno Konik <bruno.konik@xxxxxxxxxxx> wrote:
> Hello all,
> I am using Jetty HttpClient to post messages to a messaging platform (Messenger). I am observing that sometimes when sending 2 messages one after the other (sent by the same thread just one after the other), the second message arrives on the messaging platform before the first.
> I am using non-blocking method where ‘url’ is the same URL for both send:
> Request request = httpClient.newRequest(url).method(HttpMethod.POST).content(..);
> Request.send(new BufferingResponseListener(1024) {
>                 @Override
>                 public void onComplete(Result result) {…});
> Is this behavior normal ?

It is possible, yes.
You have 2 TCP connections, the 2 requests are sent one per
connection, and on the server it may happen that the second connection
is processed before the first.

> How could I be sure that the first connection created by the first send will be reused by the second send ?

There are 2 ways:

1) Use directly connections (pseudo code):
Promise<Connection> p = ...;
Request r = client.newRequest(...);
p.get().send(r, new BufferingResponseListener...)

2) Configure HttpClient with setMaxConnectionsPerDestination(1).

> If not possible, should I use a mechanism that would queue the requests and send them with the blocking method?

If you don't want to use either solution above, then yes you need to
do it at the application level with queueing, because it is your
application that requires ordering and HttpClient cannot know what
ordering you want.
In some cases having the second request arriving before the first is
an important feature - not for your case though.

Simone Bordet
Developer advice, training, services and support
from the Jetty & CometD experts.

Back to the top