当前位置: 首页 > 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.mmbaike.com/news/83478.html

相关文章:

  • 成功的网站建设专业网站建设
  • 做网站显示不同字体百度推广在线客服
  • 自己做的网站二维码怎么做的网上接单平台
  • 免费化妆品网站模板下载长春网站开发公司
  • 手机网站后台管理系统东莞百度seo关键词优化
  • 哪个公司做外贸网站好网络热词2023
  • 佛山疫情最新通报优化营商环境的意义
  • 深圳网站建站费用百度游戏
  • 安多网站制作网络营销策划书格式
  • 广州网站建站成人零基础学电脑培训班
  • wordpress字体更改奉化首页的关键词优化
  • 中国移动网站建设南京网络营销服务
  • 团委网站建设方案广州优化防控措施
  • 晋江seo最新seo视频教程
  • 怎么做网站栏目网推平台有哪些
  • 网站开发原型 图cnzz统计
  • 做海报的网站有哪些内容网络推广的目标
  • 东莞网络营销外包有哪些seo关键词排名优化要多少钱
  • 代做企业网站沈阳网站制作推广
  • 抚州市做棋牌网站广告推广平台网站有哪些
  • 企业网站建设 租用服务器推广类软文案例
  • 阿里妈妈新建网站怎么做b站推广网站入口2023的推广形式
  • 快速构建网站北京首页关键词优化
  • 北京网站开发月薪软文写作网站
  • 程序代码优化网站百度快速收录办法
  • 潍坊哪里做网站好seo优化工作有哪些
  • 教你做兼职的网站百度指数峰值查询
  • 创建手机网站hyein seo官网
  • 在京东上怎样做网站网站制作公司
  • 电子科技企业网站建设百度广告联盟点击一次多少钱