日志优化, 代码优化

master
liuqingkun 3 years ago
parent b50ece48f5
commit 6119bb8c2a
  1. 6
      src/main/java/cn/org/hentai/jtt1078/app/VideoServerApp.java
  2. 8
      src/main/java/cn/org/hentai/jtt1078/app/websocket/WebsocketClientHandler.java
  3. 25
      src/main/java/cn/org/hentai/jtt1078/server/Jtt1078Handler.java
  4. 7
      src/main/java/cn/org/hentai/jtt1078/subscriber/RTMPPublisher.java

@ -50,11 +50,7 @@ public class VideoServerApp {
new Thread(() -> {
WebsocketClient wsClient = WebsocketClient.getWsInstance();
try {
wsClient.connect();
} catch (MyException e) {
e.printStackTrace();
}
wsClient.connect();
}).start();
videoServer.start();

@ -129,12 +129,8 @@ public class WebsocketClientHandler extends SimpleChannelInboundHandler<Object>
reconnTimer.schedule(new TimerTask() {
@Override
public void run() {
try {
WebsocketClient wsClient = WebsocketClient.getWsInstance();
wsClient.connect();
} catch (MyException e) {
log.info(BusinessConstant.LOGGER_PREFIX + " : jt808服务websocket重连失败 : msg = ", e.getMessage());
}
WebsocketClient wsClient = WebsocketClient.getWsInstance();
wsClient.connect();
}
}, 10, 5000);
}

@ -83,9 +83,12 @@ public class Jtt1078Handler extends SimpleChannelInboundHandler<Packet> {
@Override
public void channelInactive(ChannelHandlerContext ctx) {
try {
logger.info(BusinessConstant.LOGGER_PREFIX + " : 连接断开触发 channelInactive : channelId = {} ", ctx.channel() == null ? "null" : ctx.channel().id().asLongText());
super.channelInactive(ctx);
release(ctx.channel());
String tag = SessionManager.get(ctx.channel(), "tag");
if (tag != null) {
logger.info(BusinessConstant.LOGGER_PREFIX + " : 连接断开触发 channelInactive : tag = {} : channelId = {} ", tag, ctx.channel().id().asLongText());
super.channelInactive(ctx);
release(ctx.channel());
}
} catch (Exception e) {
logger.error(e.getMessage());
}
@ -100,11 +103,13 @@ public class Jtt1078Handler extends SimpleChannelInboundHandler<Packet> {
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
logger.info(BusinessConstant.LOGGER_PREFIX + " : 连接异常触发 exceptionCaught : channelId = {} ", ctx.channel() == null ? "null" : ctx.channel().id().asLongText());
cause.printStackTrace();
release(ctx.channel());
ctx.close();
String tag = SessionManager.get(ctx.channel(), "tag");
if (tag != null) {
logger.info(BusinessConstant.LOGGER_PREFIX + " : 连接异常触发 exceptionCaught : tag = {} : channelId = {} ", tag, ctx.channel().id().asLongText());
cause.printStackTrace();
release(ctx.channel());
ctx.close();
}
}
@Override
@ -116,7 +121,7 @@ public class Jtt1078Handler extends SimpleChannelInboundHandler<Packet> {
// 只有当该超时的连接已存在时, 打印超时日志, 防止重复打印
if (tag != null) {
logger.info(BusinessConstant.LOGGER_PREFIX + " : 连接超时 userEventTriggered : channelId = {} : tag = {}", ctx.channel() == null ? "null" : ctx.channel().id().asLongText(), tag);
logger.info(BusinessConstant.LOGGER_PREFIX + " : 连接超时 userEventTriggered : tag = {} : channelId = {} ", ctx.channel() == null ? "null" : ctx.channel().id().asLongText(), tag);
release(ctx.channel());
}
}
@ -132,7 +137,7 @@ public class Jtt1078Handler extends SimpleChannelInboundHandler<Packet> {
String tag = SessionManager.get(channel, "tag");
if (tag != null) {
SessionManager.printMapping();
logger.info(BusinessConstant.LOGGER_PREFIX + " : release : channelId = {} : tag = {}, 调用close方法释放通道 ", channel.id(), tag);
logger.info(BusinessConstant.LOGGER_PREFIX + " : release : tag = {} : channelId = {} : , 调用close方法释放通道 ", channel.id().asLongText(), tag);
PublishManager.getInstance().close(tag);
SessionManager.remove(channel);
}

@ -39,19 +39,22 @@ public class RTMPPublisher extends Thread {
process = Runtime.getRuntime().exec(cmd);
stderr = process.getErrorStream();
String errMsg = "";
logger.info(BusinessConstant.LOGGER_PREFIX + " : 执行ffmpeg命令 : 执行完成1 ");
while ((len = stderr.read(buff)) > -1) {
String str = new String(buff, 0, len);
// errMsg += str;
if (debugMode) System.out.print(str);
}
logger.info(BusinessConstant.LOGGER_PREFIX + " : 执行ffmpeg命令 : 执行完成2 ");
if (!closeFlag) {
// 若ffmpeg主动退出标志为否, 重新执行一次ffmpeg命令
logger.info(BusinessConstant.LOGGER_PREFIX + " : 执行ffmpeg命令失败 - 进行重试 : {} ", cmd);
closeFlag = Boolean.FALSE;
process = Runtime.getRuntime().exec(cmd);
stderr = process.getErrorStream();
while ((len = stderr.read(buff)) > -1) {
String str = new String(buff, 0, len);
if (debugMode) System.out.print(str);
}
}
logger.info(BusinessConstant.LOGGER_PREFIX + " : 执行ffmpeg命令失败, 关闭通道[{}], 并调用close方法释放原来的通道 : Process FFMPEG exited... : error msg = {} ", tag, errMsg);

Loading…
Cancel
Save