Skip to content

Commit

Permalink
feat: 增加handler
Browse files Browse the repository at this point in the history
  • Loading branch information
WeiXiao-Hyy committed Jan 6, 2024
1 parent de5446d commit f1a4924
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.alipay.shop.designer.dutyChain.controller;

import com.alipay.shop.designer.dutyChain.service.UserDutyChainService;
import com.alipay.shop.model.BusinessLaunch;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
* @author hyy
* @Description
* @create 2024-01-06 12:35
*/
@RestController
@RequestMapping("/dutyChain")
public class UserDutyChainController {

@Resource
private UserDutyChainService userDutyChainService;

@PostMapping("/business/launch")
public List<BusinessLaunch> filterBusinessLaunch(@RequestParam("city") String city, @RequestParam("sex") String sex,
@RequestParam("product") String product) {
return userDutyChainService.filterBusinessLaunch(city, sex, product);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.alipay.shop.designer.dutyChain.enums;

import lombok.Getter;

/**
* File Description.
*
* @author arron
* @date crated at 2024/1/6 12:01
* @see com.alipay.shop.designer.dutyChain.enums
*/
@Getter
public enum HandlerEnum {
city("com.alipay.shop.designer.dutyChain.CityHandler"),
sex("com.alipay.shop.designer.dutyChain.SexHandler"),
product("com.alipay.shop.designer.dutyChain.ProductHandler");

String value="";
HandlerEnum(String value) {
this.value = value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.alipay.shop.designer.dutyChain.service;

import com.alipay.shop.designer.dutyChain.AbstractBusinessHandler;
import com.alipay.shop.designer.dutyChain.CityHandler;
import com.alipay.shop.designer.dutyChain.enums.HandlerEnum;
import com.alipay.shop.model.BusinessLaunch;
import com.alipay.shop.repo.mapper.BusinessLaunchMapper;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
* @author hyy
* @Description
* @create 2024-01-06 11:59
*/
@Service
public class UserDutyChainService {

@Resource
private BusinessLaunchMapper businessLaunchRepo;

//获取Apollo配置
@Value("${duty.chain}")
private String handlerType;

private String currentHandlerType;

private AbstractBusinessHandler currentHandler;

public List<BusinessLaunch> filterBusinessLaunch(String city, String sex, String product) {
List<BusinessLaunch> launchList = businessLaunchRepo.findAll();
return buildChain().processHandler(launchList, city, sex, product);
}

//组装责任链条并返回责任链条首节点
private AbstractBusinessHandler buildChain() {

//如果没有配置,直接返回null
if (Objects.isNull(this.handlerType)) {
return null;
}

if (Objects.isNull(this.currentHandlerType)) {
this.currentHandlerType = this.handlerType;
}

if (this.currentHandlerType.equals(this.handlerType) && Objects.nonNull(this.currentHandler)) {
return this.currentHandler;
} else {
System.out.println("config modified or first init, start to assemble duty chain.");
synchronized (this) {
try {
//伪头节点
AbstractBusinessHandler dummyHeadHandler = new CityHandler();
AbstractBusinessHandler preHandler = dummyHeadHandler;

String[] handlerTypeList = this.handlerType.split(",");
for (String handlerType : handlerTypeList) {
AbstractBusinessHandler handler = (AbstractBusinessHandler)
Class.forName(HandlerEnum.valueOf(handlerType).getValue()).newInstance();
preHandler.nextHandler = handler;
preHandler = handler;
}

this.currentHandler = dummyHeadHandler.nextHandler;
this.currentHandlerType = this.handlerType;

return currentHandler;
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw new UnsupportedOperationException(e);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.alipay.shop.model.BusinessLaunch;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
* File Description.
Expand All @@ -14,4 +16,6 @@
@Mapper
public interface BusinessLaunchMapper extends BaseMapper<BusinessLaunch> {

@Select("select * from business_launch")
List<BusinessLaunch> findAll();
}

0 comments on commit f1a4924

Please sign in to comment.