RestTemplate.postForObject() java.net.SocketTimeoutException: Read timed out EVEN THOUGH SUCCESSFUL












0














I have two Java Spring Boot web service apps on the same server calling each other via REST. Service A calls Service B and the latter successfully acts upon the notfication.
THE PROBLEM is that Service A never receives the acknowlegement from Service B, so it thinks it has failed, and in accordance with its looping recovery logic, it tries again…and again…and again. Service B ends up doing 3 times the work for no added benefit.



The relevant code (stripped down and falsified to protect the guilty) is as follows:



Service A:



public void giveOrderToServiceB(@RequestBody CustomClass message) {
...
org.springframework.web.client.RestTemplate template = new RestTemplate(clientHttpRequestFactory());
com.mycompany.CustomReply reply = template.postForObject(serviceBUrl, message, CustomReply.class);


Service B REST Controller:



@PostMapping(value="ExecuteTheWork", produces=org.springframework.http.MediaType.APPLICATION_JSON_VALUE, consumes=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody CustomReply executeTheWork(@RequestBody CustomClass thing) {
// do something with the thing...
CustomReply reply = new CustomReply();
reply.setReply("Successfully executed the work.");
return reply;
}


The actual exception caught by Service A after calling RestTemplate.postForObject() is



java.net.SocketTimeoutException: Read timed out


Please advise.










share|improve this question






















  • Have you checked that Service B actually finishes it's method? I guess the culprit lies somewhere in // do something with the thing...
    – dunni
    Nov 21 '18 at 18:59










  • Thanks. Yes, it executes, parses an XML file, makes calls to a database (I can query to see the results) and the logging statement before the final return statement succeeds.
    – Howard007
    Nov 21 '18 at 19:21










  • I'm wondering if there is something about the JSON marshalling and un-marshalling. Since these services are in separate JVMs they aren't actually referencing the same instances of the return object.
    – Howard007
    Nov 21 '18 at 19:21












  • Try to add time to your reste template by doing like this : @Bean public RestTemplate restTemplate( RestTemplateBuilder restTemplateBuilder) { return restTemplateBuilder .setConnectTimeout(500) .setReadTimeout(500) .build(); } Else try to tell us what's is the url of every service ?
    – TinyOS
    Nov 21 '18 at 19:38












  • Inexplicably, it seems to be working now; the return message is getting back to the caller. The only change I made related to this was to put the declaration of the return class variable towards the top of the method body. Go figure.
    – Howard007
    Nov 21 '18 at 20:05


















0














I have two Java Spring Boot web service apps on the same server calling each other via REST. Service A calls Service B and the latter successfully acts upon the notfication.
THE PROBLEM is that Service A never receives the acknowlegement from Service B, so it thinks it has failed, and in accordance with its looping recovery logic, it tries again…and again…and again. Service B ends up doing 3 times the work for no added benefit.



The relevant code (stripped down and falsified to protect the guilty) is as follows:



Service A:



public void giveOrderToServiceB(@RequestBody CustomClass message) {
...
org.springframework.web.client.RestTemplate template = new RestTemplate(clientHttpRequestFactory());
com.mycompany.CustomReply reply = template.postForObject(serviceBUrl, message, CustomReply.class);


Service B REST Controller:



@PostMapping(value="ExecuteTheWork", produces=org.springframework.http.MediaType.APPLICATION_JSON_VALUE, consumes=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody CustomReply executeTheWork(@RequestBody CustomClass thing) {
// do something with the thing...
CustomReply reply = new CustomReply();
reply.setReply("Successfully executed the work.");
return reply;
}


The actual exception caught by Service A after calling RestTemplate.postForObject() is



java.net.SocketTimeoutException: Read timed out


Please advise.










share|improve this question






















  • Have you checked that Service B actually finishes it's method? I guess the culprit lies somewhere in // do something with the thing...
    – dunni
    Nov 21 '18 at 18:59










  • Thanks. Yes, it executes, parses an XML file, makes calls to a database (I can query to see the results) and the logging statement before the final return statement succeeds.
    – Howard007
    Nov 21 '18 at 19:21










  • I'm wondering if there is something about the JSON marshalling and un-marshalling. Since these services are in separate JVMs they aren't actually referencing the same instances of the return object.
    – Howard007
    Nov 21 '18 at 19:21












  • Try to add time to your reste template by doing like this : @Bean public RestTemplate restTemplate( RestTemplateBuilder restTemplateBuilder) { return restTemplateBuilder .setConnectTimeout(500) .setReadTimeout(500) .build(); } Else try to tell us what's is the url of every service ?
    – TinyOS
    Nov 21 '18 at 19:38












  • Inexplicably, it seems to be working now; the return message is getting back to the caller. The only change I made related to this was to put the declaration of the return class variable towards the top of the method body. Go figure.
    – Howard007
    Nov 21 '18 at 20:05
















0












0








0







I have two Java Spring Boot web service apps on the same server calling each other via REST. Service A calls Service B and the latter successfully acts upon the notfication.
THE PROBLEM is that Service A never receives the acknowlegement from Service B, so it thinks it has failed, and in accordance with its looping recovery logic, it tries again…and again…and again. Service B ends up doing 3 times the work for no added benefit.



The relevant code (stripped down and falsified to protect the guilty) is as follows:



Service A:



public void giveOrderToServiceB(@RequestBody CustomClass message) {
...
org.springframework.web.client.RestTemplate template = new RestTemplate(clientHttpRequestFactory());
com.mycompany.CustomReply reply = template.postForObject(serviceBUrl, message, CustomReply.class);


Service B REST Controller:



@PostMapping(value="ExecuteTheWork", produces=org.springframework.http.MediaType.APPLICATION_JSON_VALUE, consumes=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody CustomReply executeTheWork(@RequestBody CustomClass thing) {
// do something with the thing...
CustomReply reply = new CustomReply();
reply.setReply("Successfully executed the work.");
return reply;
}


The actual exception caught by Service A after calling RestTemplate.postForObject() is



java.net.SocketTimeoutException: Read timed out


Please advise.










share|improve this question













I have two Java Spring Boot web service apps on the same server calling each other via REST. Service A calls Service B and the latter successfully acts upon the notfication.
THE PROBLEM is that Service A never receives the acknowlegement from Service B, so it thinks it has failed, and in accordance with its looping recovery logic, it tries again…and again…and again. Service B ends up doing 3 times the work for no added benefit.



The relevant code (stripped down and falsified to protect the guilty) is as follows:



Service A:



public void giveOrderToServiceB(@RequestBody CustomClass message) {
...
org.springframework.web.client.RestTemplate template = new RestTemplate(clientHttpRequestFactory());
com.mycompany.CustomReply reply = template.postForObject(serviceBUrl, message, CustomReply.class);


Service B REST Controller:



@PostMapping(value="ExecuteTheWork", produces=org.springframework.http.MediaType.APPLICATION_JSON_VALUE, consumes=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody CustomReply executeTheWork(@RequestBody CustomClass thing) {
// do something with the thing...
CustomReply reply = new CustomReply();
reply.setReply("Successfully executed the work.");
return reply;
}


The actual exception caught by Service A after calling RestTemplate.postForObject() is



java.net.SocketTimeoutException: Read timed out


Please advise.







rest web-services spring-boot socket-timeout-exception






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 18:44









Howard007Howard007

695




695












  • Have you checked that Service B actually finishes it's method? I guess the culprit lies somewhere in // do something with the thing...
    – dunni
    Nov 21 '18 at 18:59










  • Thanks. Yes, it executes, parses an XML file, makes calls to a database (I can query to see the results) and the logging statement before the final return statement succeeds.
    – Howard007
    Nov 21 '18 at 19:21










  • I'm wondering if there is something about the JSON marshalling and un-marshalling. Since these services are in separate JVMs they aren't actually referencing the same instances of the return object.
    – Howard007
    Nov 21 '18 at 19:21












  • Try to add time to your reste template by doing like this : @Bean public RestTemplate restTemplate( RestTemplateBuilder restTemplateBuilder) { return restTemplateBuilder .setConnectTimeout(500) .setReadTimeout(500) .build(); } Else try to tell us what's is the url of every service ?
    – TinyOS
    Nov 21 '18 at 19:38












  • Inexplicably, it seems to be working now; the return message is getting back to the caller. The only change I made related to this was to put the declaration of the return class variable towards the top of the method body. Go figure.
    – Howard007
    Nov 21 '18 at 20:05




















  • Have you checked that Service B actually finishes it's method? I guess the culprit lies somewhere in // do something with the thing...
    – dunni
    Nov 21 '18 at 18:59










  • Thanks. Yes, it executes, parses an XML file, makes calls to a database (I can query to see the results) and the logging statement before the final return statement succeeds.
    – Howard007
    Nov 21 '18 at 19:21










  • I'm wondering if there is something about the JSON marshalling and un-marshalling. Since these services are in separate JVMs they aren't actually referencing the same instances of the return object.
    – Howard007
    Nov 21 '18 at 19:21












  • Try to add time to your reste template by doing like this : @Bean public RestTemplate restTemplate( RestTemplateBuilder restTemplateBuilder) { return restTemplateBuilder .setConnectTimeout(500) .setReadTimeout(500) .build(); } Else try to tell us what's is the url of every service ?
    – TinyOS
    Nov 21 '18 at 19:38












  • Inexplicably, it seems to be working now; the return message is getting back to the caller. The only change I made related to this was to put the declaration of the return class variable towards the top of the method body. Go figure.
    – Howard007
    Nov 21 '18 at 20:05


















Have you checked that Service B actually finishes it's method? I guess the culprit lies somewhere in // do something with the thing...
– dunni
Nov 21 '18 at 18:59




Have you checked that Service B actually finishes it's method? I guess the culprit lies somewhere in // do something with the thing...
– dunni
Nov 21 '18 at 18:59












Thanks. Yes, it executes, parses an XML file, makes calls to a database (I can query to see the results) and the logging statement before the final return statement succeeds.
– Howard007
Nov 21 '18 at 19:21




Thanks. Yes, it executes, parses an XML file, makes calls to a database (I can query to see the results) and the logging statement before the final return statement succeeds.
– Howard007
Nov 21 '18 at 19:21












I'm wondering if there is something about the JSON marshalling and un-marshalling. Since these services are in separate JVMs they aren't actually referencing the same instances of the return object.
– Howard007
Nov 21 '18 at 19:21






I'm wondering if there is something about the JSON marshalling and un-marshalling. Since these services are in separate JVMs they aren't actually referencing the same instances of the return object.
– Howard007
Nov 21 '18 at 19:21














Try to add time to your reste template by doing like this : @Bean public RestTemplate restTemplate( RestTemplateBuilder restTemplateBuilder) { return restTemplateBuilder .setConnectTimeout(500) .setReadTimeout(500) .build(); } Else try to tell us what's is the url of every service ?
– TinyOS
Nov 21 '18 at 19:38






Try to add time to your reste template by doing like this : @Bean public RestTemplate restTemplate( RestTemplateBuilder restTemplateBuilder) { return restTemplateBuilder .setConnectTimeout(500) .setReadTimeout(500) .build(); } Else try to tell us what's is the url of every service ?
– TinyOS
Nov 21 '18 at 19:38














Inexplicably, it seems to be working now; the return message is getting back to the caller. The only change I made related to this was to put the declaration of the return class variable towards the top of the method body. Go figure.
– Howard007
Nov 21 '18 at 20:05






Inexplicably, it seems to be working now; the return message is getting back to the caller. The only change I made related to this was to put the declaration of the return class variable towards the top of the method body. Go figure.
– Howard007
Nov 21 '18 at 20:05














1 Answer
1






active

oldest

votes


















0














OK, I think I got it. I don't send the response back from Service B until after the method has completed all of its work, which can take several seconds to several minutes.
If I immediately answer (and skip the processing), it works consistently.
Need to spin off the actual work to a separate thread.
Cheeers






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418675%2fresttemplate-postforobject-java-net-sockettimeoutexception-read-timed-out-eve%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    OK, I think I got it. I don't send the response back from Service B until after the method has completed all of its work, which can take several seconds to several minutes.
    If I immediately answer (and skip the processing), it works consistently.
    Need to spin off the actual work to a separate thread.
    Cheeers






    share|improve this answer


























      0














      OK, I think I got it. I don't send the response back from Service B until after the method has completed all of its work, which can take several seconds to several minutes.
      If I immediately answer (and skip the processing), it works consistently.
      Need to spin off the actual work to a separate thread.
      Cheeers






      share|improve this answer
























        0












        0








        0






        OK, I think I got it. I don't send the response back from Service B until after the method has completed all of its work, which can take several seconds to several minutes.
        If I immediately answer (and skip the processing), it works consistently.
        Need to spin off the actual work to a separate thread.
        Cheeers






        share|improve this answer












        OK, I think I got it. I don't send the response back from Service B until after the method has completed all of its work, which can take several seconds to several minutes.
        If I immediately answer (and skip the processing), it works consistently.
        Need to spin off the actual work to a separate thread.
        Cheeers







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 20:36









        Howard007Howard007

        695




        695






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418675%2fresttemplate-postforobject-java-net-sockettimeoutexception-read-timed-out-eve%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'