Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Provide a way to specify a timeout in Rest Services #651

Description

@mnesarco

It would be nice if we can set the timeout in Rest method declarations. Something like:

@Get(value="/myresource", timeout=3000)
public Event getEvent(); 

or

@Get(value="/myresource")
@HttpOptions(timeout=3000)
public Event getEvent(); 

or

@Get(value="/myresource")
@Timeout(3000)
public Event getEvent(); 

Activity

  1. yDelouis commented on Jul 5, 2013

    @yDelouis
    Contributor

    What would be the generated code ?

  2. mnesarco commented on Jul 5, 2013

    @mnesarco
    Author

    Something like this?

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

    On Fri, Jul 5, 2013 at 9:38 AM, Yoann Delouis notifications@github.comwrote:

    What would be the generated code ?

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/651#issuecomment-20522145
    .

    Frank D. Martínez M.

  3. DayS commented on Jul 5, 2013

    @DayS
    Contributor

    This point has been discussed on the google group

    I'm still thinking that a field in each @Get, @Post, etc.. annotations is the best choice for end users.

  4. yDelouis commented on Jul 5, 2013

    @yDelouis
    Contributor

    I think it should be done at the @Rest annotation level because you set the timeout to the RestTemplate. (Like interceptors and converters).
    Moreover, how would you restore the default behaviour for other methods annotated with @Get, @Post, ...
    Then, if you set interceptors, the requestFactory is an InterceptingClientHttpRequestFactory and it has no method to set a timeout.

  5. DayS commented on Jul 5, 2013

    @DayS
    Contributor

    The only problem with this solution is to set different timeout on some methods. I agree that timeout is set on RestTemplate but it also odd to create a new @Rest annotated interface just for having different timeout. What do you think ?

  6. yDelouis commented on Jul 5, 2013

    @yDelouis
    Contributor

    I don't know if it's a common use case to set different timeouts on methods of the same rest service...
    And it seems that Spring thought like me since they set the timeout at RestTemplate level ;-)
    Then, how do you solve the incompatibility between timeout and interceptors ?

  7. mnesarco commented on Jul 5, 2013

    @mnesarco
    Author

    Another way is to create an injectable Template, something like:

    @RestTemplateFactory
    class MyCustomRestTemplateFactory {
    public RestTemplate getInstance() {
    ....
    }
    }

    So we can inject it at method level

    @get("/Myresource")
    @TemplateProvider(MyCustomRestTemplateFactory.class)
    public Event getEvent();

    MyCustomRestTemplateFactory should be a managed singleton.

    What do you think?

    On Fri, Jul 5, 2013 at 10:08 AM, Damien notifications@github.com wrote:

    The only problem with this solution is to set different timeout on some
    methods. I agree that timeout is set on RestTemplate but it also odd to
    create a new @rest annotated interface just for having different timeout.
    What do you think ?

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/651#issuecomment-20523571
    .

    Frank D. Martínez M.

  8. DayS commented on Jul 5, 2013

    @DayS
    Contributor

    Well... I think you're right @yDelouis. I was thinking of our user case in my current project but it's quite odd :)
    Let's do it in @Rest annotation then

  9. yDelouis commented on Jul 7, 2013

    @yDelouis
    Contributor

    To handle very special cases, I had an idea :
    We could change a bit the way rest services are done. The annotations would be :

    • @Rest with only one parameter rootUrl.
    • @Get, @Post, etc. are the same as before.
    • @RestTemplate with parameters : converters, interceptors and timeout.
      @RestTemplate can be placed on both the interface and the methods. The annotation @RestTemplate on the interface defines the default RestTemplate used in the service. And, if a method is annotated with @RestTemplate, a new RestTemplate is created in the method and used instead of the default one.

    If you don't want to break the API, we could just add the annotation @RestTemplate on methods and add the parameter timeout to the annotation @Rest

    This would enable us to handle special cases without creating a new interface. And we could explain that the parameter timeout is ignored if interceptors are specified.

    Tell me what you think about this.

  10. mnesarco commented on Jul 7, 2013

    @mnesarco
    Author

    Sounds really good! also RestTemplate instances can be cached composing a
    key with @RestTemplate fields. but maybe it is not necessary.

    On Sun, Jul 7, 2013 at 6:18 AM, Yoann Delouis notifications@github.comwrote:

    To handle very special cases, I had an idea :
    We could change a bit the way rest services are done. The annotations
    would be :

    • @rest with only one parameter rootUrl.
    • @get, @post, etc. are the same as before.
    • @RestTemplate with parameters : converters, interceptors and
      timeout. @RestTemplate can be placed on both the interface and the
      methods. The annotation @RestTemplate on the interface defines the
      default RestTemplate used in the service. And, if a method is
      annotated with @RestTemplate, a new RestTemplate is created in the
      method and used instead of the default one.

    This enables us to handle special cases without creating a new interface.
    And we can explain that the parameter timeout is ignored if interceptors
    are specified.

    Tell me what you think.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/651#issuecomment-20569291
    .

    Frank D. Martínez M.

  11. DayS commented on Jul 8, 2013

    @DayS
    Contributor

    We need to stay focused in the main purpose of AA : removing boiler plate code for common cases.
    Each @Rest annotated interface provides an easy way to implement rest-services for a root url (ie: domain). It's common to have an unique timeout for all calls to a domain. So, let's keep it simple and just add a timeout field to @Rest annotation as you suggested before. All other tricky cases can be handled by getting the RestTemplate instance.

  12. mnesarco commented on Jul 8, 2013

    @mnesarco
    Author

    "getting the RestTemplate instance" is "boiler plate code for common cases"

    On Mon, Jul 8, 2013 at 9:01 AM, Damien notifications@github.com wrote:

    We need to stay focused in the main purpose of AA : removing boiler plate
    code for common cases.
    Each @rest annotated interface provides an easy way to implement
    rest-services for a root url (ie: domain). It's common to have an unique
    timeout for all calls to a domain. So, let's keep it simple and just add a
    timeout field to @rest annotation as you suggested before. All other
    tricky cases can be handled by getting the RestTemplate instance.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/651#issuecomment-20607101
    .

    Frank D. Martínez M.

  13. JoanZapata commented on Jul 10, 2013

    @JoanZapata
    Contributor

    @mnesarco I don't think this is the subject. "Getting the RestTemplate instance to set different timeouts for different methods" is not a common case.

    You were basically asking the question "Is setting different timeouts on different methods of a web service a common case". @yDelouis said before it's not, @DayS agreed.

    I agree too. If there is a real difference between timeouts, it's probably a different web services (file download, statistics computation or whatever) in which all method needs a bigger timeout.

    Putting the timeout attribute at the @Rest annotation level sounds good to me.

  14. yDelouis commented on Jul 16, 2013

    @yDelouis
    Contributor

    Instead of a parameter which set the timeout, we could inject a ClientHttpRequestFactory just as we are doing for the interceptors.
    For one of my apps, I need to inject another ClientHttpRequestFactory to allow every certificates using https, and I don't want to set it manually.

    To change the timeout, you'll just need to create a subclass of HttpComponentsClientHttpRequestFactory in which you set the timeout in the constructor and put it in the parameter requestFactory of the annotation @Rest.

  15. sergicastellsague commented on Feb 4, 2014

    @sergicastellsague

    How is this issue going? I've created an Issue relating this: #902

  16. DayS commented on May 11, 2014

    @DayS
    Contributor

    I think we can close this issue as we can now inject a custom RequestFactoy

  17. pedroernnesto commented on Mar 7, 2016

    @pedroernnesto

    Create a custom RequestFactory:

    import org.springframework.http.client.SimpleClientHttpRequestFactory;
    
    public class CustomRequestFactory extends SimpleClientHttpRequestFactory {
    
        public CustomRequestFactory() {
            setConnectTimeout(2000);
            setReadTimeout(2000);
        }
    }
    

    And put to @Rest annotation:

    @Rest(requestFactory = CustomRequestFactory.class)

  18. WonderCsabo commented on Mar 6, 2019

    @WonderCsabo
    Member

    @Rest is part of the AndroidAnnotations REST library.

  19. WonderCsabo commented on Mar 6, 2019

    @WonderCsabo
    Member

    Sorry, this is AndroidAnnotations' repository. For Spring Boot questions, go to the relevant Spring Boot channels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions