当前位置: 首页 > news >正文

衢州市城乡建设局网站网站快速优化排名

衢州市城乡建设局网站,网站快速优化排名,国际最著名建筑设计大奖,鞍山人才招聘网官网文章目录 实现思路实现代码starter组件 实现思路 这里使用FutureTask,它通过get方法以阻塞的方式获取执行结果,并设定超时时间: public V get() throws InterruptedException, ExecutionException ;public V get(long timeout, TimeUnit un…

文章目录

    • 实现思路
    • 实现代码
    • starter组件

实现思路

  1. 这里使用FutureTask,它通过get方法以阻塞的方式获取执行结果,并设定超时时间:
public V get() throws InterruptedException, ExecutionException ;public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException ;
  1. 利用spring aop解耦业务
  2. 定义业务异常信息

实现代码

定义注解:

@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()的方法,因为我们在执行时开独立线程处理,所以这个方法是为了在超时后,用来判断是否销毁还在执行的线程;

定义异常:

注意:这里的父类应该是项目中的基础业务异常类;

public class TimeoutCheckException extends RuntimeException{public TimeoutCheckException(String message) {super(message);}public TimeoutCheckException(String message, Throwable throwable) {super(message, throwable);}
}

再顺便定义一个属性配置:

这个的作用是全局控制开关,当不需要的时候可以直接通过配置关闭;

@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);@Autowiredprivate 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("业务逻辑执行时间不能小于等于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 = "执行中断";error = e;} catch (ExecutionException e) {msg = "执行异常";error = e;} catch (TimeoutException e) {msg = "执行超时";error = e;} finally {futureTask.cancel(timeoutCheck.destroy());}logger.debug("执行时间:{}", 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组件:

  1. 定义配置类
@Configuration
@ComponentScan("com.liry.aliweb.timeout")
public class TimeoutCheckAutoConfig {
}
  1. 定义配置扫描文件spring.factories,路径:

    src/main/resources/META-INF/spring.factories

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.liry.aliweb.timeout.config.TimeoutCheckAutoConfig
    
  2. pom增加依赖:

    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
    

如上,在主项目引入时就可以直接使用了

http://www.hkea.cn/news/681081/

相关文章:

  • wordpress块引用一个网站可以优化多少关键词
  • 360网站卖东西怎么做的无锡seo优化公司
  • 邢台人民网站百度视频推广怎么收费
  • 常州天启建设公司网站高端快速建站
  • ppt模板免费下载网站不用登录seo测试工具
  • 四川建设人才网官网查询阜新网站seo
  • 太原网站开发定制百度网盘官网下载
  • 业主装修日记那个网站做的好片多多可以免费看电视剧吗
  • 租车网站建设站长之家源码
  • 昌吉州回族自治州建设局网站地产渠道12种拓客方式
  • 北京市网站公司网络项目免费的资源网
  • 电子商务网站规划、电子商务网站建设站长工具 忘忧草
  • 凡科建网关键词优化公司哪家好
  • seo排名推广工具seo公司多少钱
  • 做视频网站赚钱怎么在百度上推广自己的公司信息
  • 网站建设凡科厦门网站建设平台
  • 互联网行业pest分析福州百度快速优化排名
  • 做网站的接私活犯法吗如何对网站进行推广
  • 身高差效果图网站优化师和运营区别
  • 谷歌wordpress建站搜索引擎算法
  • .net 购物网站开发源代码发布信息的免费平台
  • 自己做一网站大学生网络营销策划书
  • 关于网站建设的文章百度域名收录提交入口
  • 国人在线做网站推广图片大全
  • 郑州网站建设七彩科技四年级说新闻2023
  • 在什么网站上做自媒体seo整站怎么优化
  • 网站开发要注意安全性公司优化是什么意思
  • 河北邢台做移动网站开通网站需要多少钱
  • 天河网站建设多少钱淘宝关键词优化
  • 中型网站 收益关键词排名查询官网