From 4a62d146fc233f8822974909f169e0e9f9855676 Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期三, 07 九月 2022 16:44:47 +0800
Subject: [PATCH] 优化代码
---
java110-core/src/main/java/com/java110/core/event/cmd/ServiceCmdEventPublishing.java | 56 +++++++++++++++++++++++++++++++++++---------------------
1 files changed, 35 insertions(+), 21 deletions(-)
diff --git a/java110-core/src/main/java/com/java110/core/event/cmd/ServiceCmdEventPublishing.java b/java110-core/src/main/java/com/java110/core/event/cmd/ServiceCmdEventPublishing.java
index 990e21a..dcfbb49 100644
--- a/java110-core/src/main/java/com/java110/core/event/cmd/ServiceCmdEventPublishing.java
+++ b/java110-core/src/main/java/com/java110/core/event/cmd/ServiceCmdEventPublishing.java
@@ -1,15 +1,12 @@
package com.java110.core.event.cmd;
-import com.java110.core.annotation.Java110Cmd;
-import com.java110.core.context.DataFlowContext;
+import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.center.DataFlowListenerOrderComparator;
-import com.java110.core.event.service.api.ServiceDataFlowEvent;
-import com.java110.core.event.service.api.ServiceDataFlowListener;
-import com.java110.entity.center.AppService;
+import com.java110.core.log.LoggerFactory;
+import com.java110.dto.CmdListenerDto;
import com.java110.utils.constant.CommonConstant;
import com.java110.utils.constant.ResponseConstant;
-import com.java110.utils.constant.ServiceCodeConstant;
import com.java110.utils.exception.BusinessException;
import com.java110.utils.exception.CmdException;
import com.java110.utils.exception.ListenerExecuteException;
@@ -17,9 +14,8 @@
import com.java110.utils.log.LoggerEngine;
import com.java110.utils.util.Assert;
import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.http.HttpMethod;
+import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -42,7 +38,7 @@
/**
* 淇濆瓨渚﹀惉瀹炰緥淇℃伅锛屼竴鑸惎鍔ㄦ椂鍔犺浇
*/
- private static final List<String> listeners = new ArrayList<String>();
+ private static final List<CmdListenerDto> listeners = new ArrayList<CmdListenerDto>();
/**
* 鏍规嵁 浜嬩欢绫诲瀷鏌ヨ渚﹀惉
@@ -54,7 +50,7 @@
*
* @param listener
*/
- public static void addListener(String listener) {
+ public static void addListener(CmdListenerDto listener) {
listeners.add(listener);
}
@@ -63,7 +59,7 @@
*
* @return
*/
- public static List<String> getListeners() {
+ public static List<CmdListenerDto> getListeners() {
return listeners;
}
@@ -85,10 +81,10 @@
}
List<ServiceCmdListener> cmdListeners = new ArrayList<ServiceCmdListener>();
- for (String listenerBeanName : getListeners()) {
- ServiceCmdListener listener = ApplicationContextFactory.getBean(listenerBeanName, ServiceCmdListener.class);
- Java110Cmd java110Cmd = listener.getClass().getDeclaredAnnotation(Java110Cmd.class);
- if(java110Cmd.serviceCode().equals(serviceCode)) {
+ for (CmdListenerDto listenerBean : getListeners()) {
+ //ServiceCmdListener listener = ApplicationContextFactory.getBean(listenerBean.getBeanName(), ServiceCmdListener.class);
+ ServiceCmdListener listener = ApplicationContextFactory.getBean(listenerBean.getBeanName(), ServiceCmdListener.class);
+ if (listenerBean.getServiceCode().equals(serviceCode)) {
cmdListeners.add(listener);
}
}
@@ -123,7 +119,7 @@
* @param dataFlowContext
*/
public static void multicastEvent(String serviceCode, ICmdDataFlowContext dataFlowContext) throws BusinessException {
- multicastEvent(serviceCode, dataFlowContext, null);
+ multicastEvent(serviceCode, dataFlowContext, null);
}
/**
@@ -151,7 +147,7 @@
* @param event
* @param asyn A 琛ㄧず寮傛澶勭悊
*/
- public static void multicastEvent(String serviceCode, final CmdEvent event, String asyn) {
+ public static void multicastEvent(String serviceCode, final CmdEvent event, String asyn) throws ParseException {
List<ServiceCmdListener> listeners = getListeners(serviceCode);
//杩欓噷鍒ゆ柇 serviceCode + httpMethod 鐨勪睛鍚紝濡傛灉娌℃湁娉ㄥ唽鐩存帴鎶ラ敊銆�
if (listeners == null || listeners.size() == 0) {
@@ -166,7 +162,11 @@
executor.execute(new Runnable() {
@Override
public void run() {
- invokeListener(listener, event);
+ try {
+ invokeListener(listener, event);
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
}
});
break;
@@ -196,12 +196,26 @@
* @since 4.1
*/
@SuppressWarnings({"unchecked", "rawtypes"})
- protected static void invokeListener(ServiceCmdListener listener, CmdEvent event) {
+ protected static void invokeListener(ServiceCmdListener listener, CmdEvent event) throws ParseException {
try {
- listener.cmd(event);
- } catch (CmdException e) {
+ // //杩欓噷澶勭悊涓氬姟閫昏緫鏁版嵁
+ ICmdDataFlowContext dataFlowContext = event.getCmdDataFlowContext();
+ //鑾峰彇璇锋眰鏁版嵁
+ JSONObject reqJson = dataFlowContext.getReqJson();
+
+ logger.debug("API鏈嶅姟 --- 璇锋眰鍙傛暟涓猴細{}", reqJson.toJSONString());
+
+ listener.validate(event, dataFlowContext, reqJson);
+
+ listener.doCmd(event, dataFlowContext, reqJson);
+
+ //logger.debug("API鏈嶅姟 --- 杩斿洖鎶ユ枃淇℃伅锛歿}", dataFlowContext.getResponseEntity());
+ // listener.cmd(event);
+ } catch (CmdException | ParseException e) {
LoggerEngine.error("鍙戝竷渚﹀惉澶辫触", e);
throw e;
}
}
+
+
}
--
Gitblit v1.8.0