莫大人

第三方接口调用框架

最近给公司写了一套第三方接口调用的框架,为了规范并帮助业务稳定的允许,特写了这个框架。

 

 

###框架结构
Invoker.invoking	门面
	-BizService	业务接口
	-BizAOPService	业务切面
	+invoking	业务接口调用
		BizService.check	参数检查
		BizService.pre	业务前置调用
		BizAOPService.pre 业务切面前置调用
		BizService.execute	业务调用
		BizAOPService.after	业务切面后置调用
		BizService.after	业务后只调用
		BizService.handlerException	异常处理

  框架结构实际很简单,只是将相同的东西模板化,不明确的东西抽象化,不能主导的东西桥接出去。

参数适配器

import java.io.Serializable;

public interface RequestAdaptor<T> extends Serializable {

	/**
	 * 参数适配
	 */
	PayRequest<T> adapt();

	/**
	 * 参数检查
	 */
	void check();
	
}

  

实际这里我完全可以将 BizAOPService 放入一个集合形成类似拦截器一样的存在,这里看具体的需求了。

public interface Invoker<M,N> {

	void invoking(BizService<M,N> bizService, BizAOPService<M,N> bizAOPService, M m, N n) throws Exception ;
	
}

 

public interface BizService<M,N> {
	
	/**参数校验*/
	void check(M request, N response);
	/**调用前处理 如内置参数组装,更新业务状态*/
	void pre(M request, N response);
	/**接口调用*/
	void execute(M request, N response);
	/**调用后处理 更新业务状态*/
	void after(M request, N response);
	/**参数校验异常处理 */
	void checkException(M request, N response, Exception e) throws Exception ;
	/**调用前异常处理*/
	void preException(M request, N response, Exception e) throws Exception ;
	/**调用异常处理*/
	void executeException(M request, N response, Exception e) throws Exception ;
	/**调用后异常处理*/
	void afterException(M request, N response, Exception e) throws Exception ;
	
}

  

 

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractBizService<M,N> implements BizService<M,N>{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(AbstractBizService.class);

	@Override
	public void check(M request, N response) {
		LOGGER.info("接口调用参数检查:{},{}", request, response);
	}

	@Override
	public void pre(M request, N response) {
		LOGGER.info("调用前处理:{},{}", request, response);
	}

	@Override
	public void after(M request, N response) {
		LOGGER.info("调用后处理:{},{}", request, response);
	}

	@Override
	public void checkException(M request, N response, Exception e) throws Exception {
		LOGGER.info("接口调用参数检查异常处理:{},{}", request, response, e);
		throw e;
	}

	@Override
	public void preException(M request, N response, Exception e)  throws Exception {
		LOGGER.info("调用前异常处理:{},{}", request, response, e);
		throw e;
	}

	@Override
	public void executeException(M request, N response, Exception e)  throws Exception {
		LOGGER.info("接口调用异常处理:{},{}", request, response, e);
		throw e;
	}

	@Override
	public void afterException(M request, N response, Exception e)  throws Exception {
		LOGGER.info("调用后异常处理:{},{}", request, response, e);
		throw e;
	}


}

 

public interface BizAOPService<M,N> {

	void pre(M request, N response);
	
	void after(M request, N response);
	
	String getBizNo(M request);
	
}

  

 

posted on 2019-09-12 14:55  莫大人  阅读(861)  评论(0编辑  收藏  举报

导航