熔断器 Hystrix 源码解析 —— 命令执行(三)之执行超时

本文深入解析 Hystrix 命令执行超时的实现,涵盖 HystrixObservableTimeoutOperator、HystrixTimer 及其内部机制,包括 ScheduledExecutor、TimerListener 和 TimerReference。通过配置启用超时功能,并分析超时后的处理流程,如取消订阅和异常处理。建议在了解 RxJava 基础上阅读。
摘要由CSDN通过智能技术生成

摘要: 原创出处 http://www.iocoder.cn/Hystrix/command-execute-third-timeout/ 「芋道源码」欢迎转载,保留摘要,谢谢!

本文主要基于 Hystrix 1.5.X 版本


������关注微信公众号:【芋道源码】有福利:
1. RocketMQ / MyCAT / Sharding-JDBC 所有源码分析文章列表
2. RocketMQ / MyCAT / Sharding-JDBC 中文注释源码 GitHub 地址
3. 您对于源码的疑问每条留言将得到认真回复。甚至不知道如何读源码也可以请教噢
4. 新的源码解析文章实时收到通知。每周更新一篇左右
5. 认真的源码交流微信群。


1. 概述

本文主要分享 Hystrix 命令执行(三)之执行超时

建议 :对 RxJava 已经有一定的了解的基础上阅读本文。

开启执行超时功能,需要配置 :

  • HystrixCommandProperties.executionTimeoutEnabled :执行命令超时功能开关。
    • 值 :Boolean
    • 默认值 :true
  • HystrixCommandProperties.executionTimeoutInMilliseconds :执行命令超时时长。
    • 值 :Integer
    • 单位 :毫秒
    • 默认值 :1000 毫秒

《Hystrix 源码解析 —— 命令执行(一)之正常执行逻辑》「4. #executeCommandAndObserve(…)」 中,#executeCommandAndObserve(...) 方法的第 75 行 lift(new HystrixObservableTimeoutOperator<R>(_cmd)) ,实现了对执行命令超时的监控。


推荐 Spring Cloud 书籍

2. HystrixObservableTimeoutOperator

HystrixObservableTimeoutOperator 类,代码如下 :

  1: private static class HystrixObservableTimeoutOperator<R> implements Operator<R, R> {
   
  2: 
  3:     final AbstractCommand<R> originalCommand;
  4: 
  5:     public HystrixObservableTimeoutOperator(final AbstractCommand<R> originalCommand) {
   
  6:         this.originalCommand = originalCommand;
  7:     }
  8: 
  9:     @Override
 10:     public Subscriber<? super R> call(final Subscriber<? super R> child) {
   
 11:         // 创建 订阅
 12:         final CompositeSubscription s = new CompositeSubscription();
 13:         // 添加 订阅
 14:         // if the child unsubscribes we unsubscribe our parent as well
 15:         child.add(s);
 16: 
 17:         //capture the HystrixRequestContext upfront so that we can use it in the timeout thread later
 18:         final HystrixRequestContext hystrixRequestContext = HystrixRequestContext.getContextForCurrentThread();
 19: 
 20:         TimerListener listener = new TimerListener() {
   
 21: 
 22:             @Override
 23:             public void tick() {
   
 24:                 // if we can go from NOT_EXECUTED to TIMED_OUT then we do the timeout codepath
 25: 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值