Sushil Kumar
Sushil Kumar

Reputation: 84

how to create a connection pool/only once for a third party Application?

I'm using JAVA/Spring MVC and I need to make a Connection Pool for a Third Party Application integration in my application becouse when i try to connect it multiple time my application and server System utilize 100% RAM.

here i have to problem, when users start to hit a specific method (callGenerationService()) multiple time, my Heap memory(RAM space) increases and becomes 100% and application going to slow becouse of it connect third party application multiple times ? here i need to create a connection only once and get it multiple times. where my connection like,

public class ClickToCallServiceImpl implements ClickToCallServiceInterface {
Client client = null;
@Override
public ClickToCall callGenerationService(ClickToCall clickToCall) {
     client = new Client();
     client.connect("127.0.0.1", 8021 , "password", 10); //Every time Connection Connect.
     client.setEventSubscriptions("plain", "all");
     // client.sendSyncApiCommand("",""); //here i run command on every hit like.
    client.sendSyncApiCommand(clickToCall.command1, clickToCall.command2);
    client.close();
}
}

and here 'ClickToCall' is a @Component Bean/POJO Class with variables setters and getters.

Is there, how to we create a connection (either pool or only once connect) for above connection where i connect only once and hit clickToCall.Command1 and clickToCall.Command2 multiple times and utilize less RAM? Thanks in advance.

Upvotes: 1

Views: 285

Answers (1)

Angelo Immediata
Angelo Immediata

Reputation: 6944

Please note that I'm not an expert of freeswitch esl so you must check the code properly. Anyway this is what I would do.

First I create a Factory for Client

public class FreeSwitchEslClientFactory extends BasePooledObjectFactory<Client> {

    @Override
    public Client create() throws Exception {
        //Create and connect: NOTE I'M NOT AN EXPERT OF ESL FREESWITCH SO YOU MUST CHECK IT PROPERLY
        Client client = new Client();
        client.connect("127.0.0.1", 8021 , "password", 10);
        client.setEventSubscriptions("plain", "all");
        return client;
    }

    @Override
    public PooledObject<Client> wrap(Client obj) {

        return new DefaultPooledObject<Client>(obj);
    }
}

Then I create a shareable GenericObjectPool:

@Configuration
@ComponentScan(basePackages= {"it.olgna.spring.pool"})
public class CommonPoolConfig {

    @Bean("clientPool")
    public GenericObjectPool<Client> clientPool(){
        GenericObjectPool<Client> result = new GenericObjectPool<Client>(new FreeSwitchEslClientFactory());
        //Pool config e.g. max pool dimension
        result.setMaxTotal(20);
        return result;
    }
}

Finally I use the created pool in order to get the Client obj:

@Component
public class FreeSwitchEslCommandSender {

    @Autowired
    @Qualifier("clientPool")
    private GenericObjectPool<Client> pool;

    public void sendCommand(String command, String param) throws Exception{
        Client client = null;
        try {
            client = pool.borrowObject();
            client.sendSyncApiCommand(command, param);
        } finally {
            if( client != null ) {
                client.close();
            }
            pool.returnObject(client);
        }
    }
}

I didn't test (also because I can't) it but it should work. In any case I pray you to properly check the configuration. I don't know if it's OK to always create a Client object and connect or if it's better to connect when you want to send command

I hope it can be useful

EDIT INFORMATION

Sorry I made an error early. You must return the client to the pool I updated my FreeSwitchEslCommandSender class

Angelo

Upvotes: 1

Related Questions