From 9a452bea6b26cecbe37cf3941d7cdaac00cc1306 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Sun, 2 Aug 2020 23:17:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E4=B8=8A=E6=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../socket/aspect/ClientExceptionAspect.java | 57 +++++++++++++++++++ .../exception/ExceptionServiceImpl.java | 48 +++++++++++++++- .../socket/socket/SocketClientRunnable.java | 2 + 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 cloud-central-control-client/src/main/java/com/cm/central/control/client/socket/aspect/ClientExceptionAspect.java diff --git a/cloud-central-control-client/src/main/java/com/cm/central/control/client/socket/aspect/ClientExceptionAspect.java b/cloud-central-control-client/src/main/java/com/cm/central/control/client/socket/aspect/ClientExceptionAspect.java new file mode 100644 index 0000000..47ecdea --- /dev/null +++ b/cloud-central-control-client/src/main/java/com/cm/central/control/client/socket/aspect/ClientExceptionAspect.java @@ -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; + } + +} diff --git a/cloud-central-control-client/src/main/java/com/cm/central/control/client/socket/service/socket/exception/ExceptionServiceImpl.java b/cloud-central-control-client/src/main/java/com/cm/central/control/client/socket/service/socket/exception/ExceptionServiceImpl.java index 8670d40..0ce9cc6 100644 --- a/cloud-central-control-client/src/main/java/com/cm/central/control/client/socket/service/socket/exception/ExceptionServiceImpl.java +++ b/cloud-central-control-client/src/main/java/com/cm/central/control/client/socket/service/socket/exception/ExceptionServiceImpl.java @@ -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()); + } + } + +} \ No newline at end of file diff --git a/cloud-central-control-client/src/main/java/com/cm/central/control/client/socket/socket/SocketClientRunnable.java b/cloud-central-control-client/src/main/java/com/cm/central/control/client/socket/socket/SocketClientRunnable.java index 8d8e580..8a72407 100644 --- a/cloud-central-control-client/src/main/java/com/cm/central/control/client/socket/socket/SocketClientRunnable.java +++ b/cloud-central-control-client/src/main/java/com/cm/central/control/client/socket/socket/SocketClientRunnable.java @@ -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); }