目标: 执行有顺序流程任务,可根据业务自动异动流程顺序
上代码吧!!!
1.上下文引用RequestContext.class
public interface RequestContext<V> {
V toView();
}
2.处理器定义ActionHandle.class
public interface ActionHandle<Ctx extends RequestContext> {
boolean execute(Ctx request);
}
3.构建处理器BuildDataHandle.class
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
public class BuildDataHandle implements ActionHandle<SearchRequestContext> {
@Override
public boolean execute(SearchRequestContext requestContext) {
log.info("构建数据处理器....");
ShowDataParam showDataParam = new ShowDataParam();
showDataParam.setIsShow(Boolean.TRUE);
requestContext.setShowDataParam(showDataParam);
return true;
}
}
4.展示处理器ShowDataHandle.class
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
public class ShowDataHandle implements ActionHandle<SearchRequestContext> {
@Override
public boolean execute(SearchRequestContext requestContext) {
log.warn("展示数据...");
ShowDataParam showDataParam = requestContext.getShowDataParam();
if (Objects.isNull(showDataParam)) {
return false;
}
return true;
}
}
5.处理器链ActionHandleChain.class
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ActionHandleChain<Ctx extends RequestContext> {
/**
* 处理链
*/
private List<ActionHandle<RequestContext>> handleChain = new ArrayList<>();
/**
* 请求上下文引用
*/
private Ctx requestContext;
public ActionHandleChain context(Ctx requestContext) {
this.requestContext = requestContext;
return this;
}
public ActionHandleChain addHandle(ActionHandle<RequestContext> actionHandle) {
handleChain.add(actionHandle);
return this;
}
public CallbackImpl submit() {
for (ActionHandle<RequestContext> handle : handleChain) {
boolean isSuccess = handle.execute(requestContext);
if (!isSuccess) {
log.error("链执行失败,handle:{}", handle);
throw new RuntimeException("链执行失败");
}
}
return new CallbackImpl(requestContext);
}
}
6.返回值定义Callback.class
public interface Callback {
<V> V toView(Class<V> vClass);
}
7.返回值实现定义CallbackImpl.class
public class CallbackImpl implements Callback {
private RequestContext requestContext;
public CallbackImpl(RequestContext requestContext) {
this.requestContext = requestContext;
}
public <V> V toView(Class<V> vClass) {
Object viewObject = requestContext.toView();
return vClass.cast(viewObject);
}
}
8.入参:
SearchRequestContext.class
import lombok.Data;
@Data
public class SearchRequestContext implements RequestContext<SearchResponseVO> {
private ShowDataParam showDataParam;
@Override
public SearchResponseVO toView() {
SearchResponseVO responseVO = new SearchResponseVO();
responseVO.setMessage("流程结束");
return responseVO;
}
}
ShowDataParam.class
import lombok.Data;
@Data
public class ShowDataParam {
private Boolean isShow;
}
9.出参:
SearchResponseVO.class
import lombok.Data;
@Data
public class SearchResponseVO {
private String message;
}
10.执行conroller
RestController
public class TestController {
@Autowired
private BuildDataHandle buildDataHandle;
@Autowired
private ShowDataHandle showDataHandle;
@GetMapping("/test")
public SearchResponseVO test() {
SearchRequestContext searchRequestContext = new SearchRequestContext();
return new ActionHandleChain<>()
.context(searchRequestContext)
.addHandle(buildDataHandle)
.addHandle(showDataHandle)
.submit()
.toView(SearchResponseVO.class);
}
}