最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring aop實(shí)現(xiàn)接口超時(shí)處理組件的代碼詳解

 更新時(shí)間:2024年02月05日 11:36:33   作者:用針戳左手中指指頭  
這篇文章給大家介紹了spring aop實(shí)現(xiàn)接口超時(shí)處理組件,文中有詳細(xì)的實(shí)現(xiàn)思路,并通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下

實(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í)后是否銷毀線程
     */
    boolean destroy() default true;
}

這里有一個(gè)destroy()的方法,因?yàn)槲覀冊(cè)趫?zhí)行時(shí)開(kāi)獨(dú)立線程處理,所以這個(gè)方法是為了在超時(shí)后,用來(lái)判斷是否銷毀還在執(zhí)行的線程;

定義異常:

注意:這里的父類應(yīng)該是項(xiàng)目中的基礎(chǔ)業(yè)務(wù)異常類;

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類:

@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組件:

  • 定義配置類
@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>

如上,在主項(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)文章

  • Java 泛型(Generic)簡(jiǎn)介及用法詳解

    Java 泛型(Generic)簡(jiǎn)介及用法詳解

    泛型是一種把類型明確的工作推遲到創(chuàng)建對(duì)象或者調(diào)用方法的時(shí)候才去明確的特殊的類型,參數(shù)化類型,把類型當(dāng)作參數(shù)一樣的傳遞,本文給大家介紹Java 泛型(Generic)概述及使用,感興趣的朋友跟隨小編一起看看吧
    2023-10-10
  • 關(guān)于MyBatis中SqlSessionFactory和SqlSession簡(jiǎn)解

    關(guān)于MyBatis中SqlSessionFactory和SqlSession簡(jiǎn)解

    這篇文章主要介紹了MyBatis中SqlSessionFactory和SqlSession簡(jiǎn)解,具有很好的參考價(jià)值,希望大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Git在IDEA中合并多個(gè)commit為一個(gè)

    Git在IDEA中合并多個(gè)commit為一個(gè)

    文章介紹了兩種情況下的commit合并方法:未提交到遠(yuǎn)程分支和已經(jīng)提交到遠(yuǎn)程分支,對(duì)于未提交的,通過(guò)reset和push操作即可;對(duì)于已提交的,使用rebase操作將多個(gè)commit合并為一個(gè),然后強(qiáng)制push
    2025-10-10
  • JAVA中通過(guò)Hibernate-Validation進(jìn)行參數(shù)驗(yàn)證

    JAVA中通過(guò)Hibernate-Validation進(jìn)行參數(shù)驗(yàn)證

    這篇文章主要介紹了JAVA中通過(guò)Hibernate-Validation進(jìn)行參數(shù)驗(yàn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 快速了解Spring Boot

    快速了解Spring Boot

    這篇文章主要介紹了快速了解Spring Boot,介紹了其環(huán)境準(zhǔn)備,URL中的變量以及模板渲染等內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • SpringMVC ajax請(qǐng)求的處理方法介紹

    SpringMVC ajax請(qǐng)求的處理方法介紹

    Ajax即異步的 JavaScript和XML,是一種無(wú)需重新加載整個(gè)網(wǎng)頁(yè)的情況下,能夠更新部分模塊的網(wǎng)頁(yè)技術(shù),下面這篇文章主要給大家介紹了關(guān)于SpringMVC Ajax請(qǐng)求的處理,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • SpringBoot之整合MyBatis實(shí)現(xiàn)CRUD方式

    SpringBoot之整合MyBatis實(shí)現(xiàn)CRUD方式

    這篇文章主要介紹了SpringBoot之整合MyBatis實(shí)現(xiàn)CRUD方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Spring中的模塊與應(yīng)用場(chǎng)景詳解

    Spring中的模塊與應(yīng)用場(chǎng)景詳解

    這篇文章主要介紹了Spring中的模塊與應(yīng)用場(chǎng)景詳解,Spring 框架可以為 Java 應(yīng)用程序開(kāi)發(fā)提供全面的基礎(chǔ)設(shè)施支持,它是現(xiàn)在非常流行的 Java 開(kāi)源框架,對(duì)于一個(gè) Java 開(kāi)發(fā)人員來(lái)說(shuō),熟練掌握 Spring 是必不可少的,需要的朋友可以參考下
    2023-09-09
  • 啟動(dòng)Tomcat報(bào)錯(cuò)Unsupported major.minor version xxx的解決方法

    啟動(dòng)Tomcat報(bào)錯(cuò)Unsupported major.minor version xxx的解決方法

    這篇文章主要為大家詳細(xì)介紹了啟動(dòng)Tomcat報(bào)錯(cuò)Unsupported major.minor version xxx的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Java8 ArrayList之forEach的使用

    Java8 ArrayList之forEach的使用

    這篇文章主要介紹了Java8 ArrayList之forEach的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08

最新評(píng)論

浦城县| 西盟| 潮安县| 搜索| 嵩明县| 板桥市| 桂林市| 武鸣县| 长阳| 德钦县| 闻喜县| 达日县| 祁东县| 屯昌县| 抚宁县| 和顺县| 绥芬河市| 滨州市| 大化| 灵宝市| 阿坝县| 岳普湖县| 周至县| 都匀市| 越西县| 肥城市| 苏尼特右旗| 抚松县| 米脂县| 南平市| 普兰县| 高雄县| 宽甸| 南召县| 青田县| 德兴市| 栾城县| 安岳县| 金华市| 芒康县| 芜湖市|