将传统的service层代码改造成逻辑链,以期提高代码的可伸缩性能

代码结构如下:
将传统的service层代码改造成逻辑链,以期提高代码的可伸缩性能_第1张图片
主要类说明:
XmlConfigReader:读取xml配置,转化成链的定义信息。
XmlLogic:Logic定义,含排序,禁用等。
XmlServer:单个服务定义,包含Logic链。服务id
AsyncExecutor:异步执行逻辑链
ContextLogic:逻辑链的上下文对象
DefaultLogicExecutor:按排序执行逻辑链
KeywordWrapperBuilder:关键字查询构建器。
Logic:逻辑片断接口
LogicBee:逻辑执行工具类。
LogicContextRequestBodyAdvice:逻辑上下文预处理对象。来自于RequestBodyArgmentHandler:表单参数处理器。
LogicContextWebHandlerRegister:表单参数处理注册。
LogicExecutor:逻辑链执行接口
LogicExecutors:逻辑链执行工具类。
LogicFactory:Logic链工厂
SelectLogicContext:查询公用上下文对象。
SelectWrapperLogicContext:查询公用上下文对象。
TransactionLogicExecutor:带事务执行logic链
UnionContextLogic:关连链基类。
UpdateLogicContext:增删改操作上下文。

实际使用效果:

将传统的service层代码改造成逻辑链,以期提高代码的可伸缩性能_第2张图片

@RestController
@RequestMapping("/handingPortionAdjust")
public class HandingPortionAdjustController {

    @RequestMapping("/list")
    @SystemLog("xxxxxxxxx")
    public JsonResult findHandingPortionAdjusts(@RequestBody HandingPortionAdjustSelectRequest request) {
        return HandingPortionAdjustServices.LIST.executorResult(request);
    }

    @RequestMapping("/record")
    @SystemLog("xxxxxxxxx")
    public JsonResult findHandingPortionAdjustRecord(@RequestBody HandingPortionAdjustSelectRequest request) {
        return HandingPortionAdjustServices.RECORD.executorResult(request);
    }

    @RequestMapping("/info")
    @SystemLog("xxxxxxxxx")
    public JsonResult getHandingPortionAdjust(@RequestBody HandingPortionAdjustSelectRequest request) {
        return HandingPortionAdjustServices.INFO.executorResult(request);
    }

    @RequestMapping("/add")
    @SystemLog("xxxxxxxxx")
    public JsonResult saveHandingPortionAdjust(@RequestBody HandingPortionAdjustUpdateRequest request) {
        return HandingPortionAdjustServices.ADD.executorTransactionalResult(request);
    }
    
    @RequestMapping("/edit")
    @SystemLog("xxxxxxxxx")
    public JsonResult updateHandingPortionAdjust(@RequestBody HandingPortionAdjustUpdateRequest request) {
        return HandingPortionAdjustServices.EDIT.executorTransactionalResult(request);
    }
    
    @RequestMapping("/verify")
    @SystemLog("xxxxxxxxx")
    public JsonResult verifyHandingPortionAdjust(@RequestBody HandingPortionAdjustUpdateRequest request) {
        return HandingPortionAdjustServices.VERIFY.executorTransactionalResult(request);
    }

    @RequestMapping("/dels")
    @SystemLog("xxxxxxxxx")
    public JsonResult deleteHandingPortionAdjusts(@RequestBody HandingPortionAdjustUpdateRequest request) {
        return HandingPortionAdjustServices.DEL.executorTransactionalResult(request);
    }
}

配置项:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE servers SYSTEM "logic.dtd">
<servers>
    <!--xxxxx-->
    <server serverName="addHandingPortionAdjustServer">
        <logic logicName="addHandingPortionAdjustLogic" sort="10"/>
    </server>
    <!--xxxxx-->
    <server serverName="editHandingPortionAdjustServer">
        <logic logicName="editHandingPortionAdjustLogic" sort="10"/>
    </server>
    <!--xxxxx-->
    <server serverName="delHandingPortionAdjustServer">
        <logic logicName="delHandingPortionAdjustLogic" sort="10"/>
    </server>
    <!--xxxxx-->
    <server serverName="findHandingPortionAdjustServer">
        <logic logicName="findHandingPortionAdjustLogic" sort="10"/>
    </server>
    <!--xxxxx-->
    <server serverName="getHandingPortionAdjustServer">
        <logic logicName="getHandingPortionAdjustLogic" sort="10"/>
    </server>
    <!--xxxxx-->
    <server serverName="verifyHandingPortionAdjustService">
        <logic logicName="verifyHandingPortionAdjustLogic" sort="10"/>
        <logic logicName="verifyHandingPortionAdjustNoPassLogic" sort="20"/>
        <logic logicName="verifyHandingPortionAdjustPassLogic" sort="30"/>
    </server>
    <!--xxxxx-->
    <server serverName="findHandingPortionAdjustRecordServer">
        <logic logicName="findHandingPortionAdjustRecordLogic" sort="10"/>
    </server>
</servers>

主要类:

public interface Logic extends GlobalConstant {

      void handleLogic(LogicContext context);
}

public class LogicChain {

    private final List<Logic> logics = new ArrayList<>();

    public LogicContext execute(LogicContext logicContext) {
            logics.forEach(logic -> {
            // 如果没有结束,接着执行
            if (!logicContext.hasReturn) {
                logic.handleLogic(logicContext);
            }
        });
    }

    public void putLogic(Logic logic) {
        if (null != logic) {
            this.logics.add(logic);
        }
    }
public class LogicContext implements GlobalConstant {

    // 前置操作为添加
    private final static int ADD_FLAG = 1;
    // 前置操作为修改
    private final static int EDIT_FLAG = 2;
    // 前置操作为删除
    private final static int DEL_FLAG = 3;
    // 前置操作为详情
    private final static int DETAIL_FLAG = 4;

    private int handleType = 0;

    // 用于接收基线版本以外的参数,方便业务扩展
    public Param param;
    // json对象类参数
    public JSONObject objectParam;
    // json数组类参数
    public JSONArray arrayParam;
    // 设置成公用的,方便业务调用
    public Header header;
    // logic切换过程中的中间变量流转,设置成public,方便使用
    public Attribute attribute = new Attribute();
    // 结果,可以获取结果集,通过继承或组合的形式扩展结果集
    protected Object result;

    protected Object extResult;
    // 分页结果集
    protected IPage<?> pageResult;
    // 分页条数
    private long total;
    // 是否有错误
    private boolean hasError = false;
    // 错误消息
    private String errorMessage;
    // 正常消息
    private String noticeMessage;
    // 用于判断逻辑执行是否提前结束
    public boolean hasReturn = false;
    }
public class LogicContextRequestBodyAdvice extends RequestBodyAdviceAdapter {

    @Override
    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
        return methodParameter.hasParameterAnnotation(RequestBody.class)
                && LogicContext.class.isAssignableFrom(methodParameter.getParameterType());
    }

    @Override
    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        LogicContext logicContext;
        if (null == body) {
            logicContext = new LogicContext();
        } else {
            logicContext = (LogicContext) body;
        }
        logicContext.setHeader(new Header(inputMessage.getHeaders()));
        return logicContext;
    }
}
public interface LogicExecutor {

    LogicContext executorLogic(String serverName, LogicContext context);
}
public class LogicFactory {

    private final static Map<String, LogicChain> logicChainCache = new ConcurrentHashMap<>();

    private static Map<String, List<String>> logicRelations;

    static {
        try {
            logicRelations = XmlConfigReader.findLogicRelations();
        } catch (Throwable e) {
            e.printStackTrace();
            log.error("配置读取异常" + e.getMessage());
        }
    }

    public static LogicChain getLogicChain(String serverName) {
        LogicChain logicChain = logicChainCache.get(serverName);
        if (null == logicChain) {
            logicChain = buildLogicChain(serverName);
            logicChainCache.put(serverName, logicChain);
        }
        return logicChain;
    }

    private static LogicChain buildLogicChain(String serverName) {
        LogicChain logicChain = new LogicChain();
        List<String> logicBeanNames = loadLogicNameFormXmlConfig(serverName);
        if (null == logicBeanNames) {
            throw new LocalException("没有找到" + serverName + "服务的处理逻辑");
        }
        for (String logicBeanName : logicBeanNames) {
            Logic logic = SpringUtils.getBean(logicBeanName, Logic.class);
            logicChain.putLogic(logic);
        }
        if (logicChain.noExistsLogics()) {
            throw new LocalException("没有找到" + serverName + "服务的处理逻辑");
        }
        logicChain.setReplaceClass(XmlConfigReader.findServiceReplace(serverName));
        return logicChain;
    }

    // 会写在xml配置里面。提供排序,覆盖,插入等方式的配置。
    private static List<String> loadLogicNameFormXmlConfig(String serverName) {
        List<String> logicNames = logicRelations.get(serverName);
        if (null == logicNames || logicNames.isEmpty()) {
            throw new LocalException("没有找到" + serverName + "服务的处理逻辑");
        }
        return logicNames;
    }
}

你可能感兴趣的:(java,spring,boot)