开发者

Wait for data to download in RESTful Webservice

开发者 https://www.devze.com 2023-02-25 07:50 出处:网络
I am sending one request to fetch data on the server side RESTful webservices, now in my webservice I have written a code to fetch historical data from external service,

I am sending one request to fetch data on the server side RESTful webservices, now in my webservice I have written a code to fetch historical data from external service, when the all historical data is being fetched from external services a notification function is called that indicates that downloadi开发者_如何学运维ng data is finished.

The problem is when I send a request to my webservices, my webservices calls the function to fetch data from external services and doesn't wait for notification and returns back. To make it wait I need to put a delay.

So is this a right way to do it or there are other alternatives because I can't predict how much time a webservices will take to download data.


You have to create a "Transaction Resource" that identifies the long opeation. Here is a good explanation:

One of the common criticism of REST is because it is so tied in to HTTP (which doesn't support a client callback mechanism), doing asynchronous service or notification on REST is hard. So how do we implement long running transactions (which typically require asynchronicity and callback support) in REST ?

The basic idea is to immediately create a "Transaction Resource" to return back to the client. While the actual processing happens asynchronously in the background, the client at any time, can poll the "Transaction Resource" for the latest processing status. Lets look at an example to request for printing a book, which may take a long time to complete

(example ...)

Note that a response is created immediately which contains the URI of a transaction resource, even before the print job is started. Client can poll the transaction resource to obtain the latest status of the print job.

(obtained from "Common REST Design Pattern").


I would suggest the use of a caching mechanism. It's historical data so I would imagine it doesn't change that much, refresh the cache by a different process.

Your WebService can read from the cache.


Use a java.util.concurrent.CountDownLatch.

  1. Create it and set the count to one (1) before calling the external service
  2. Wait on the latch
  3. Have the notification function call countDown() on the latch.
0

精彩评论

暂无评论...
验证码 换一张
取 消