java esl连接freeswitch(三)之多台freeswitch切换下

根据java esl连接freeswitch(三)之多台freeswitch切换上 我们知道了几个核心类,那我们如何用esl去监听和匹配freeswitch呢

 

第三分部 多台fs自动切换下部分


1.思路

     可以把数据库里的fs先全部查询,用map做缓存,在一一比较


2.这里给出核心代码

/**
 * @author zzyfs event socket service
 * 此Service类 socket连接需要跟数据库打交道绑定
 * 发出esl命令
 */
@Service 
@Transactional
public class FsEventSocketAPIService {


private final static Logger log = Logger.getLogger(FsEventSocketAPIService.class);

@Autowired
private FsEventSocketDAO fsEventSocketDAO;//dao连接fs数据库
@Autowired
private  TopicSender topicSender;//mq发送
 

/**
* 发出命令
* @param command 命令
*/
@MethodDesc(description="发出命令")
public   String comandString(String hostName,String command) throws Exception{
String strList = null;
if(StringUtils.isNotBlank(hostName)){//如果指定hostName语音服务器
strList = singleFS(hostName,command);
}else{
List<FsEventSocket> sockets = fsEventSocketDAO.findAll();//循环所有的
strList = multipleFS(sockets,hostName,command);

} //end else

return strList;
 
}



//多个fs中任选一个fs最少的执行
private String multipleFS(List<FsEventSocket> sockets, String hostName, String command) {
String strList = null;
//存储fs所有可用的语音服务器
TreeSet<StatusSocket> socketTrees = new TreeSet<StatusSocket>(new StatusSocketComparator());
for(FsEventSocket socket:sockets){
String host_vo = socket.getHostname();
Integer enable = socket.getEnable();
if(enable==FsEventSocket.DISABLED){
log.info("指定hostName,状态无效:"+host_vo);
continue;
}
log.info("没有指定hostName,需要探测探测hostname:"+host_vo);
try {

StatusSocket statusSocket = checkSocket(socket,command,socketTrees);
log.info("探测statusSocket状态:"+statusSocket);
/*if(statusSocket.getResult()){//启动成功
strList = statusSocket.getStrList();
log.info("探测hostname成功:"+host_vo);
break;
} */
} catch (Exception e) {
 strList = e.getMessage();
 log.info("探测hostname失败:"+strList);
 continue;
}
 

}//end for

//需求已改
//获取服务器可容许的最低请求格式的服务器
System.out.println(socketTrees.size());
StatusSocket statusSocket = socketTrees.first();
if(statusSocket!=null){
String finalHostName = statusSocket.getIp();
log.info("最终执行的hostname:"+finalHostName);
ESLClient eSLClient = ESLClientMap.get(finalHostName);
strList = finalHostName+"  "+eSLClient.comandString(command);
}
//情况fs存储器缓存
socketTrees.clear();
socketTrees =null;
return strList;
}




//单个指定fs执行
private String singleFS(String hostName, String command) {
String strList = null;
FsEventSocket socket = fsEventSocketDAO.findByHostName(hostName);
if(socket==null){
strList = "ip地址指定错误...";
return strList;
}
Integer enable = socket.getEnable();
if(enable==FsEventSocket.DISABLED){
strList = "指定hostName,状态无效:"+hostName;
return strList;
}
StatusSocket statusSocket = null;
try {
statusSocket = checkSocket(socket,command,null);
strList = statusSocket.getStrList();
} catch (Exception e) {
 strList = e.getMessage();
 log.info("探测hostname失败:"+strList);
}

return strList;
}






/**
* 发出命令
* @param hostName esl名字
* @param command 命令
* @param socketTrees 存储fs语音服务器 当socketTrees==null表示不存储立即执行命令,socketTrees!=null表示不执行命令存在fs
*/
private   StatusSocket checkSocket(FsEventSocket instance,String command,Set<StatusSocket> socketTrees) throws Exception{

String strList = null;
   boolean result = false;
   StatusSocket statusSocket = new StatusSocket();
   statusSocket.setShowcalls(instance.getShowCalls());
   String hostName = instance.getHostname();
try {
ESLClient eSLClient = ESLClientMap.get(hostName);
if(eSLClient!=null&&eSLClient.isConnect()&&eSLClient.canSend()){
boolean canUse = statusSocket.canUseSocket(eSLClient);
if(canUse){
if(socketTrees==null){//立即执行命令
strList = eSLClient.comandString(command);
result = true;

}else{//socketTrees!=null表示不执行命令存在fs
strList = "暂缓";
result = true;
socketTrees.add(statusSocket);
}//end socketTrees==null

}else{
strList = " 【改语音服务器通话话能力超标】";
log.info(hostName+""+strList);
result =false;
}//end canuse
}else{
//清除该缓存
ESLClientMap.remove(hostName);
strList = hostName+"对应的esl socket配置错误,请检查是否开启";
log.info("comandString:"+strList);
//重新自动启动
boolean ok = this.startSocket(instance);
 
if(ok){
eSLClient = ESLClientMap.get(hostName);
boolean canUse = statusSocket.canUseSocket(eSLClient);
if(canUse){

if(socketTrees==null){//立即执行命令
strList = eSLClient.comandString(command);
log.info("重新自动启动成功:"+strList);
result = true;

}else{//socketTrees!=null表示不执行命令存在fs
strList = "暂缓";
result = true;
socketTrees.add(statusSocket);
}//end socketTrees==null
}else{
strList = "【改语音服务器通话话能力超标】";
log.info("重新自动启动成功:但是"+strList);
result =false;
}//end canuse

}else{
log.info("重新自动启动失败:"+strList);
result =false;
}
}// end eSLClient!=null
} catch (Exception e) {
 
strList = e.getMessage();
result = false;
//throw e;
}
statusSocket.setResult(result);
statusSocket.setStrList(strList);
statusSocket.setIp(hostName);
return statusSocket;
}







/**
* 数据库连接运行   
* @param bean
*/
@MethodDesc(description="连接fs ")
boolean startSocket(FsEventSocket instance){
String result = "";
boolean isStart = false; 
try {

ESLClient eSLClient = ESLClientMap.get(instance.getListenIp());
if(eSLClient==null){
ESLClient e = new ESLClient(instance.getListenIp(),instance.getListenPort(),instance.getPassword());
e.setTopicSender(topicSender);
e.connect();
boolean isConnect = e.isConnect();

if(isConnect){//如果连接成功
 
log.info("连接成功:"+instance.getHostname());

ESLClientMap.add(e);
instance.setConnected(FsEventSocket.START_CONNECTED);
result="运行中";
isStart = true;
}else{
instance.setConnected(FsEventSocket.NOT_CONNECTED);
ESLClientMap.remove(instance.getListenIp());
result="ip或者端口或者密码配置错误";
isStart = false;
}
}else{
instance.setConnected(FsEventSocket.START_CONNECTED);
result="已运行";
isStart = true;
}
} catch (Exception e) {
e.printStackTrace();
result = e.getMessage();
instance.setConnected(FsEventSocket.NOT_CONNECTED);
ESLClientMap.remove(instance.getListenIp());
}
instance.setUpdatetime(DateUtils.getCurrentTime());
instance.setConnectedResult(result);
fsEventSocketDAO.merge(instance);
return isStart ;
}



 
}

以上可以看注释,具体不再分析




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值