如何为第三方应用程序创建一次连接 pool/only?
how to create a connection pool/only once for a third party Application?
我正在使用 JAVA/Spring MVC,我需要在我的应用程序中为第三方应用程序集成创建一个连接池,因为当我尝试多次连接它时,我的应用程序和服务器系统使用 100% RAM .
这里我有一个问题,当用户开始多次点击特定方法 (callGenerationService()
) 时,我的堆内存 (RAM space) 增加并变为 100% 并且应用程序变慢因为它多次连接第三方应用程序?在这里我只需要创建一个 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();
}
}
and here 'ClickToCall' is a @Component Bean/POJO Class with variables setters and getters.
我们如何为上面的连接创建一个 connection (either pool or only once connect)
,我只连接一次并点击 clickToCall.Command1 and 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 对象并连接是否可以,或者当你想发送命令时连接是否更好
希望有用
编辑信息
对不起,我之前犯了一个错误。您必须 return 客户端到池中
我更新了我的 FreeSwitchEslCommandSender class
安杰洛
我正在使用 JAVA/Spring MVC,我需要在我的应用程序中为第三方应用程序集成创建一个连接池,因为当我尝试多次连接它时,我的应用程序和服务器系统使用 100% RAM .
这里我有一个问题,当用户开始多次点击特定方法 (callGenerationService()
) 时,我的堆内存 (RAM space) 增加并变为 100% 并且应用程序变慢因为它多次连接第三方应用程序?在这里我只需要创建一个 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();
}
}
and here 'ClickToCall' is a @Component Bean/POJO Class with variables setters and getters.
我们如何为上面的连接创建一个 connection (either pool or only once connect)
,我只连接一次并点击 clickToCall.Command1 and 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 对象并连接是否可以,或者当你想发送命令时连接是否更好
希望有用
编辑信息
对不起,我之前犯了一个错误。您必须 return 客户端到池中 我更新了我的 FreeSwitchEslCommandSender class
安杰洛