import java.util.List; /** * 业务执行模板(把责任链的逻辑串起来) * @author 3y */ public class ProcessTemplate { private List<BusinessProcess> processList; public List<BusinessProcess> getProcessList() { return processList; } public void setProcessList(List<BusinessProcess> processList) { this.processList = processList; } }
import com.java3y.austin.service.api.enums.BusinessCode; import com.java3y.austin.service.api.impl.action.AfterParamCheckAction; import com.java3y.austin.service.api.impl.action.AssembleAction; import com.java3y.austin.service.api.impl.action.PreParamCheckAction; import com.java3y.austin.service.api.impl.action.SendMqAction; import com.java3y.austin.service.api.impl.domain.SendTaskModel; import com.java3y.austin.support.pipeline.BusinessProcess; import com.java3y.austin.support.pipeline.ProcessController; import com.java3y.austin.support.pipeline.ProcessTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.*; /** * api层的pipeline配置类 * @author 3y */ @Configuration public class PipelineConfig { @Autowired private PreParamCheckAction preParamCheckAction; @Autowired private AssembleAction assembleAction; @Autowired private AfterParamCheckAction afterParamCheckAction; @Autowired private SendMqAction sendMqAction; /** * 普通发送执行流程 * 1. 前置参数校验 * 2. 组装参数 * 3. 后置参数校验 * 4. 发送消息至MQ * @return */ @Bean("commonSendTemplate") public ProcessTemplate commonSendTemplate() { ProcessTemplate processTemplate = new ProcessTemplate(); processTemplate.setProcessList(Arrays.asList(preParamCheckAction, assembleAction, afterParamCheckAction, sendMqAction)); return processTemplate; } /** * 消息撤回执行流程 * 1.组装参数 * 2.发送MQ * @return */ @Bean("recallMessageTemplate") public ProcessTemplate recallMessageTemplate() { ProcessTemplate processTemplate = new ProcessTemplate(); processTemplate.setProcessList(Arrays.asList(assembleAction, sendMqAction)); return processTemplate; } /** * pipeline流程控制器 * 后续扩展则加BusinessCode和ProcessTemplate * @return */ @Bean public ProcessController processController() { ProcessController processController = new ProcessController(); Map<String, ProcessTemplate> templateConfig = new HashMap<>(4); templateConfig.put(BusinessCode.COMMON_SEND.getCode(), commonSendTemplate()); templateConfig.put(BusinessCode.RECALL.getCode(), recallMessageTemplate()); processController.setTemplateConfig(templateConfig); return processController; } }
/** * 执行责任链 * * @param context * @return 返回上下文内容 */ public ProcessContext process(ProcessContext context) { /** * 前置检查 */ try { preCheck(context); } catch (ProcessException e) { return e.getProcessContext(); } /** * 遍历流程节点 */ List<BusinessProcess> processList = templateConfig.get(context.getCode()).getProcessList(); for (BusinessProcess businessProcess : processList) { businessProcess.process(context); if (context.getNeedBreak()) { break; } } return context; }