需求
接入某平台,接收状态变更通知,来实现具体业务逻辑
实现
- 注解 EventType 事件类型type
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface EventType {
/**
* 事件类型
* @return
*/
String eventType() default "-1";
/**
* 功能类型
* @return
*/
String funcType() default "ele";
/**
* 描述
* @return
*/
String description() default "空包处理";
}
- 事件处理器 EventHandleService
public interface EventHandleService<T> {
/**
* handle
* @param t
*/
void handle(T t);
}
- 获取上下文service ContextService
@Service
public class ContextServiceImpl implements ContextService {
@Autowired
EleNoneContextService eleNoneContextService;
@Autowired
List<EventHandleService> eventHandleServiceList;
@Override
public EventHandleService getContext(String eventType, String funcType) {
if(StringUtils.isBlank(eventType)) {
return eleNoneContextService;
}
EventHandleService eventHandleService= eventHandleServiceList.stream()
.filter(f -> f.getClass().getAnnotation(EventType.class) != null)
.filter(f -> StringUtils.equals(eventType, f.getClass().getAnnotation(EventType.class).eventType()))
.filter(f->StringUtils.equals(funcType, f.getClass().getAnnotation(EventType.class).funcType()))
.findFirst().orElse(eleNoneContextService);
return eventHandleService;
}
}
- 对应事件处理器
- 空包事件处理器
@Slf4j
@EventType(eventType = "-1", funcType = "ele", description = "空包处理器")
@Service
public class EleNoneContextService implements EventHandleService<EleWmDto> {
@Override
public void handle(EleWmDto eleDto) {
log.info("空包事件处理");
}
}
- 解除授权处理器
@Slf4j
@Service
@EventType(eventType = "100", funcType = "ele", description = "饿了么外卖解除授权")
public class EleRelieveOAuthEventHandle implements EventHandleService<EleWmDto> {
@Autowired
ShopShineUponService shopShineUponService;
@Override
public void handle(EleWmDto eleDto) {
shopShineUponService.eleWmRelieveOAuth(eleDto.getShopId());
}
}
- 处理器实现类
@Service
public class EleWmEventHandleServiceImpl implements EventHandleService<EleWmDto> {
@Autowired
ContextService contextService;
@Override
public void handle(EleWmDto o) {
String eventType = Optional.ofNullable(o).map(EleWmDto::getAppId).orElse("-1");
String funcType = Optional.ofNullable(o).map(EleWmDto::getFundType).orElse("ele");;
EventHandleService context = contextService.getContext(eventType, funcType);
context.handle(o);
}
}
- 推送回调处理
@Override
public void run(ApplicationArguments args) throws Exception {
Account account = new Account(key, secret);
List<Account> accounts = new ArrayList<Account>();
accounts.add(account);
Config config = new Config(accounts,
new BusinessHandle() {
@Override
public boolean onMessage(String message) {
log.info("饿了么外卖推送消息:{}", message);
EleWmDto eleWmDto = JSONObject.parseObject(message, EleWmDto.class);
eleWmEventHandleService.handle(eleWmDto);
return true;
}
},
new ElemeSdkLogger() {
@Override
public void info(String message) {
}
@Override
public void error(String message) {
log.error("饿了么外卖推送错误:{}", message);
}
});
try {
Bootstrap.start(config);
} catch (UnableConnectionException e) {
e.printStackTrace();
}
}
总结
使用策略模式,去掉了if else 结构,解耦合,提升了代码灵活性,增加新的事件类型,只需要增加对应的实现类即可
各个事件处理器实现单一职责
缺点: 多策略类
适用场景:单点多行为模式