Creating a Socket Client Pool in Java

Advertisements

Some time ago I needed to create a pool of Socket connections for a customer. First thing I tried to do was to google it and find a library that already provided what I needed. Unfortunately I wasn´t able to find one that fits with my needs. Therefore I had to create an artifact that resolved that issue at the moment.

What do I need to create a Socket Pool ?

This Socket Pool has only 4 classes (really 5, but 1 is only an custom exception).

  • Socket Client
  • SocketPool and SocketFactory from Apache Commons Pool
  • Socket Helper class

Keep in mind that in order to create a socket pool, the server side doesn´t have close that connection when finishes a message. The goal of a socket pool is to keep a bunch of active connections waiting to be used, therefore the server must not close connections, otherwise, the socket pool doesn´t make sense.

Socket Client Class

Nothing special here! Just a class that creates a Socket and provides methods to send a message and close it.

Java

Apache Pool

Of course, I can implement something as simple as a class with a Collection of Socket Clients and provide a couple of methods to get and return connections (socket clients) in that collection.

But for applications in the real world, with a hundred/thousands of request per minute something like that wouldn´t be scalable and might have performance issues. Therefore, I decided to use Apache Commons Pool, from the Apache Commons project as the base to build it.

A Helper Class to Interact

Java

How to use it?

You can initialize a new pool by using:

Java

The above line is going to initilizate a new pool or return and existent pool. Then you can use the methods of the helper to perform actions on the pool.

Advertisements

Getting a new Socket Client from the pool.

When you want to interact with a Socket server endpoint you will use a SocketClient object from the pool. To obtaing one of them you just need to use:

Java

When you finished using a Socket client instance you can return it back to the pool by using

Java

Shutting down a Socket Pool

When you decided to shutdown a pool just use the following line:

Java

That line is going to shutdown the pool that was initiliazed or gotten on that instance.

Customizing Configurations

That’s done by using the GenericObjectPoolConfig class from Apache Commons Pool2.

Java

For example, it’s possible to customize the minimum, maximum, initial amount of instances on the pool.

Where is the code?

You can access the complete code I created at my github.
https//github.com/AdamGamboa/socket-pool

Advertisements

6 comments

  1. Hi,

    I used this code in my application.

    I found a problem after using this pool after certain time and it happens randomly. The pool doesn’t increase based on the request that comes to it. It stay stable creating one pool only and keep requests in the queue instead opening socket connection to server it.

    I used following settings
    setMinEvictableIdleDuration(Duration.ofSeconds(120))
    setTimeBetweenEvictionRuns(Duration.ofSeconds(60))
    setTestWhileIdle(true)
    setMaxTotal(30)
    setMaxIdle(5)

    Thanks
    Masuqur

Leave a Reply

Your email address will not be published. Required fields are marked *