1 回答
TA贡献1876条经验 获得超6个赞
请注意,我不是自由开关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;
}
}
最后,我使用创建的池来获取客户端 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);
}
}
}
我没有测试(也是因为我不能)但它应该有效。无论如何,我祈祷您正确检查配置。我不知道是否可以始终创建 Client 对象并进行连接,或者当您要发送命令时连接是否更好
我希望它能有用
编辑信息
对不起,我早点犯了一个错误。您必须将客户端返回到池我更新了我的 FreeSwitchEslCommandSender 类
添加回答
举报