视频流token失效重试

This commit is contained in:
huangweixiong 2022-08-26 17:39:19 +08:00
parent 951923b551
commit 1e321145ac
1 changed files with 28 additions and 4 deletions

View File

@ -41,6 +41,7 @@ import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.nio.charset.Charset;
import java.time.LocalDate;
import java.time.LocalDateTime;
@ -118,6 +119,8 @@ public class MonitorService {
static int keepaliveCount = 16;
private WeakReference<Timer> weakTimer;
@PostConstruct
public CompletableFuture<String> init() {
// 不要阻塞程序启动
@ -133,6 +136,8 @@ public class MonitorService {
public String monitorLogin() {
String loginUrl = monitorDomain + "/videoService/accounts/authorize";
HashMap<String, String> loginParam = new HashMap<>();
loginParam.put("userName", userName);
@ -182,6 +187,8 @@ public class MonitorService {
MonitorService thatMonitorService = this;
weakTimer.get().cancel();
//会话保活
Timer timer = new Timer("monitor-capture-keepalive");
timer.scheduleAtFixedRate(new TimerTask() {
@ -215,8 +222,13 @@ public class MonitorService {
}
}, 0, duration * 1000);
weakTimer = new WeakReference<Timer>(timer);
}
return token;
}
@ -772,10 +784,22 @@ public class MonitorService {
String url = monitorDomain + "/videoService/realmonitor/uri?scheme=HLS&channelId=" + code;
HttpHeaders headers = new HttpHeaders();
headers.add("X-Subject-Token", token);
int tryCount = 3;
while (tryCount-- > 0){
try {
ResponseEntity<HashMap> forEntity = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity(headers), HashMap.class);
HashMap body = forEntity.getBody();
String file = (String) body.get("url");
return file;
}catch (HttpClientErrorException.Unauthorized e){
log.error("token失效重新登录");
init();
}
}
return null;
}
public void videoService(String parentId, String path) throws InterruptedException {