新增主动重连功能以及定时检测连接有效性
This commit is contained in:
parent
bcb7a7a6dd
commit
b500691008
@ -34,6 +34,11 @@ public class GalleryManageController {
|
|||||||
return galleryManageService.createTask(link, targetResolution, AuthCode);
|
return galleryManageService.createTask(link, targetResolution, AuthCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/reconnect")
|
||||||
|
public String reconnect(){
|
||||||
|
return galleryManageService.reconnect();
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("")
|
@GetMapping("")
|
||||||
public String selectGallery(String param, String type, String AuthCode) {
|
public String selectGallery(String param, String type, String AuthCode) {
|
||||||
int userId = userService.getUserId(AuthCode); //能调到这里的授权码对应用户不可能为空
|
int userId = userService.getUserId(AuthCode); //能调到这里的授权码对应用户不可能为空
|
||||||
|
|||||||
@ -16,6 +16,9 @@ public class AbstractMessage {
|
|||||||
public static final byte IDENTITY_MESSAGE = 6;
|
public static final byte IDENTITY_MESSAGE = 6;
|
||||||
|
|
||||||
public static final byte MAINTAIN_MESSAGE = 7;
|
public static final byte MAINTAIN_MESSAGE = 7;
|
||||||
|
|
||||||
|
public static final byte AVAILABLE_CHECK_MESSAGE = 8;
|
||||||
|
|
||||||
public byte messageType;
|
public byte messageType;
|
||||||
|
|
||||||
public int messageId;
|
public int messageId;
|
||||||
|
|||||||
@ -0,0 +1,10 @@
|
|||||||
|
package com.lion.lionwebsite.Message;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class AvailableCheckMessage extends AbstractMessage{
|
||||||
|
{
|
||||||
|
messageType = AVAILABLE_CHECK_MESSAGE;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -43,6 +43,7 @@ public class MessageCodec extends ByteToMessageCodec<AbstractMessage> {
|
|||||||
case AbstractMessage.DELETE_GALLERY_MESSAGE -> objectMapper.readValue(metadata, DeleteGalleryMessage.class);
|
case AbstractMessage.DELETE_GALLERY_MESSAGE -> objectMapper.readValue(metadata, DeleteGalleryMessage.class);
|
||||||
case AbstractMessage.IDENTITY_MESSAGE -> objectMapper.readValue(metadata, IdentityMessage.class);
|
case AbstractMessage.IDENTITY_MESSAGE -> objectMapper.readValue(metadata, IdentityMessage.class);
|
||||||
case AbstractMessage.MAINTAIN_MESSAGE -> objectMapper.readValue(metadata, MaintainMessage.class);
|
case AbstractMessage.MAINTAIN_MESSAGE -> objectMapper.readValue(metadata, MaintainMessage.class);
|
||||||
|
case AbstractMessage.AVAILABLE_CHECK_MESSAGE -> objectMapper.readValue(metadata, AvailableCheckMessage.class);
|
||||||
default -> null;
|
default -> null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -104,7 +104,7 @@ public class GalleryManageService {
|
|||||||
log.info("创建任务: {} 目标分辨率:{}", link, targetResolution);
|
log.info("创建任务: {} 目标分辨率:{}", link, targetResolution);
|
||||||
if (remoteService.addGalleryToQueue(gallery) != 0) {
|
if (remoteService.addGalleryToQueue(gallery) != 0) {
|
||||||
log.error("传送任务{}失败, 未知原因", gallery.getName());
|
log.error("传送任务{}失败, 未知原因", gallery.getName());
|
||||||
response.failure("任务传送失败,未知原因");
|
response.failure("任务传送失败,未知原因,尝试点击重连按钮看看");
|
||||||
pushService.taskCreateReport(user.getUsername(), link, response);
|
pushService.taskCreateReport(user.getUsername(), link, response);
|
||||||
return response.toJSONString();
|
return response.toJSONString();
|
||||||
}
|
}
|
||||||
@ -139,6 +139,21 @@ public class GalleryManageService {
|
|||||||
return response.toJSONString();
|
return response.toJSONString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 尝试重新连接
|
||||||
|
* @return 重连结果
|
||||||
|
*/
|
||||||
|
public String reconnect(){
|
||||||
|
Response response = Response.generateResponse();
|
||||||
|
|
||||||
|
return switch (remoteService.reconnect()) {
|
||||||
|
case 0 -> response.success("重连成功").toJSONString();
|
||||||
|
case -1 -> response.failure("重连失败").toJSONString();
|
||||||
|
case -2 -> response.failure("当前未连接").toJSONString();
|
||||||
|
default -> response.failure("未知错误").toJSONString();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据链接查询图片
|
* 根据链接查询图片
|
||||||
* @param link 链接
|
* @param link 链接
|
||||||
|
|||||||
@ -44,11 +44,39 @@ public class LocalServiceImpl{
|
|||||||
|
|
||||||
PushService pushService;
|
PushService pushService;
|
||||||
|
|
||||||
public LocalServiceImpl(CustomConfigurationMapper configurationMapper, ShareFileMapper shareFileMapper, GalleryMapper galleryMapper, PushService pushService) {
|
RemoteService remoteService;
|
||||||
|
|
||||||
|
public LocalServiceImpl(CustomConfigurationMapper configurationMapper, ShareFileMapper shareFileMapper, GalleryMapper galleryMapper, PushService pushService, RemoteService remoteService) {
|
||||||
this.shareFileMapper = shareFileMapper;
|
this.shareFileMapper = shareFileMapper;
|
||||||
this.configurationMapper = configurationMapper;
|
this.configurationMapper = configurationMapper;
|
||||||
this.galleryMapper = galleryMapper;
|
this.galleryMapper = galleryMapper;
|
||||||
this.pushService = pushService;
|
this.pushService = pushService;
|
||||||
|
this.remoteService = remoteService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查连接是否有效,如果无效自动重连
|
||||||
|
*/
|
||||||
|
@Scheduled(cron = "0 0/30 * * * *")
|
||||||
|
public void CheckConnectionAvailability(){
|
||||||
|
if (remoteService.isDead()){
|
||||||
|
remoteService.initChannel();
|
||||||
|
pushService.sendToMe("主动检测连接已断开,自动进行重连");
|
||||||
|
System.out.println("主动检测连接已断开,自动进行重连");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -1 为对方没有返回
|
||||||
|
if (remoteService.checkAvailability() == -1) {
|
||||||
|
String result = switch (remoteService.reconnect()){
|
||||||
|
case 0 -> "重连成功";
|
||||||
|
case -1 -> "重连失败";
|
||||||
|
case -2 -> "当前未连接,不进行重连";
|
||||||
|
default -> "未知错误";
|
||||||
|
};
|
||||||
|
pushService.sendToMe("主动检测连接无数据返回,自动进行重连:" + result);
|
||||||
|
System.out.println("主动检测连接无数据返回,自动进行重连:" + result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -117,6 +117,41 @@ public class RemoteService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte reconnect(){
|
||||||
|
//如果当前就是未连接状态,直接返回-2
|
||||||
|
if (isDead()){
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
channelFuture.channel().close();
|
||||||
|
|
||||||
|
if(initChannel()){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte checkAvailability(){
|
||||||
|
AvailableCheckMessage acm = new AvailableCheckMessage();
|
||||||
|
acm.setMessageId(atomicInteger.getAndIncrement());
|
||||||
|
|
||||||
|
channel.writeAndFlush(acm);
|
||||||
|
DefaultPromise<AbstractMessage> promise = new DefaultPromise<>(eventLoopGroup);
|
||||||
|
promiseHashMap.put(acm.messageId, promise);
|
||||||
|
try {
|
||||||
|
boolean result = promise.await(10, TimeUnit.SECONDS);
|
||||||
|
if(result){
|
||||||
|
ResponseMessage rsm = (ResponseMessage)promise.getNow();
|
||||||
|
return rsm.getResult();
|
||||||
|
}
|
||||||
|
else return -1;
|
||||||
|
}catch (InterruptedException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isDead(){
|
public boolean isDead(){
|
||||||
return channelFuture.channel() == null || !channelFuture.channel().isActive();
|
return channelFuture.channel() == null || !channelFuture.channel().isActive();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user