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

标签 java spring spring-mvc connection-pooling

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

这里我有一个问题,当用户开始多次点击特定方法(callGenerationService())时,我的堆内存(RAM 空间)会增加并变为 100%,并且应用程序会变慢,因为它多次连接第三方应用程序?在这里,我只需要创建一次连接并多次获取它。我的连接在哪里,

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.

有吗,我们如何为上述连接创建一个连接(池或仅一次连接),其中我仅连接一次并点击clickToCall.Command1和clickToCall.Command2 多次并使用更少的 RAM?提前致谢。

最佳答案

请注意,我不是 freeswitch esl 的专家,因此您必须正确检查代码。无论如何,这就是我要做的。

首先,我为客户端创建一个工厂

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

然后我创建一个可共享的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;
    }
}

最后我使用创建的池来获取客户端对象:

@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);
        }
    }
}

我没有测试(也是因为我不能)它,但它应该可以工作。无论如何,我请求您正确检查配置。我不知道总是创建一个 Client 对象并连接是否可以,或者当您想发送命令时连接是否更好

希望对你有用

编辑信息

抱歉,我之前犯了一个错误。您必须将客户端返回到池中 我更新了我的 FreeSwitchEslCommandSender 类

安杰洛

关于java - 如何为第三方应用程序创建连接池/仅一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55236189/

相关文章:

Java NumberFormatException?

java - 使用 Spring Security 的 IP 过滤器

java - Spring MVC 在页面中只提交渲染列表的一个元素

java - 从 html 页面恢复 JAVA-Spring (with thymeleaf) 中的表单

java - 重定向到 View 文件中的外部 URL

spring - 在 freemarker 模板中获取区域设置

java - 如何使用多个 bean 配置文件配置 Spring BeanFactory?

java - 从大型未排序数组中删除重复项并保持顺序

java - 在飞溅 Activity 中按下后退按钮时如何完成其​​他 Activity ?

xml - 为什么 Eclipse 不能解析 spring-dwr 模式?