【问题标题】:Handling timeout with AndroidAnnotations (Spring Rest)使用 AndroidAnnotations (Spring Rest) 处理超时
【发布时间】:2014-01-30 08:35:33
【问题描述】:

基本上,我今天面临的情况如下:

  • 在执行 Rest 操作时处理请求超时。

看似简单的写法,但并不容易编码。

这是我目前的实现:

List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add( new NetworkInterceptor() );

tpl.setInterceptors( interceptors );

所以现在,在设置拦截器之后,我想为模板设置自定义超时配置。

所以我做了以下事情: tpl.getRequestFactory()

这将返回 InterceptingClientHttpRequestFactory 而不是 SimpleClientHttpRequestFactory,因为如果没有设置拦截器,则会返回。

所以当它返回 InterceptingClientHttpRequestFactory 实例时,我无法设置超时。

可以查看Spring的源码,最后一个方法:http://grepcode.com/file_/repo1.maven.org/maven2/org.springframework/spring-web/3.1.1.RELEASE/org/springframework/http/client/support/InterceptingHttpAccessor.java/?v=source

那么...有什么建议吗?

【问题讨论】:

    标签: android spring rest spring-mvc android-annotations


    【解决方案1】:

    假设 tpl 是一个 RestTemplate,您可以将 SimpleClientHttpRequestFactory 作为参数传递给它的构造函数:

        List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
        interceptors.add( new NetworkInterceptor() );
    
        SimpleClientHttpRequestFactory s = new SimpleClientHttpRequestFactory();
        s.setReadTimeout(5000);
        s.setConnectTimeout(1000);
    
        RestTemplate tpl = new RestTemplate(s);//Or however you instantiated it
        tpl.setInterceptors( interceptors );
    
        ClientHttpRequestFactory c =  tpl.getRequestFactory();
    

    希望对你有帮助。

    【讨论】:

    • 我尝试过类似的方法。问题是一旦你设置了任何拦截器,你将无法更改模板,因为它被固定为 InterceptingClientHttpRequestFactory。
    • 即使您保留对您的 SimpleClientHttpRequestFactory 的引用? InterceptingClientHttpRequestFactory 保留 SimpleClientHttpRequestFactory 的引用,所以我认为您可以修改超时。
    • 明天我会试一试。谢谢。
    • 确定不行?看看这个要点gist.github.com/eltabo/8931347 实际上它是相同的解决方案,因为设置拦截器 SimpleClientHttpRequestFactory(比如默认的,比如我们的工厂)是用 InterceptingClientHttpRequestFactory 包装的。最后,它是使用包装的 SimpleClientHttpRequestFactory 创建的请求。
    • 是的。这是工作。这是我的问题,现在我将模板引用保持为静态。我要等4个小时才能给你赏金。非常感谢=)
    【解决方案2】:

    我看到 androidannotations 文档有 @Rest 注释

    @Rest(rootUrl="yourRootUrl",requestFactory=AppRequestFacetory.class,converters ={..},interceptors={..})
    public interface RestApis extends RestClientErrorHandling{...};
    

    AppRequestFacetory.class 设置 TIMEOUT 如下:

    @EBean
    class AppRequestFactory extends SimpleClientHttpRequestFactory {
    
         @AfterInject
         void afterinject() {
             setReadTimeout(20*1000); //set 20s read timeout
             setConnectTimeout(20*1000); //set 20s connect timeout
         }
    }
    

    它有效。享受androidannotations rest api

    【讨论】:

      【解决方案3】:

      当我需要配置 Android 注释休息客户端时,我使用了这样的代码:

          ClientHttpRequestFactory requestFactory = restClient.getRestTemplate().getRequestFactory();
          if (requestFactory instanceof SimpleClientHttpRequestFactory) {
              Log.d("HTTP", "HttpUrlConnection is used");
              ((SimpleClientHttpRequestFactory) requestFactory).setConnectTimeout(3 * 1000);
              ((SimpleClientHttpRequestFactory) requestFactory).setReadTimeout(3 * 1000);
          } else if (requestFactory instanceof HttpComponentsClientHttpRequestFactory) {
              Log.d("HTTP", "HttpClient is used");
              ((HttpComponentsClientHttpRequestFactory) requestFactory).setReadTimeout(3 * 1000);
              ((HttpComponentsClientHttpRequestFactory) requestFactory).setConnectTimeout(3 * 1000);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-10
        • 2012-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-06
        • 1970-01-01
        相关资源
        最近更新 更多