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
.
- Create it and set the count to one (1) before calling the external service
- Wait on the latch
- Have the notification function call countDown() on the latch.
精彩评论