Home > Software engineering >  Send Response of Rest Api after some time
Send Response of Rest Api after some time

Time:10-10

I have a rest api v1/create/order.I need to implement the below

  • Client A -> calls my create order rest api.
  • I need to return an acknowledgement to the Client immediately
  • Then i will do some processing in background.It will take approximate 50 to 200 sec).
  • Once i complete the step 3 .i need to return the response.Result will have some computed fields that i did in step 3.

How can i implement it ? I am using Java 8 and Spring Boot framework. I can execute step 3 in background thread as it involves some parallel operations.

CodePudding user response:

What you're wishing for is a use-case of server-sent events. REST follows a request-response cycle. In this case, you could use long polling to re-request after the initial response and keeps the connection waiting for the second response.

CodePudding user response:

I think that this "use case" should be faced with asynchronous behaviour.

You should expose two rest endpoints

1. /v1/create/order (or even better POST /v1/order... )

2. /v1/order/{id}

the first return an id for ex. "123" that identify the resource server side or even better a link (to be more "REST") like the following :

/v1/order/123

then you from your client periodically could invoke :

GET /v1/order/123

until you get a result.

Another solution could be to expose an api from the Client A (if it is possibile) and in this way the server could call the Client A when has finished the job. (this is possibile if Client A isitself a service)

  • Related