spring aop實(shí)現(xiàn)接口超時(shí)處理組件的代碼詳解
實(shí)現(xiàn)思路
- 這里使用
FutureTask,它通過(guò)get方法以阻塞的方式獲取執(zhí)行結(jié)果,并設(shè)定超時(shí)時(shí)間:
public V get() throws InterruptedException, ExecutionException ; public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException ;
- 利用spring aop解耦業(yè)務(wù)
- 定義業(yè)務(wù)異常信息
實(shí)現(xiàn)代碼
定義注解:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
public @interface TimeoutCheck {
/**
* 超時(shí)時(shí)間,默認(rèn)5秒
*/
long timeout() default 5L;
/**
* 超時(shí)單位,默認(rèn)秒
*/
TimeUnit unit() default TimeUnit.SECONDS;
/**
* 超時(shí)后是否銷(xiāo)毀線(xiàn)程
*/
boolean destroy() default true;
}
這里有一個(gè)destroy()的方法,因?yàn)槲覀冊(cè)趫?zhí)行時(shí)開(kāi)獨(dú)立線(xiàn)程處理,所以這個(gè)方法是為了在超時(shí)后,用來(lái)判斷是否銷(xiāo)毀還在執(zhí)行的線(xiàn)程;
定義異常:
注意:這里的父類(lèi)應(yīng)該是項(xiàng)目中的基礎(chǔ)業(yè)務(wù)異常類(lèi);
public class TimeoutCheckException extends RuntimeException{
public TimeoutCheckException(String message) {
super(message);
}
public TimeoutCheckException(String message, Throwable throwable) {
super(message, throwable);
}
}
再順便定義一個(gè)屬性配置:
這個(gè)的作用是全局控制開(kāi)關(guān),當(dāng)不需要的時(shí)候可以直接通過(guò)配置關(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類(lèi):
@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í)行時(shí)間不能小于等于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í)行超時(shí)";
error = e;
} finally {
futureTask.cancel(timeoutCheck.destroy());
}
logger.debug("執(zhí)行時(shí)間:{}", 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組件:
- 定義配置類(lèi)
@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增加依賴(lài):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
如上,在主項(xiàng)目引入時(shí)就可以直接使用了
到此這篇關(guān)于spring aop實(shí)現(xiàn)接口超時(shí)處理組件的代碼詳解的文章就介紹到這了,更多相關(guān)spring aop接口超時(shí)處理組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Maven中兩個(gè)命令clean 和 install的使用
Maven是一個(gè)項(xiàng)目管理和自動(dòng)構(gòu)建工具,clean命令用于刪除項(xiàng)目中由先前構(gòu)建生成的target目錄,install命令用于將打包好的jar包安裝到本地倉(cāng)庫(kù)中,供其他項(xiàng)目依賴(lài)使用,下面就來(lái)詳細(xì)的介紹一下這兩個(gè)命令2024-09-09
MyBatis-Flex實(shí)現(xiàn)多表聯(lián)查(自動(dòng)映射)
我們可以輕松的使用 Mybaits-Flex 鏈接任何數(shù)據(jù)庫(kù),本文主要介紹了MyBatis-Flex實(shí)現(xiàn)多表聯(lián)查(自動(dòng)映射),具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
SpringCloud使用Feign實(shí)現(xiàn)動(dòng)態(tài)路由操作
這篇文章主要介紹了SpringCloud使用Feign實(shí)現(xiàn)動(dòng)態(tài)路由操作,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
ElasticSearch6.2.3+head插件安裝的方法步驟
這篇文章主要介紹了ElasticSearch6.2.3+head插件安裝的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
Java創(chuàng)建線(xiàn)程的七種方法總結(jié)(全網(wǎng)最全面)
線(xiàn)程是Java中的基本執(zhí)行單元,它允許程序在同一時(shí)間執(zhí)行多個(gè)任務(wù),下面這篇文章主要給大家總結(jié)介紹了關(guān)于Java創(chuàng)建線(xiàn)程的七種方法,文中通過(guò)實(shí)例代碼將這七種方法介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05

