异常上报

This commit is contained in:
wenc000 2020-08-02 23:17:43 +08:00
parent f210d8a1e6
commit 9a452bea6b
3 changed files with 106 additions and 1 deletions

View File

@ -0,0 +1,57 @@
package com.cm.central.control.client.socket.aspect;
import com.cm.central.control.client.socket.service.socket.BaseSocketService;
import com.cm.central.control.client.socket.service.socket.exception.ExceptionServiceImpl;
import com.cm.common.exception.base.SystemException;
import com.cm.socket.service.ISocketService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.sql.SQLSyntaxErrorException;
/**
* @ClassName: ApiLogAspect
* @Description: 接口日志切面
* @Author: WangGeng
* @Date: 2019/3/14 9:33 AM
* @Version: 1.0
**/
@Aspect
@Component
@Order(3)
public class ClientExceptionAspect {
@Resource(name = "exceptionServiceImpl")
private ExceptionServiceImpl exceptionServiceImpl;
/**
* service切入点
*/
@Pointcut("execution(public * com.cm..service..*.*(..)) && !execution(public * com.cm.central.control.client.socket.service..*.*(..))")
public void serviceCutPoint() {
}
@Around("serviceCutPoint()")
public Object appLogAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object result;
try {
result = proceedingJoinPoint.proceed();
} catch (Throwable e) {
boolean exceptionFlag = e instanceof SystemException
|| e instanceof SQLSyntaxErrorException
|| e instanceof InsufficientAuthenticationException;
if (!exceptionFlag) {
new Thread(() -> exceptionServiceImpl.writeThrowable(e)).start();
}
throw e;
}
return result;
}
}

View File

@ -1,6 +1,10 @@
package com.cm.central.control.client.socket.service.socket.exception;
import com.alibaba.fastjson.JSONObject;
import com.cm.central.control.client.socket.manager.SocketClientManager;
import com.cm.central.control.client.socket.service.socket.BaseSocketService;
import com.cm.socket.consts.ISocketConst;
import com.cm.socket.enums.SocketTypeMessageEnum;
import com.cm.socket.pojo.Message;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.stereotype.Service;
@ -27,4 +31,46 @@ public class ExceptionServiceImpl extends BaseSocketService {
public void autoReply(ChannelHandlerContext channelHandlerContext) {
}
}
/**
* 发送异常信息
*
* @param throwable
*/
public void writeThrowable(Throwable throwable) {
SocketClientManager socketClientManager = SocketClientManager.getInstance();
if (socketClientManager.getChannel() == null) {
LOG.debug("Socket客户端未登录不发送异常信息...");
return;
}
StringBuffer exceptionSB = new StringBuffer();
exceptionSB.append(throwable).append("\n");
StackTraceElement[] stackTrace = throwable.getStackTrace();
for (StackTraceElement stackTraceElement : stackTrace) {
exceptionSB.append("\tat " + stackTraceElement).append("\n");
}
JSONObject exceptionJSONObject = new JSONObject();
exceptionJSONObject.put(ISocketConst.EXCEPTION_CLASS, throwable.getClass().getName());
exceptionJSONObject.put(ISocketConst.EXCEPTION_MESSAGE, throwable.getMessage());
exceptionJSONObject.put(ISocketConst.EXCEPTION_CONTENT, exceptionSB.toString());
Message message = new Message();
message.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType());
message.setType(SocketTypeMessageEnum.MESSAGE_TYPE_EXCEPTION.getType());
message.setToken(socketClientManager.getToken());
JSONObject.toJSONString(successResult(exceptionJSONObject));
message.setContent(exceptionJSONObject.toString());
socketClientManager.getChannel().writeAndFlush(message);
}
public static void main(String[] args) {
try {
JSONObject jsonObject = JSONObject.parseObject("123");
} catch (Exception e) {
System.out.println(e.getClass().getName());
}
}
}

View File

@ -41,6 +41,8 @@ public class SocketClientRunnable implements Runnable, InitializingBean {
bootstrap.group(eventLoopGroup);
bootstrap.channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
bootstrap.option(ChannelOption.SO_SNDBUF, 1024 * 1024 * 1024);
bootstrap.option(ChannelOption.SO_RCVBUF, 1024 * 1024 * 1024);
bootstrap.handler(socketClientChannelInitializer);
}