Skip to content

Thread & connection usage

Petros Pissias edited this page Jul 19, 2018 · 1 revision

This topic addresses how threads & TCP connections are used on the generated code by xsrpcj

On the client side

each instance of a service implementation, for example:

PersonsClientService serverRef = new PersonsClientServiceImpl(serverHost, cbHandler);

(see the full example source code)

will use 1 TCP connection with the server. On each interaction (on each method call) it will check if it has a valid connection and it not it will try to create one, throwing a RemoteCommunicationsException if it cannot.

Each instance will also use one thread that is reading data out of the TCP connection.

There is a default timeout of 60 seconds that deals with error cases, for example if a server crashes before it sends back a reply or if a server misbehaves and does not send back a reply at all. In these rare cases the client will throw a RemoteCommunicationsException after 60 seconds.

On the server side

when we start the server, for example: new PersonsServer(serviceImplementation).start(); a new thread is listening for connections and will create a new client data handler to handle all incoming data from the client on each new connection. Each new client handler creates a new thread.

Scaling

If you require multiple parallel communications with a server then you must create multiple instances of the Client Service Implementation. Each instance will create a new connection with the server and a new thread for reading out data from this connection. This gives the required flexibility in order to balance consumed resources with the required performance.

If you have a scenario where you have many (many: as many thousands) of clients connected to a server, then xsrpcj would need to be extended in order to support a fixed number of threads that read out data from incoming connections.

If on the other hand you have clients in the order of hundreds, xsrpcj, as is, will do the job for you very efficiently.