AsyncHttpClient IOExceptionFilter异常过滤器

 更新时间:2023年12月13日 09:34:57   作者:codecraft  
这篇文章主要为大家介绍了AsyncHttpClient IOExceptionFilter异常过滤器代码流程解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

本文主要研究一下AsyncHttpClient的IOExceptionFilter

IOExceptionFilter

org/asynchttpclient/filter/IOExceptionFilter.java

/**
 * This filter is invoked when an {@link java.io.IOException} occurs during an http transaction.
 */
public interface IOExceptionFilter {
  /**
   * An {@link org.asynchttpclient.AsyncHttpClient} will invoke {@link IOExceptionFilter#filter} and will
   * use the returned {@link FilterContext} to replay the {@link org.asynchttpclient.Request} or abort the processing.
   *
   * @param ctx a {@link FilterContext}
   * @param <T> the handler result type
   * @return {@link FilterContext}. The {@link FilterContext} instance may not the same as the original one.
   * @throws FilterException to interrupt the filter processing.
   */
  <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException;
}
IOExceptionFilter接口定义了filter方法,在捕获到IOException的时候执行

ResumableIOExceptionFilter

org/asynchttpclient/handler/resumable/ResumableIOExceptionFilter.java

/**
 * Simple {@link org.asynchttpclient.filter.IOExceptionFilter} that replay the current {@link org.asynchttpclient.Request} using a {@link ResumableAsyncHandler}
 */
public class ResumableIOExceptionFilter implements IOExceptionFilter {
  public <T> FilterContext<T> filter(FilterContext<T> ctx) {
    if (ctx.getIOException() != null && ctx.getAsyncHandler() instanceof ResumableAsyncHandler) {

      Request request = ResumableAsyncHandler.class.cast(ctx.getAsyncHandler()).adjustRequestRange(ctx.getRequest());

      return new FilterContext.FilterContextBuilder<>(ctx).request(request).replayRequest(true).build();
    }
    return ctx;
  }
}
ResumableIOExceptionFilter实现了IOExceptionFilter接口,它在asyncHandler是ResumableAsyncHandler类型的时候执行adjustRequestRange方法获取新的request,然后再通过new FilterContext.FilterContextBuilder<>(ctx).request(request).replayRequest(true).build()返回

ResumableAsyncHandler

org/asynchttpclient/handler/resumable/ResumableAsyncHandler.java

/**
   * Invoke this API if you want to set the Range header on your {@link Request} based on the last valid bytes
   * position.
   *
   * @param request {@link Request}
   * @return a {@link Request} with the Range header properly set.
   */
  public Request adjustRequestRange(Request request) {
    Long ri = resumableIndex.get(request.getUrl());
    if (ri != null) {
      byteTransferred.set(ri);
    }
    // The Resumable
    if (resumableListener != null && resumableListener.length() > 0 && byteTransferred.get() != resumableListener.length()) {
      byteTransferred.set(resumableListener.length());
    }
    RequestBuilder builder = new RequestBuilder(request);
    if (request.getHeaders().get(RANGE) == null && byteTransferred.get() != 0) {
      builder.setHeader(RANGE, "bytes=" + byteTransferred.get() + "-");
    }
    return builder.build();
  }
adjustRequestRange方法在byteTransferred不等于resumableListener.length()的时候会重置byteTransferred,对于有range的header则重置其bytes参数值

ResponseFiltersInterceptor

org/asynchttpclient/netty/handler/intercept/ResponseFiltersInterceptor.java

public boolean exitAfterProcessingFilters(Channel channel,
                                            NettyResponseFuture<?> future,
                                            AsyncHandler<?> handler,
                                            HttpResponseStatus status,
                                            HttpHeaders responseHeaders) {
    FilterContext fc = new FilterContext.FilterContextBuilder().asyncHandler(handler).request(future.getCurrentRequest()).responseStatus(status)
            .responseHeaders(responseHeaders).build();
    for (ResponseFilter asyncFilter : config.getResponseFilters()) {
      try {
        fc = asyncFilter.filter(fc);
        // FIXME Is it worth protecting against this?
        assertNotNull("fc", "filterContext");
      } catch (FilterException efe) {
        requestSender.abort(channel, future, efe);
      }
    }
    // The handler may have been wrapped.
    future.setAsyncHandler(fc.getAsyncHandler());
    // The request has changed
    if (fc.replayRequest()) {
      requestSender.replayRequest(future, fc, channel);
      return true;
    }
    return false;
  }
ResponseFiltersInterceptor的exitAfterProcessingFilters方法,对于fc.replayRequest()为true则执行requestSender.replayRequest(future, fc, channel)

replayRequest

org/asynchttpclient/netty/request/NettyRequestSender.java

public void replayRequest(final NettyResponseFuture<?> future, FilterContext fc, Channel channel) {

    Request newRequest = fc.getRequest();
    future.setAsyncHandler(fc.getAsyncHandler());
    future.setChannelState(ChannelState.NEW);
    future.touch();

    LOGGER.debug("\n\nReplaying Request {}\n for Future {}\n", newRequest, future);
    try {
      future.getAsyncHandler().onRetry();
    } catch (Exception e) {
      LOGGER.error("onRetry crashed", e);
      abort(channel, future, e);
      return;
    }

    channelManager.drainChannelAndOffer(channel, future);
    sendNextRequest(newRequest, future);
  }
NettyRequestSender的replayRequest方法会触发future.getAsyncHandler().onRetry()回调,然后执行channelManager.drainChannelAndOffer(channel, future)及sendNextRequest

applyIoExceptionFiltersAndReplayRequest

org/asynchttpclient/netty/request/NettyRequestSender.java

public boolean applyIoExceptionFiltersAndReplayRequest(NettyResponseFuture<?> future, IOException e,
                                                         Channel channel) {
    boolean replayed = false;
    @SuppressWarnings({"unchecked", "rawtypes"})
    FilterContext<?> fc = new FilterContext.FilterContextBuilder().asyncHandler(future.getAsyncHandler())
            .request(future.getCurrentRequest()).ioException(e).build();
    for (IOExceptionFilter asyncFilter : config.getIoExceptionFilters()) {
      try {
        fc = asyncFilter.filter(fc);
        assertNotNull(fc, "filterContext");
      } catch (FilterException efe) {
        abort(channel, future, efe);
      }
    }
    if (fc.replayRequest() && future.incrementRetryAndCheck() && future.isReplayPossible()) {
      future.setKeepAlive(false);
      replayRequest(future, fc, channel);
      replayed = true;
    }
    return replayed;
  }
NettyRequestSender的applyIoExceptionFiltersAndReplayRequest方法会遍历config.getIoExceptionFilters(),然后挨个执行fc = asyncFilter.filter(fc)

小结

AsyncHttpClient的IOExceptionFilter接口定义了filter方法,在捕获到IOException的时候执行;

ResumableIOExceptionFilter实现了IOExceptionFilter接口,它在asyncHandler是ResumableAsyncHandler类型的时候执行adjustRequestRange方法获取新的request,然后再通过new FilterContext.FilterContextBuilder<>(ctx).request(request).replayRequest(true).build()返回;

NettyRequestSender的replayRequest方法会触发future.getAsyncHandler().onRetry()回调,然后执行channelManager.drainChannelAndOffer(channel, future)及sendNextRequest。

以上就是AsyncHttpClient IOExceptionFilter异常过滤器的详细内容,更多关于AsyncHttpClient IOExceptionFilter的资料请关注脚本之家其它相关文章!

相关文章

  • Java 全方位讲解面向对象特点与使用

    Java 全方位讲解面向对象特点与使用

    面向对象开发方法将面向对象的思想应用于软件开发过程中,指导开发活动,是建立在“对象”概念基础上的方法学,简称OO( Object-Oriented)方法
    2022-04-04
  • springboot2.1.7整合thymeleaf代码实例

    springboot2.1.7整合thymeleaf代码实例

    这篇文章主要介绍了springboot2.1.7整合thymeleaf代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • SpringBoot实现列表数据导出为Excel文件

    SpringBoot实现列表数据导出为Excel文件

    这篇文章主要为大家详细介绍了在Spring Boot框架中如何将列表数据导出为Excel文件,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下
    2024-02-02
  • 详解Mybatis Generator的具体使用教程

    详解Mybatis Generator的具体使用教程

    Mybatis Generator可以帮助我们自动生成很多结构化的代码,比如每张表对应的Entity、Mapper接口和Xml文件,可以省去很多繁琐的工作,今天通过本文给大家介绍Mybatis Generator的具体使用教程,感兴趣的朋友一起看看吧
    2022-02-02
  • Spring Boot Web 开发注解篇

    Spring Boot Web 开发注解篇

    在 Spring Boot 快速入门中,只要在 pom.xml 加入了 spring-boot-starter-web 依赖,即可快速开发 web 应用。下文给大家详细介绍了spring boot web 开发注解,感兴趣的朋友参考下吧
    2017-08-08
  • java实现简单贪吃蛇小游戏

    java实现简单贪吃蛇小游戏

    这篇文章主要为大家详细介绍了java实现简单贪吃蛇小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05
  • icePDF去水印的方法(推荐)

    icePDF去水印的方法(推荐)

    下面小编就为大家带来一篇icePDF去水印的方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • 说明Java的传递与回调机制的代码示例分享

    说明Java的传递与回调机制的代码示例分享

    这篇文章主要介绍了说明Java的传递与回调机制的代码示例分享,传递与回调机制是Java入门学习中的基础知识,需要的朋友可以参考下
    2015-09-09
  • 邮件收发原理你了解吗? 邮件发送基本过程与概念详解(一)

    邮件收发原理你了解吗? 邮件发送基本过程与概念详解(一)

    你真的了解邮件收发原理吗?这篇文章主要为大家详细介绍了邮件发送基本过程与概念,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Java泛型映射不同的值类型详解及实例代码

    Java泛型映射不同的值类型详解及实例代码

    这篇文章主要介绍了Java泛型映射不同的值类型详解及实例代码的相关资料,需要的朋友可以参考下
    2017-02-02

最新评论