Authored by jack

update nginxsync

package com.monitor.nginxsync.model;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
/**
* Created by yoho on 2016/8/25.
*/
@Data
public class LogMsgResp {
String taskId = StringUtils.EMPTY;
int isFinished;
String log;
}
... ...
... ... @@ -6,12 +6,16 @@ import com.monitor.nginxsync.nio.service.LogService;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by yoho on 2016/8/23.
*/
public class LogMsgHandler extends SimpleChannelInboundHandler {
public static final Logger DEBUG = LoggerFactory.getLogger(LogMsgHandler.class);
public static final LogService LOG_SERVICE = new LogService();
@Override
... ... @@ -25,13 +29,19 @@ public class LogMsgHandler extends SimpleChannelInboundHandler {
result.readBytes(resultWarp);
DEBUG.info("recv log msg {}", new String(resultWarp));
LogMsg logMsg = Constants.OBJECT_MAPPER.readValue(resultWarp, LogMsg.class);
// 插入日志管理
LOG_SERVICE.inLogMsg(logMsg);
// 释放资源,这行很关键
result.release();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
DEBUG.warn("Catch exception {}", cause.getMessage());
}
}
... ...
... ... @@ -52,6 +52,7 @@ public class LogService {
List<String> savedList = LOGMAPPER.get(taskId);
if (null != savedList && !savedList.isEmpty()) {
CollectionUtils.addAll(logList, new String[savedList.size()]);
Collections.copy(logList, savedList);
... ... @@ -68,6 +69,12 @@ public class LogService {
logMsg.setIsFinished(0);
} else {
logMsg.setIsFinished(FINISHMAPPER.get(taskId));
if (1 == FINISHMAPPER.get(taskId)) {
FINISHMAPPER.remove(taskId);
LOGMAPPER.remove(taskId);
}
}
}
... ...
... ... @@ -4,9 +4,11 @@ import com.google.common.base.Preconditions;
import com.monitor.model.response.BaseResponse;
import com.monitor.nginxsync.constant.Constants;
import com.monitor.nginxsync.model.CmdTaskInfo;
import com.monitor.nginxsync.model.LogMsgResp;
import com.monitor.nginxsync.model.TaskInfo;
import com.monitor.nginxsync.nio.model.LogMsg;
import com.monitor.nginxsync.nio.service.LogService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
... ... @@ -97,8 +99,36 @@ public class NginxSyncService {
BaseResponse baseResponse = new BaseResponse();
baseResponse.setData(logService.outLogMsg(taskInfo.getTaskId()));
LogMsg logMsg = logService.outLogMsg(taskInfo.getTaskId());
baseResponse.setData(convertLogMsg(logMsg));
return baseResponse;
}
private LogMsgResp convertLogMsg(LogMsg logMsg) {
LogMsgResp logMsgResp = new LogMsgResp();
logMsgResp.setIsFinished(logMsg.getIsFinished());
logMsgResp.setTaskId(logMsg.getTaskId());
String str = StringUtils.EMPTY;
if (null != logMsg.getMsgList()) {
for (String logItem : logMsg.getMsgList()) {
if(StringUtils.isEmpty(logItem))
{
continue;
}
str += "\r\n" + logItem;
}
}
logMsgResp.setLog(str);
return logMsgResp;
}
}
... ...