From 2c781b7fa48006ac1369c22743a7f87e10a568d0 Mon Sep 17 00:00:00 2001 From: CaptainB Date: Fri, 1 Apr 2022 18:18:51 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B6=88=E6=81=AF=E9=80=9A=E7=9F=A5):?= =?UTF-8?q?=20=E4=BF=AE=E6=94=B9=E5=8F=91=E9=80=81=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E5=BC=8F=EF=BC=8C=E9=81=BF=E5=85=8D=E5=9C=A8?= =?UTF-8?q?=E9=AB=98=E7=89=88=E6=9C=ACjdk=E4=B8=AD=E5=87=BA=E7=8E=B0?= =?UTF-8?q?=E5=8F=8D=E5=B0=84=E8=AD=A6=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notice/annotation/SendNotice.java | 5 --- .../AfterReturningNoticeSendService.java | 22 +-------- .../notice/sender/SendNoticeAspect.java | 45 +++++++++++-------- 3 files changed, 28 insertions(+), 44 deletions(-) diff --git a/backend/src/main/java/io/metersphere/notice/annotation/SendNotice.java b/backend/src/main/java/io/metersphere/notice/annotation/SendNotice.java index db6fddebe1..a89dec0a05 100644 --- a/backend/src/main/java/io/metersphere/notice/annotation/SendNotice.java +++ b/backend/src/main/java/io/metersphere/notice/annotation/SendNotice.java @@ -29,11 +29,6 @@ public @interface SendNotice { */ Class targetClass() default Object.class; - /** - * 保存资源的 json - */ - String source() default ""; - /** * 消息内容 */ diff --git a/backend/src/main/java/io/metersphere/notice/sender/AfterReturningNoticeSendService.java b/backend/src/main/java/io/metersphere/notice/sender/AfterReturningNoticeSendService.java index 64ff179074..4dbf1b262f 100644 --- a/backend/src/main/java/io/metersphere/notice/sender/AfterReturningNoticeSendService.java +++ b/backend/src/main/java/io/metersphere/notice/sender/AfterReturningNoticeSendService.java @@ -1,19 +1,16 @@ package io.metersphere.notice.sender; -import com.alibaba.fastjson.JSON; import io.metersphere.commons.constants.NoticeConstants; import io.metersphere.commons.user.SessionUser; import io.metersphere.dto.BaseSystemConfigDTO; import io.metersphere.notice.annotation.SendNotice; import io.metersphere.notice.service.NoticeSendService; import io.metersphere.service.SystemParameterService; -import org.apache.commons.beanutils.BeanMap; import org.apache.commons.lang3.StringUtils; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import javax.annotation.Resource; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -26,23 +23,8 @@ public class AfterReturningNoticeSendService { private NoticeSendService noticeSendService; @Async - public void sendNotice(SendNotice sendNotice, Object retValue, SessionUser sessionUser, String currentProjectId) { - // - List resources = new ArrayList<>(); - String source = sendNotice.source(); - if (StringUtils.isNotBlank(source)) { - // array - if (StringUtils.startsWith(source, "[")) { - resources.addAll(JSON.parseArray(source, Map.class)); - } - // map - else { - Map value = JSON.parseObject(source, Map.class); - resources.add(value); - } - } else { - resources.add(new BeanMap(retValue)); - } + public void sendNotice(SendNotice sendNotice, List resources, SessionUser sessionUser, String currentProjectId) { + // 有批量操作发送多次 BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo(); for (Map resource : resources) { diff --git a/backend/src/main/java/io/metersphere/notice/sender/SendNoticeAspect.java b/backend/src/main/java/io/metersphere/notice/sender/SendNoticeAspect.java index 4b12083a48..f26d609f94 100644 --- a/backend/src/main/java/io/metersphere/notice/sender/SendNoticeAspect.java +++ b/backend/src/main/java/io/metersphere/notice/sender/SendNoticeAspect.java @@ -6,6 +6,7 @@ import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.notice.annotation.SendNotice; +import org.apache.commons.beanutils.BeanMap; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; @@ -22,10 +23,9 @@ import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.stereotype.Component; import javax.annotation.Resource; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; -import java.lang.reflect.Proxy; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import static com.alibaba.fastjson.serializer.SerializerFeature.WriteMapNullValue; @@ -38,7 +38,7 @@ public class SendNoticeAspect { private ExpressionParser parser = new SpelExpressionParser(); private LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer(); - + private ThreadLocal source = new ThreadLocal<>(); @Pointcut("@annotation(io.metersphere.notice.annotation.SendNotice)") public void pointcut() { @@ -55,10 +55,6 @@ public class SendNoticeAspect { Object[] args = joinPoint.getArgs(); SendNotice sendNotice = method.getAnnotation(SendNotice.class); - InvocationHandler invocationHandler = Proxy.getInvocationHandler(sendNotice); - Field value = invocationHandler.getClass().getDeclaredField("memberValues"); - value.setAccessible(true); - if (StringUtils.isNotEmpty(sendNotice.target())) { // 操作内容 //获取方法参数名 @@ -73,8 +69,7 @@ public class SendNoticeAspect { String target = sendNotice.target(); Expression titleExp = parser.parseExpression(target); Object v = titleExp.getValue(context, Object.class); - Map memberValues = (Map) value.get(invocationHandler); - memberValues.put("source", JSON.toJSONString(v, WriteMapNullValue)); + source.set(JSON.toJSONString(v, WriteMapNullValue)); } } catch (Exception e) { LogUtil.error(e.getMessage(), e); @@ -95,9 +90,6 @@ public class SendNoticeAspect { //获取操作 SendNotice sendNotice = method.getAnnotation(SendNotice.class); // 再次从数据库查询一次内容,方便获取最新参数 - InvocationHandler invocationHandler = Proxy.getInvocationHandler(sendNotice); - Field value = invocationHandler.getClass().getDeclaredField("memberValues"); - value.setAccessible(true); if (StringUtils.isNotEmpty(sendNotice.target())) { //将参数纳入Spring管理 @@ -113,20 +105,35 @@ public class SendNoticeAspect { // 查询结果如果是null或者是{},不使用这个值 String jsonObject = JSON.toJSONString(v); if (v != null && !StringUtils.equals("{}", jsonObject) && !StringUtils.equals("[]", jsonObject)) { - Map memberValues = (Map) value.get(invocationHandler); - memberValues.put("source", JSON.toJSONString(v, WriteMapNullValue)); + source.set(JSON.toJSONString(v, WriteMapNullValue)); } } - EvaluationContext context = new StandardEvaluationContext(); - for (int len = 0; len < params.length; len++) { - context.setVariable(params[len], args[len]); + // + List resources = new ArrayList<>(); + String v = source.get(); + if (StringUtils.isNotBlank(v)) { + // array + if (StringUtils.startsWith(v, "[")) { + resources.addAll(JSON.parseArray(v, Map.class)); + } + // map + else { + Map value = JSON.parseObject(v, Map.class); + resources.add(value); + } + } else { + resources.add(new BeanMap(retValue)); } + + SessionUser sessionUser = SessionUtils.getUser(); String currentProjectId = SessionUtils.getCurrentProjectId(); - afterReturningNoticeSendService.sendNotice(sendNotice, retValue, sessionUser, currentProjectId); + afterReturningNoticeSendService.sendNotice(sendNotice, resources, sessionUser, currentProjectId); } catch (Exception e) { LogUtil.error(e.getMessage(), e); + } finally { + source.remove(); } } }