猿问

如何为第三方应用程序创建连接池/仅创建一次?

我正在使用JAVA / Spring MVC,当我尝试多次连接它时,我需要在我的应用程序中为第三方应用程序集成制作一个连接池,我的应用程序和服务器系统使用100%RAM。


在这里,我必须有问题,当用户开始多次点击特定方法()时,我的堆内存(RAM空间)增加并成为100%,应用程序将缓慢地使用它多次连接第三方应用程序?在这里,我只需要创建一次并多次获取它。我的连接喜欢,callGenerationService()connection


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();

}

}

这里的“ClickToCall”是一个@Component Bean/POJO类,具有变量设置器和 getters。


有没有,我们如何创建一个用于上述连接,我只连接一次并多次点击并利用更少的RAM?提前致谢。connection (either pool or only once connect)clickToCall.Command1 and clickToCall.Command2


蛊毒传说
浏览 76回答 1
1回答

HUX布斯

请注意,我不是自由开关ESL的专家,所以你必须正确检查代码。无论如何,这就是我要做的。首先,我为客户创建一个工厂public class FreeSwitchEslClientFactory extends BasePooledObjectFactory<Client> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public Client create() throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; //Create and connect: NOTE I'M NOT AN EXPERT OF ESL FREESWITCH SO YOU MUST CHECK IT PROPERLY&nbsp; &nbsp; &nbsp; &nbsp; Client client = new Client();&nbsp; &nbsp; &nbsp; &nbsp; client.connect("127.0.0.1", 8021 , "password", 10);&nbsp; &nbsp; &nbsp; &nbsp; client.setEventSubscriptions("plain", "all");&nbsp; &nbsp; &nbsp; &nbsp; return client;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public PooledObject<Client> wrap(Client obj) {&nbsp; &nbsp; &nbsp; &nbsp; return new DefaultPooledObject<Client>(obj);&nbsp; &nbsp; }}然后我创建一个可共享的:GenericObjectPool@Configuration@ComponentScan(basePackages= {"it.olgna.spring.pool"})public class CommonPoolConfig {&nbsp; &nbsp; @Bean("clientPool")&nbsp; &nbsp; public GenericObjectPool<Client> clientPool(){&nbsp; &nbsp; &nbsp; &nbsp; GenericObjectPool<Client> result = new GenericObjectPool<Client>(new FreeSwitchEslClientFactory());&nbsp; &nbsp; &nbsp; &nbsp; //Pool config e.g. max pool dimension&nbsp; &nbsp; &nbsp; &nbsp; result.setMaxTotal(20);&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }}最后,我使用创建的池来获取客户端 obj:@Componentpublic class FreeSwitchEslCommandSender {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; @Qualifier("clientPool")&nbsp; &nbsp; private GenericObjectPool<Client> pool;&nbsp; &nbsp; public void sendCommand(String command, String param) throws Exception{&nbsp; &nbsp; &nbsp; &nbsp; Client client = null;&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; client = pool.borrowObject();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; client.sendSyncApiCommand(command, param);&nbsp; &nbsp; &nbsp; &nbsp; } finally {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( client != null ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; client.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pool.returnObject(client);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}我没有测试(也是因为我不能)但它应该有效。无论如何,我祈祷您正确检查配置。我不知道是否可以始终创建 Client 对象并进行连接,或者当您要发送命令时连接是否更好我希望它能有用编辑信息对不起,我早点犯了一个错误。您必须将客户端返回到池我更新了我的 FreeSwitchEslCommandSender 类
随时随地看视频慕课网APP

相关分类

Java
我要回答