spring aop實現(xiàn)接口超時處理組件的代碼詳解
實現(xiàn)思路
- 這里使用
FutureTask,它通過get方法以阻塞的方式獲取執(zhí)行結(jié)果,并設(shè)定超時時間:
public V get() throws InterruptedException, ExecutionException ; public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException ;
- 利用spring aop解耦業(yè)務(wù)
- 定義業(yè)務(wù)異常信息
實現(xiàn)代碼
定義注解:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
public @interface TimeoutCheck {
/**
* 超時時間,默認5秒
*/
long timeout() default 5L;
/**
* 超時單位,默認秒
*/
TimeUnit unit() default TimeUnit.SECONDS;
/**
* 超時后是否銷毀線程
*/
boolean destroy() default true;
}
這里有一個destroy()的方法,因為我們在執(zhí)行時開獨立線程處理,所以這個方法是為了在超時后,用來判斷是否銷毀還在執(zhí)行的線程;
定義異常:
注意:這里的父類應該是項目中的基礎(chǔ)業(yè)務(wù)異常類;
public class TimeoutCheckException extends RuntimeException{
public TimeoutCheckException(String message) {
super(message);
}
public TimeoutCheckException(String message, Throwable throwable) {
super(message, throwable);
}
}
再順便定義一個屬性配置:
這個的作用是全局控制開關(guān),當不需要的時候可以直接通過配置關(guān)閉;
@Component
@ConfigurationProperties(prefix = "aliweb.timeout")
public class TimeoutCheckProperties {
private boolean enable = true;
public boolean isEnable() {
return enable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
}
最后就是我們的aop類:
@Aspect
@Component
public class TimeoutAop {
private static final Logger logger = LoggerFactory.getLogger(TimeoutAop.class);
@Autowired
private TimeoutCheckProperties timeoutCheckProperties;
@Pointcut("@annotation(timeoutCheck)")
public void pointCut(TimeoutCheck timeoutCheck) {
}
@Around(value = "pointCut(timeoutCheck)", argNames = "joinPoint, timeoutCheck")
public Object around(ProceedingJoinPoint joinPoint, TimeoutCheck timeoutCheck) throws Throwable {
if (!timeoutCheckProperties.isEnable()) {
return joinPoint.proceed();
}
long timeout = timeoutCheck.timeout();
if (timeout <= 0) {
throw new TimeoutCheckException("業(yè)務(wù)邏輯執(zhí)行時間不能小于等于0");
}
long start = System.currentTimeMillis();
String msg = null;
Exception error = null;
Object data = null;
FutureTask<Object> futureTask = createTask(joinPoint);
try {
Thread thread = new Thread(futureTask);
thread.start();
data = futureTask.get(timeout, timeoutCheck.unit());
} catch (InterruptedException e) {
msg = "執(zhí)行中斷";
error = e;
} catch (ExecutionException e) {
msg = "執(zhí)行異常";
error = e;
} catch (TimeoutException e) {
msg = "執(zhí)行超時";
error = e;
} finally {
futureTask.cancel(timeoutCheck.destroy());
}
logger.debug("執(zhí)行時間:{}", System.currentTimeMillis() - start);
if (error != null) {
String suf = error.getMessage() == null ? "" : ":" + error.getMessage();
logger.error(msg + suf, error);
throw new TimeoutCheckException(msg + suf, error);
}
return data;
}
private static FutureTask<Object> createTask(ProceedingJoinPoint joinPoint) {
return new FutureTask<>(() -> {
try {
return joinPoint.proceed();
} catch (Throwable e) {
throw new RuntimeException(e);
}
});
}
}
starter組件
將功能提取成starter組件:
- 定義配置類
@Configuration
@ComponentScan("com.liry.aliweb.timeout")
public class TimeoutCheckAutoConfig {
}
- 定義配置掃描文件
spring.factories,路徑:
src/main/resources/META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.liry.aliweb.timeout.config.TimeoutCheckAutoConfig
- pom增加依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
如上,在主項目引入時就可以直接使用了
到此這篇關(guān)于spring aop實現(xiàn)接口超時處理組件的代碼詳解的文章就介紹到這了,更多相關(guān)spring aop接口超時處理組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于MyBatis中SqlSessionFactory和SqlSession簡解
這篇文章主要介紹了MyBatis中SqlSessionFactory和SqlSession簡解,具有很好的參考價值,希望大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
JAVA中通過Hibernate-Validation進行參數(shù)驗證
這篇文章主要介紹了JAVA中通過Hibernate-Validation進行參數(shù)驗證,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04
SpringBoot之整合MyBatis實現(xiàn)CRUD方式
這篇文章主要介紹了SpringBoot之整合MyBatis實現(xiàn)CRUD方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
啟動Tomcat報錯Unsupported major.minor version xxx的解決方法
這篇文章主要為大家詳細介紹了啟動Tomcat報錯Unsupported major.minor version xxx的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11

