0

Say I have to perform an insert operation on a database, but before that I need to call a function(maybe more than one), that hits a url with the data to be inserted in DB, which returns values that dictate whether the data being held in my app, is eligible for insertion.

void func(DataToInsert data) {
    Promise<ReceivedResponse> response = checkIfDataIsEligibleForInsertion(data));

  response.then(result-> {
      if(result.getoperationstatus().equals("FAILED")) {
          // Log message
      } else{
          performInsertOp(data);
      }
  });
}

Promise<ReceivedResponse> checkIfDataIsEligibleForInsertion(DataToInsert data) {
    // API call performed through an async httpclient and promise returned
}

I often get a gateway timeout. How do I avoid this and ensure that the eligibility checking op completes before insertion operation?

I'm looking for solutions that can be used in a ratpack+java application

marianosimone
  • 3,366
  • 26
  • 32
hisdudeness
  • 438
  • 1
  • 5
  • 15
  • The code you have looks promising to me (although it has some syntax errors which you should fix by editing your question). I don't exactly understand what your problem is. You sequence insertion after checking with `.then` exactly as you wrote. – WorldSEnder Mar 14 '18 at 10:55
  • Are you asking about how to wait for a promise to be resolved before doing the next thing (which is `then`), or are you asking how to "avoid" a gateway timeout? It's not clear from the question, the issues appear to be conflated. – T.J. Crowder Mar 14 '18 at 10:55
  • Also note that in Java, you **never** want to use `==` to compare strings (or rather, in the 0.0000000001% of cases where you do, you'll know it's the right thing). [Use `equals` to compare strings](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). Also, statements that don't have a block attached to them must be terminated with `;` (your `then` call is not). (You probably want to return the result of that call as well, so the caller of `func` can know whether things succeeded or failed.) – T.J. Crowder Mar 14 '18 at 10:57
  • I whipped up the sample code thinking it'll be looked at as pseudo code and didn't think it would be scrutinized this diligently. Thanks for your comments. I've edited the question and the code accordingly. Please do give it a second look. – hisdudeness Mar 14 '18 at 11:06
  • @cricketeer still you don't address what it is that you are asking. There's almost no connection between the code and "I often get a gateway timeout" for example. As I already said, all code that you did give is perfectly sensible albeit so abstract that no further analysis is possible – WorldSEnder Mar 14 '18 at 12:01
  • Anyway, here's a quick guess: maybe you open the http request at the start of your code but processing thakes so long (the request stays open) so that when you finally get around to advance the request, the server decides to 504. – WorldSEnder Mar 14 '18 at 12:07

0 Answers0