java110
2023-06-07 e050a2ab2fa176ebd0d2461681a41df34838c028
java110-core/src/main/java/com/java110/core/event/cmd/ServiceCmdEventPublishing.java
@@ -3,21 +3,21 @@
import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.center.DataFlowListenerOrderComparator;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.CmdListenerDto;
import com.java110.dto.logSystemError.LogSystemErrorDto;
import com.java110.po.logSystemError.LogSystemErrorPo;
import com.java110.utils.constant.CommonConstant;
import com.java110.utils.constant.ResponseConstant;
import com.java110.utils.exception.BusinessException;
import com.java110.utils.exception.CmdException;
import com.java110.utils.exception.ListenerExecuteException;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.log.LoggerEngine;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ExceptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.ProxyGenerator;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -108,8 +108,9 @@
     *
     * @param cmdDataFlowContext
     */
    public static void multicastEvent(ICmdDataFlowContext cmdDataFlowContext) throws BusinessException {
    public static void multicastEvent(ICmdDataFlowContext cmdDataFlowContext) throws Exception {
        Assert.notNull(cmdDataFlowContext.getServiceCode(), "当前没有可处理的业务信息!");
        //todo 根据cmd serviceCode 发布事件
        multicastEvent(cmdDataFlowContext.getServiceCode(), cmdDataFlowContext, null);
    }
@@ -120,7 +121,7 @@
     * @param serviceCode
     * @param dataFlowContext
     */
    public static void multicastEvent(String serviceCode, ICmdDataFlowContext dataFlowContext) throws BusinessException {
    public static void multicastEvent(String serviceCode, ICmdDataFlowContext dataFlowContext) throws Exception {
        multicastEvent(serviceCode, dataFlowContext, null);
    }
@@ -130,14 +131,17 @@
     * @param serviceCode
     * @param dataFlowContext 这个订单信息,以便于 侦听那边需要用
     */
    public static void multicastEvent(String serviceCode, ICmdDataFlowContext dataFlowContext, String asyn) throws BusinessException {
    public static void multicastEvent(String serviceCode, ICmdDataFlowContext dataFlowContext, String asyn) throws Exception {
        try {
            //todo 组装事件
            CmdEvent targetDataFlowEvent = new CmdEvent(serviceCode, dataFlowContext);
            //todo 发布事件
            multicastEvent(serviceCode, targetDataFlowEvent, asyn);
        } catch (Exception e) {
            logger.error("发布侦听失败,失败原因为:", e);
            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, e.getMessage());
            //throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR, e.getMessage());
            throw e;
        }
    }
@@ -149,7 +153,8 @@
     * @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 Exception {
        //todo 根据serviceCode 去寻找 处理的Cmd处理类 如果java类中 @Java110Cmd(serviceCode = "xx.xx") 写了该注解就会被寻找到
        List<ServiceCmdListener> listeners = getListeners(serviceCode);
        //这里判断 serviceCode + httpMethod 的侦听,如果没有注册直接报错。
        if (listeners == null || listeners.size() == 0) {
@@ -158,17 +163,22 @@
        }
        for (final ServiceCmdListener listener : listeners) {
            if (CommonConstant.PROCESS_ORDER_ASYNCHRONOUS.equals(asyn)) { //异步处理
            if (CommonConstant.PROCESS_ORDER_ASYNCHRONOUS.equals(asyn)) { //todo 异步处理,一般很少用
                Executor executor = getTaskExecutor();
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        invokeListener(listener, event);
                        try {
                            invokeListener(listener, event);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;
            } else {
                // todo 通过同步的方式调用CMDjava类
                invokeListener(listener, event);
                break;
            }
@@ -187,43 +197,35 @@
    }
    /**
     * Invoke the given listener with the given event.
     * 执行 根据serviceCode 找到的cmd 类
     *
     * @param listener the ApplicationListener to invoke
     * @param event    the current event to propagate
     * @since 4.1
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    protected static void invokeListener(ServiceCmdListener listener, CmdEvent event) {
    protected static void invokeListener(ServiceCmdListener listener, CmdEvent event) throws Exception {
        try {
            //        //这里处理业务逻辑数据
        ICmdDataFlowContext dataFlowContext = event.getCmdDataFlowContext();
        //获取请求数据
        JSONObject reqJson = dataFlowContext.getReqJson();
            //todo 获取 cmd 上下文对象
            ICmdDataFlowContext dataFlowContext = event.getCmdDataFlowContext();
            //todo 获取请求数据
            JSONObject reqJson = dataFlowContext.getReqJson();
        logger.debug("API服务 --- 请求参数为:{}", reqJson.toJSONString());
            logger.debug("API服务 --- 请求参数为:{}", reqJson.toJSONString());
            //todo 调用 cmd的校验方法
            listener.validate(event, dataFlowContext, reqJson);
            //todo 调用 cmd的业务处理方法
            listener.doCmd(event, dataFlowContext, reqJson);
        //logger.debug("API服务 --- 返回报文信息:{}", dataFlowContext.getResponseEntity());
         //   listener.cmd(event);
        } catch (CmdException e) {
            LoggerEngine.error("发布侦听失败", e);
            //logger.debug("API服务 --- 返回报文信息:{}", dataFlowContext.getResponseEntity());
            //   listener.cmd(event);
        } catch (Throwable e) {
            LoggerEngine.error("发布侦听失败" + e);
            throw e;
        }
    }
    public static void testPoxy(Class clazz){
        byte[] bytes = ProxyGenerator.generateProxyClass("$Proxy", new Class[]{clazz});
        try(
                FileOutputStream fos =new FileOutputStream(new File("D:/$Proxy.class"))
        ){
            fos.write(bytes);
            fos.flush();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}