user2615860
user2615860

Reputation: 33

Java future, promise or?

I have the following scenario.

  1. A user requests some information from my service. However, I don't have the information and have to request it from a third party site.
  2. I make the request (a POST) to the third party site. Rather than providing the information directly, the third part site (which I don't control) expects a callback url in my initial post. So, the request I make for the information returns immediately, but without the information.
  3. When the information is ready, the third party site calls my service via the callback url (from my initial POST) and provides the information (via a POST).
  4. I then want to return the information to the user.

The issue is that I want to block on the original user's request for information until I get the information back via the callback. I'm not sure how I should code that it in java. I'm using spring boot (though I'm not sure how relevant that is). I'm also looking at coding that either as a normal spring boot mvc app or a reactive spring boot app (I'd like to do the latter, but I'm still learning about reactive spring boot). Regardless, I'd be interested in solutions for either framework.

The basic question is how do I block on the initial user information request until the third party site sends the information to the callback url? I've looked at futures and promises and either I don't really understand them or they aren't the droids I'm looking for.

Any suggestions? Thanks in advance.

Upvotes: 0

Views: 895

Answers (1)

Alexei Kaigorodov
Alexei Kaigorodov

Reputation: 13535

If you want to block a thread while waiting the reply from third party, this is useless wasting of resources. You need to prepare an object with all the information about the user's request and assign a uniqe id to it, and to send request to the third party with callback url containing that id, like http://myservice.org/callback?id=123. Start a server to accept that callbacks. That server extracts id from url, finds the user request object with this id from hashmap, and sends the reply.

Upvotes: 1

Related Questions