Łukasz
Łukasz

Reputation: 2172

Netty - how is it asynchronous?

Let's imagine there is a server, that when receives a request with a car model queries all known car dealers looking for the cheapest one and responds back with the price (using whatever protocol). This actions takes a while. In a casual blocking request/response server model, I do

request = "audi a8" // prepare a request and one line after have the response
response = server.findCheapestCar(request) // takes 20 seconds

I don't want to block my client main thread for 20 seconds, so would rather want it to be executed asynchronously. My understanding for something being asynchronous is that I can pass some sort of an object to it and carry one with my work. Once the server is ready with the response it will notify the object I passed -> Casual callback pattern.

This approach would require library match - both client ad the server need to know the object. But I want my asynchronous server built on Netty to be able to handle requests from various clients (C++/Python and others).

Where is the asynchronousness of netty coming from? What do I need on the client side to benefit from the asynchronousness?

Upvotes: 0

Views: 681

Answers (1)

Ingrim4
Ingrim4

Reputation: 331

Where is the asynchronousness of netty coming from?

Netty adopted the principle of eventloops which you may known from a language like JavaScript. This allows netty to work fully asynchronous. (For more information about eventloops and the basic underlying principle I would recommend this video about the evenloop in JavaScript)

What do I need on the client side to benefit from the asynchronousness?

  1. Client sends request (containing payload and request id = clientside incrementing integer)
  2. Server process the request for 50sec
  3. Server sends response (containing the payload and the same request id the client send in his request)
  4. Client receives the response and looks up the request id (If the client is able to find the request id and its underlying callback it will invoke it)

Hope that helped

Upvotes: 1

Related Questions