优化----okhttp连接访问超时设置,访问异常code设置为-2
This commit is contained in:
parent
1159cef1ef
commit
5f26e92e77
@ -12,6 +12,7 @@ import org.springframework.stereotype.Component;
|
|||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.ConnectException;
|
import java.net.ConnectException;
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -28,6 +29,9 @@ public class ZLMRESTfulUtils {
|
|||||||
|
|
||||||
private OkHttpClient getClient(){
|
private OkHttpClient getClient(){
|
||||||
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
|
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
|
||||||
|
//todo 暂时写死超时时间 均为5s
|
||||||
|
httpClientBuilder.connectTimeout(5,TimeUnit.SECONDS); //设置连接超时时间
|
||||||
|
httpClientBuilder.readTimeout(5,TimeUnit.SECONDS); //设置读取超时时间
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> {
|
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> {
|
||||||
logger.debug("http请求参数:" + message);
|
logger.debug("http请求参数:" + message);
|
||||||
@ -47,7 +51,10 @@ public class ZLMRESTfulUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String url = String.format("http://%s:%s/index/api/%s", mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api);
|
String url = String.format("http://%s:%s/index/api/%s", mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api);
|
||||||
JSONObject responseJSON = null;
|
JSONObject responseJSON = new JSONObject();
|
||||||
|
//-2自定义流媒体 调用错误码
|
||||||
|
responseJSON.put("code",-2);
|
||||||
|
responseJSON.put("msg","流媒体调用失败");
|
||||||
|
|
||||||
FormBody.Builder builder = new FormBody.Builder();
|
FormBody.Builder builder = new FormBody.Builder();
|
||||||
builder.add("secret",mediaServerItem.getSecret());
|
builder.add("secret",mediaServerItem.getSecret());
|
||||||
@ -78,11 +85,20 @@ public class ZLMRESTfulUtils {
|
|||||||
response.close();
|
response.close();
|
||||||
Objects.requireNonNull(response.body()).close();
|
Objects.requireNonNull(response.body()).close();
|
||||||
}
|
}
|
||||||
} catch (ConnectException e) {
|
|
||||||
logger.error(String.format("连接ZLM失败: %s, %s", e.getCause().getMessage(), e.getMessage()));
|
|
||||||
logger.info("请检查media配置并确认ZLM已启动...");
|
|
||||||
}catch (IOException e) {
|
}catch (IOException e) {
|
||||||
logger.error(String.format("[ %s ]请求失败: %s", url, e.getMessage()));
|
logger.error(String.format("[ %s ]请求失败: %s", url, e.getMessage()));
|
||||||
|
|
||||||
|
if(e instanceof SocketTimeoutException){
|
||||||
|
//读取超时超时异常
|
||||||
|
logger.error(String.format("读取ZLM数据失败: %s, %s", url, e.getMessage()));
|
||||||
|
}
|
||||||
|
if(e instanceof ConnectException){
|
||||||
|
//判断连接异常,我这里是报Failed to connect to 10.7.5.144
|
||||||
|
logger.error(String.format("连接ZLM失败: %s, %s", url, e.getMessage()));
|
||||||
|
}
|
||||||
|
|
||||||
|
}catch (Exception e){
|
||||||
|
logger.error(String.format("访问ZLM失败: %s, %s", url, e.getMessage()));
|
||||||
}
|
}
|
||||||
}else {
|
}else {
|
||||||
client.newCall(request).enqueue(new Callback(){
|
client.newCall(request).enqueue(new Callback(){
|
||||||
@ -105,8 +121,16 @@ public class ZLMRESTfulUtils {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(@NotNull Call call, @NotNull IOException e) {
|
public void onFailure(@NotNull Call call, @NotNull IOException e) {
|
||||||
logger.error(String.format("连接ZLM失败: %s, %s", e.getCause().getMessage(), e.getMessage()));
|
logger.error(String.format("连接ZLM失败: %s, %s", call.request().toString(), e.getMessage()));
|
||||||
logger.info("请检查media配置并确认ZLM已启动...");
|
|
||||||
|
if(e instanceof SocketTimeoutException){
|
||||||
|
//读取超时超时异常
|
||||||
|
logger.error(String.format("读取ZLM数据失败: %s, %s", call.request().toString(), e.getMessage()));
|
||||||
|
}
|
||||||
|
if(e instanceof ConnectException){
|
||||||
|
//判断连接异常,我这里是报Failed to connect to 10.7.5.144
|
||||||
|
logger.error(String.format("连接ZLM失败: %s, %s", call.request().toString(), e.getMessage()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -87,10 +87,15 @@ public class ZLMRTPServerFactory {
|
|||||||
int result = -1;
|
int result = -1;
|
||||||
// 查询此rtp server 是否已经存在
|
// 查询此rtp server 是否已经存在
|
||||||
JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(mediaServerItem, streamId);
|
JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(mediaServerItem, streamId);
|
||||||
if (rtpInfo != null && rtpInfo.getInteger("code") == 0 && rtpInfo.getBoolean("exist")) {
|
if(rtpInfo.getInteger("code") == 0){
|
||||||
result = rtpInfo.getInteger("local_port");
|
if (rtpInfo.getBoolean("exist")) {
|
||||||
|
result = rtpInfo.getInteger("local_port");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}else if(rtpInfo.getInteger("code") == -2){
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, Object> param = new HashMap<>();
|
Map<String, Object> param = new HashMap<>();
|
||||||
// 推流端口设置0则使用随机端口
|
// 推流端口设置0则使用随机端口
|
||||||
param.put("enable_tcp", 1);
|
param.put("enable_tcp", 1);
|
||||||
|
@ -123,7 +123,7 @@ public class PlayServiceImpl implements IPlayService {
|
|||||||
result.onCompletion(()->{
|
result.onCompletion(()->{
|
||||||
// 点播结束时调用截图接口
|
// 点播结束时调用截图接口
|
||||||
// TODO 应该在上流时调用更好,结束也可能是错误结束
|
// TODO 应该在上流时调用更好,结束也可能是错误结束
|
||||||
String path = "snap";
|
String path = "static/static/snap/";
|
||||||
String fileName = deviceId + "_" + channelId + ".jpg";
|
String fileName = deviceId + "_" + channelId + ".jpg";
|
||||||
ResponseEntity responseEntity = (ResponseEntity)result.getResult();
|
ResponseEntity responseEntity = (ResponseEntity)result.getResult();
|
||||||
if (responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK) {
|
if (responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK) {
|
||||||
@ -152,24 +152,33 @@ public class PlayServiceImpl implements IPlayService {
|
|||||||
MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);
|
MediaServerItem mediaInfo = mediaServerService.getOne(mediaServerId);
|
||||||
|
|
||||||
JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(mediaInfo, streamId);
|
JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(mediaInfo, streamId);
|
||||||
if (rtpInfo != null && rtpInfo.getBoolean("exist")) {
|
if(rtpInfo.getInteger("code") == 0){
|
||||||
|
if (rtpInfo.getBoolean("exist")) {
|
||||||
|
|
||||||
WVPResult wvpResult = new WVPResult();
|
WVPResult wvpResult = new WVPResult();
|
||||||
wvpResult.setCode(0);
|
wvpResult.setCode(0);
|
||||||
wvpResult.setMsg("success");
|
wvpResult.setMsg("success");
|
||||||
wvpResult.setData(streamInfo);
|
wvpResult.setData(streamInfo);
|
||||||
msg.setData(wvpResult);
|
msg.setData(wvpResult);
|
||||||
|
|
||||||
resultHolder.invokeAllResult(msg);
|
resultHolder.invokeAllResult(msg);
|
||||||
if (hookEvent != null) {
|
if (hookEvent != null) {
|
||||||
hookEvent.response(mediaServerItem, JSONObject.parseObject(JSON.toJSONString(streamInfo)));
|
hookEvent.response(mediaServerItem, JSONObject.parseObject(JSON.toJSONString(streamInfo)));
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
redisCatchStorage.stopPlay(streamInfo);
|
||||||
|
storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
|
||||||
|
streamInfo = null;
|
||||||
}
|
}
|
||||||
}else {
|
}else {
|
||||||
|
//zlm连接失败
|
||||||
redisCatchStorage.stopPlay(streamInfo);
|
redisCatchStorage.stopPlay(streamInfo);
|
||||||
storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
|
storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
|
||||||
streamInfo = null;
|
streamInfo = null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
if (streamInfo == null) {
|
if (streamInfo == null) {
|
||||||
String streamId = null;
|
String streamId = null;
|
||||||
|
Loading…
Reference in New Issue
Block a user