OKHttp中的责任链模式

2023-05-16

一、 什么是责任链模式

责任链, 顾名思义是将多个节点通过链条的方式连接起来,每一个节点相当于一个对象,而每一个对象层层相关,直接或者间接引用下一个对象(节点);直到链条中有一个节点处理头节点传下来的事件截止。

二、责任链模式使用场景(以下摘自 Android源码设计模式)

有一事件,可以被多个对象同时处理,但是由哪个对象处理则在运行时动态决定!
在请求处理者不明确时向多个对象中提交一个请求。
动态指定一组对象处理请求

三、责任链模式UML图

 

客户端发出请求,调用抽象类Handler中的方法处理逻辑业务。对象ConcreteHandler1与ConcreteHandler2继承Handler,其中ConcreteHandler1中持有下一个节点ConcreteHandler2的引用;事件由1对象发出,如果其处理不了,则交由2对象处理! 这是简单的责任链模式结构图,下面使用代码的方式展现:

Handler.class

/**
 * 抽象类
 */
public abstract class Handler {

    /**
     * 下一代处理者
     */
    public Handler nextProcessor;

    /**
     * 每一个实现类处理
     * 
     * @param msg
     */
    public abstract void handleRequest(String msg);
    
}

Processor1.class


/**
 * 处理者1
 */
public class Processor1 extends Handler {
    @Override
    public void handleRequest(String msg) {
        if(msg.equals("Processor1")) {
            System.out.println("第一个处理者处理");
        } else {
            nextProcessor.handleRequest(msg);
        }
    }
}

Processor2.class


/**
 * 处理者2
 */
public class Processor2 extends Handler {
    @Override
    public void handleRequest(String msg) {
        if(msg.equals("Processor2")) {
            System.out.println("第二个处理者处理");
        } else {
            nextProcessor.handleRequest(msg);
        }
    }
}

测试方法:

@Test
public void testProcessor() {
    Processor1 processor1 = new Processor1();
    Processor2 processor2 = new Processor2();

    processor1.nextProcessor = processor2;
    processor2.nextProcessor = processor1;

    processor1.handleRequest("Processor2");
    }

// 运行结果:
第二个处理者处理
Process finished with exit code 0

四、OKHttp中的责任链模式

<font color="red" size=1> 摘自百度百科</font>
android网络框架之OKhttp
一个处理网络请求的开源项目,是安卓端最火热的轻量级框架,由移动支付Square公司贡献(该公司还贡献了Picasso)
用于替代HttpUrlConnection和Apache HttpClient(android API23 6.0里已移除HttpClient,现在已经打不出来)

在使用OKHttp之前,我们可能用到更多的网络请求是Async-Http,一种用于异步处理网络的框架,或者更加直接的是使用android自带的HttpUrlConnection 和 HttpClient ,对其进行简单的封装; OKHttp开源出之后,几乎大部分项目都使用到这个开源框架,它有如下有点:

1. 官方在6.0以后添加了OKHttp
2. okHttp支持SPDY

// 同时能进行的最大请求数
private int maxRequests = 64;
// 同时请求的相同HOST的最大个数
private int maxRequestsPerHost = 5;

okhttp3.Dispatcher.class 中定义了这两个变量,并发数可以支持到64,当然这两个数值是可以自定义的,这说明OKHttp是支持SPDY的(<font color="#666666" size=1>谷歌开发的基于TCP的应用层协议,用于最小化网络延迟,提升网络速度,优化用户的网络使用体验. SPDY并不是一种替代http的协议,只是对http的一种增强</font>)

—— OKHttp的使用 同步获取

private final OkHttpClient client = new OkHttpClient();
 
public void run() throws Exception {
    // 创建Request
    Request request = new Request.Builder()
        .url("http://www.baidu.com/")
        .build();
 
    // 获取到结果
    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
    System.out.println(response.body().string());
}

—— 异步获取

private final OkHttpClient client = new OkHttpClient();
 
public void run() throws Exception {
    Request request = new Request.Builder()
        .url("http://www.baidu.com/")
        .build();
 
    client.newCall(request).enqueue(new Callback() {
      @Override public void onFailure(Request request, Throwable throwable) {
        throwable.printStackTrace();
      }
 
      @Override public void onResponse(Response response) throws IOException {
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
 
        Headers responseHeaders = response.headers();
        System.out.println(response.body().string());
      }
    });
}

使用过程很简单,创建一个OKHttpClient, 创建Request对象,使用同步方法顺序获取或者使用回调CallBack方法异步获取数据;执行的方法主要是client中newCall方法和enqueue方法,

——下面我们分析其中的源码:

/**
* Prepares the {@code request} to be executed at some point in the future.
*/
@Override
public Call newCall(Request request) {
   return RealCall.newRealCall(this, request, false /* for web socket */);
}

newCall 方法需要传一个Request,Request对象使用了构建者模式将请求方法,请求体,请求头进行了封装; newCall 获取到了Call 这个接口:

public interface Call extends Cloneable {
  /** 获取到最开始的request */
  Request request();

  /** 执行请求,获取到Response */
  Response execute() throws IOException;

  void enqueue(Callback responseCallback);

  void cancel();

  boolean isExecuted();

  boolean isCanceled();

  Call clone();

  interface Factory {
    Call newCall(Request request);
  }
}

而这个接口的 实现类只有 <font color="red">okhttp3.RealCall.class</font> ,接下来我们看下他的excute() 方法:


  @Override public Response execute() throws IOException {
    synchronized (this) {
      if (executed) throw new IllegalStateException("Already Executed");
      executed = true;
    }
    captureCallStackTrace();
    eventListener.callStart(this);
    try {
      // 将本次请求添加到事件调度器中
      client.dispatcher().executed(this);
        
      // 今天的主角, 责任链获取到Response结果
      Response result = getResponseWithInterceptorChain();
      if (result == null) throw new IOException("Canceled");
      return result;
    } catch (IOException e) {
      eventListener.callFailed(this, e);
      throw e;
    } finally {
      client.dispatcher().finished(this);
    }
  }

—— Dispatcher

在讲解责任链之前,我们先看下Dispatcher调度器中有些什么?
[图片上传失败...(image-10e627-1536636467207)]

可以知道,它有三个双端队列,

// 双端队列,支持首尾两端 双向开口可进可出
    
    /**
     * 准备运行的异步队列
     * 
     */
    private final Deque<AsyncCall> readyAsyncCalls = new ArrayDeque<>();

    // 正在运行的异步
    private final Deque<AsyncCall> runningAsyncCalls = new ArrayDeque<>();

    // 正在执行的同步队列
    private final Deque<RealCall> runningSyncCalls = new ArrayDeque<>();

队列中线程使用线程池:

    /**
     * 线程池的方式启动线程,使用懒加载的方式
     */
    private @Nullable ExecutorService executorService;
    
    public synchronized ExecutorService executorService() {
        if (executorService == null) {
            //TODO 线程池
            //TODO 核心线程 最大线程 非核心线程闲置60秒回收 任务队列
            executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,
                    new SynchronousQueue<Runnable>(), Util.threadFactory("OkHttp Dispatcher",
                    false));
        }
        return executorService;
    }

而上面client执行调度器中的excute方法,实际上就是将当前请求直接添加到这个同步的双端队列中,等待线程池中的队列被执行!

—— getResponseWithInterceptorChain()

接下来就要执行拦截器了,而拦截器中就是使用了我们今天所知道的责任链模式,上面的责任链模式已经说的很清晰了,一环接着一环,一个对象持有下个对象的引用;我们看OKHttp中的责任链模式是怎样写的,点击进入该方法:

  Response getResponseWithInterceptorChain() throws IOException {
    // Build a full stack of interceptors.
    //责任链 实际上是像递归一样倒叙执行
    List<Interceptor> interceptors = new ArrayList<>();
    interceptors.addAll(client.interceptors());
    //5、重试与重定向
    interceptors.add(retryAndFollowUpInterceptor);
    // 4、请求头等信息
    interceptors.add(new BridgeInterceptor(client.cookieJar()));
    //3、缓存配置 根据条件(存在响应缓存并被设置为不变的或者响应在有效期内)返回缓存响应设置请求头(If-None-Match、If-Modified-Since等) 服务器可能返回304(未修改)
    interceptors.add(new CacheInterceptor(client.internalCache()));
    //2、连接
    interceptors.add(new ConnectInterceptor(client));
    if (!forWebSocket) {
      interceptors.addAll(client.networkInterceptors());
    }
    //1、流操作(写出请求体、获得响应数据)进行http请求报文的封装与请求报文的解析
    interceptors.add(new CallServerInterceptor(forWebSocket));

    Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
        originalRequest, this, eventListener, client.connectTimeoutMillis(),
        client.readTimeoutMillis(), client.writeTimeoutMillis());

    return chain.proceed(originalRequest);
  }

可以看到,getResponseWithInterceptorChain() 方法,是将5个拦截器添加到链表中,再新建了一个RealInterceptorChain.class 类,然后执行了我们责任链中抽象处理类的处理方法 proceed,这里是使用了接口的形式:

public interface Interceptor {
  Response intercept(Chain chain) throws IOException;

  interface Chain {
    Request request();

    Response proceed(Request request) throws IOException;

    /**
     * Returns the connection the request will be executed on. This is only available in the chains
     * of network interceptors; for application interceptors this is always null.
     */
    @Nullable Connection connection();

    Call call();

    int connectTimeoutMillis();

    Chain withConnectTimeout(int timeout, TimeUnit unit);

    int readTimeoutMillis();

    Chain withReadTimeout(int timeout, TimeUnit unit);

    int writeTimeoutMillis();

    Chain withWriteTimeout(int timeout, TimeUnit unit);
  }
}

所以,责任现在都交给了RealInterceptorChain, 上面直接调用了Interceptor.Chain接口中的 proceed方法,我们看下他的实现:

 public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
      RealConnection connection) throws IOException {
      
      .....

    // 创建新的拦截链,链中的拦截器集合index+1
    // Call the next interceptor in the chain.
    RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
        connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
        writeTimeout);
    //执行当前的拦截器 默认是:retryAndFollowUpInterceptor
    Interceptor interceptor = interceptors.get(index);
    Response response = interceptor.intercept(next);

    ......
    
    return response;
  }

主要是上面三行代码,首先拿到下一个 拦截器,上面添加的第一个拦截器是 retryAndFollowUpInterceptor (重试与重定向)拦截器,然后将下一个拦截器传入到重试与重定向拦截器中,看看intercept这个方法在实现类中做的操作:

@Override public Response intercept(Chain chain) throws IOException {

    Request request = chain.request();
    // 首先拿到当前真实的Interceptor 实现类
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    Call call = realChain.call();
    EventListener eventListener = realChain.eventListener();
    // 核心 协调连接、请求/响应以及复用
    StreamAllocation streamAllocation = new StreamAllocation(client.connectionPool(),
        createAddress(request.url()), call, eventListener, callStackTrace);
    this.streamAllocation = streamAllocation;

    int followUpCount = 0;
    Response priorResponse = null;
    while (true) {
      if (canceled) {
        streamAllocation.release();
        throw new IOException("Canceled");
      }

      Response response;
      boolean releaseConnection = true;
      try {
        //执行到一半,又去执行了RealInterceptorChain中的proceed方法
        //实际上就是下一个拦截器
        response = realChain.proceed(request, streamAllocation, null, null);
        releaseConnection = false;
      } catch (RouteException e) {
        .....
      }
   }

这个过程其实就是递归的过程,而底就是CallServerInterceptor ,这里不对拦截器作详细的讲解,每个拦截器做的处理逻辑都差不多,下面我们看下这个过程的图解:

 

总结:

OKHttp中使用拦截器对不同的业务进行区分,我们也可以使用自己的自定义拦截器
其中的责任链模式其实和我们设计模式中的有区别,这里是将分发处理给了接口,让其去处理

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

OKHttp中的责任链模式 的相关文章

  • Mac OS10.12 编译Android源码8.1

    内容 介绍mac os10 12拉取android源码 xff0c 并且编译后 xff0c 刷入手机的过程 下载的rom是android 8 1 xff0c 手机是pixel 准备工作 硬盘大小 本人Mac磁盘空间只有256GB xff0c

随机推荐

  • android源码 xcode版本,【Android】AOSP源码下载及编译 for mac

    本文记录了AOSP在Mac系统上下载和编译的过程 采用的系统是 macOS 10 13 1 所使用的AOSP分支是 android 8 1 0 r7 系统预留空间 大于200G 一 环境配置 环境配置 xff0c 官网给出了非常全的教程 x
  • (Android 9.0)Activity启动流程源码分析

    前言 熟悉Activity的启动流程和运行原理是一个合格的应用开发人员所应该具备的基本素质 xff0c 其重要程度就不多做描述了 同时 xff0c 知识栈应该不断的更新 xff0c 最新发布的Android 9 0版本相较于之前的几个版本也
  • Lifecycle 源码详解

    Lifecycle 是 Jetpack 整个家族体系内最为基础的内容之一 xff0c 正是因为有了 Lifecycle 的存在 xff0c 使得如今开发者搭建依赖于生命周期变化的业务逻辑变得简单高效了许多 xff0c 使得我们可以用一种统一
  • git常用命令

    1 拉取远程所有分支 git clone xxx git branch r grep v 39 gt 39 while read remote do git branch track 34 remote origin 34 34 remot
  • Android应用启动流程分析

    1 前言 网上看过很多Activity启动过程的源码解析 xff0c 很多文章会贴上一大段代码 xff0c 然后从startActivity 函数开始深究整个源码的调用栈 个人感觉这类文章代码细节太多 xff0c 反而容易迷失在源码调用之中
  • 从一个分支cherry-pick多个commit到其他分支

    在branch1开发 xff0c 进行多个提交 xff0c 这是切换到branch2 xff0c 想把之前branch1分支提交的commit都 复制 过来 xff0c 怎么办 xff1f 单个commit只需要git cherry pic
  • IntWritable详解

    1 Hadoop数据类型如下图 xff1a 由上图的Writable层次结构图可以看到绝大多数的数据类型都实现了Writable WritableComparable接口 xff0c 在此先分析一下这两个接口情况 自顶下下逐步分析 Writ
  • 线程池源码剖析

    线程池 xff08 英语 xff1a thread pool xff09 xff1a 一种线程使用模式 线程过多会带来调度开销 xff0c 进而影响缓存局部性和整体性能 而线程池维护着多个线程 xff0c 等待着监督管理者分配可并发执行的任
  • Java 设计模式之装饰者模式

    一 了解装饰者模式 1 1 什么是装饰者模式 装饰者模式指的是在不必改变原类文件和使用继承的情况下 xff0c 动态地扩展一个对象的功能 它是通过创建一个包装对象 xff0c 也就是装饰者来包裹真实的对象 所以装饰者可以动态地将责任附加到对
  • Java 设计模式之策略模式

    一 了解策略模式 1 1 什么是策略模式 策略模式 Strategy Pattern 是指对一系列的算法定义 xff0c 并将每一个算法封装起来 xff0c 而且使它们还可以相互替换 此模式让算法的变化独立于使用算法的客户 1 2 策略模式
  • Java 设计模式之适配器模式

    一 了解适配器模式 1 1 什么是适配器模式 适配器模式将一个类的接口 xff0c 转换成客户期望的另一个接口 适配器让原来接口不兼容的类可以合作无间 适配器模式有两种 xff1a 对象 适配器和 类 适配器 这个模式可以通过创建适配器进行
  • 责任链模式

    责任链模式的定义与特点 责任链模式的定义 xff1a 使多个对象都有机会处理请求 xff0c 从而避免请求的发送者和接受者之间的耦合关系 xff0c 将这个对象连成一条链 xff0c 并沿着这条链传递该请求 xff0c 直到有一个对象处理他
  • java设计模式-桥接模式

    桥接模式定义 桥接模式 xff08 Bridge Pattern xff09 xff0c 将抽象部分与它的实现部分分离 xff0c 使它们都可以独立地变化 更容易理解的表述是 xff1a 实现系统可从多种维度分类 xff0c 桥接模式将各维
  • java设计模式-状态模式

    1 状态模式的定义和特点 状态 xff08 State xff09 模式的定义 xff1a 对有状态的对象 xff0c 把复杂的 判断逻辑 提取到不同的状态对象中 xff0c 允许状态对象在其内部状态发生改变时改变其行为 状态模式是一种对象
  • java设计模式-命令模式

    18 xff0c 命令模式 18 1 命令模式的定义和特点 命令 xff08 Command xff09 模式的定义如下 xff1a 将一个请求封装为一个对象 xff0c 使发出请求的责任和执行请求的责任分割开 这样两者之间通过命令对象进行
  • java设计模式-代理模式

    17 xff0c 代理模式 17 1 代理模式的定义和特点 代理模式的定义 xff1a 由于某些原因需要给某对象提供一个代理以控制对该对象的访问 这时 xff0c 访问对象不适合或者不能直接引用目标对象 xff0c 代理对象作为访问对象和目
  • 工厂方法模式

    概念定义 工厂方法 Factory Method 模式 xff0c 又称多态工厂 Polymorphic Factory 模式或虚拟构造器 Virtual Constructor 模式 工厂方法模式通过定义工厂抽象父类 或接口 负责定义创建
  • TextFuseNet: Scene Text Detection with Richer Fused Features论文阅读

    TextFuseNet Scene Text Detection with Richer Fused Features 利用更丰富的特征融合进行场景文本检测 代码 xff1a https github com ying09 TextFuse
  • JUC原子类: CAS, Unsafe和原子类详解

    CAS 线程安全的实现方法包含 互斥同步 synchronized 和 ReentrantLock非阻塞同步 CAS AtomicXXXX无同步方案 栈封闭 xff0c Thread Local xff0c 可重入代码 什么是CAS CAS
  • OKHttp中的责任链模式

    一 什么是责任链模式 责任链 xff0c 顾名思义是将多个节点通过链条的方式连接起来 xff0c 每一个节点相当于一个对象 xff0c 而每一个对象层层相关 xff0c 直接或者间接引用下一个对象 xff08 节点 xff09 xff1b