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

做网站找哪个公司北京优化网站方法

做网站找哪个公司,北京优化网站方法,网站菜单导航制作,小白node怎么做网站文章目录 实现思路实现代码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.tj-hxxt.cn/news/39648.html

相关文章:

  • 做万词霸屏后网站关键词没有排名湖南网站建设seo
  • 钉钉如何做自己的网站seo优化内容
  • 建网站怎么分类腾讯企点注册
  • 百度关键词优化软件网站百度seo推广计划类型包含
  • seo专业培训机构杭州优化大师如何删掉多余的学生
  • 佛山专业网站建设公司网站搭建模板
  • 企业所得税规避50种seo哪里可以学
  • 网站建设模板怎么用qq空间刷赞推广网站
  • 殡仪馆做网站的好处整站多关键词优化
  • 室内设计学校哪家好网站seo策划方案案例分析
  • 做网站程序sem对seo的影响有哪些
  • 1688代加工官方网站html简单网页代码
  • 建设一个招聘网站成都新闻今日最新消息
  • 帮人做网站一个多少钱国外免费网站域名服务器
  • 在线网站做图集相册教育机构网站
  • 微信小程序怎么做网站成都移动seo
  • 公司网站做么做百度排名电子商务沙盘seo关键词
  • 成都网站设计是什么六种常见的网站类型
  • 如何在阿里云建设网站昆明seo优化
  • 做算命网站犯法吗百度游戏官网
  • 宁德网百度seo排名原理
  • 东营网站建设天锐科技谷歌浏览器搜索引擎入口
  • 全球贸易中心网搜索优化
  • 营销的网站如何优化网络连接
  • 网站后台百度统计图如何做的深圳网络优化seo
  • 彩票网站开发技术品牌营销做得好的品牌有哪些
  • 网站做点击收费标准网络推广 公司 200个网站
  • wordpress 330seo 服务
  • 成都电脑培训班哪里有网站关键词优化排名怎么做
  • 深圳java网站建设赣州是哪个省