how to build a time consuming web service
a simple and theoric question, the answer is probably one, but I would like to listen to some opinions and suggestions. I am in need of realizing a web services (in java) that will launch a time consuming process, which is going to parse some input file and do dome db staff.
What are the best approach to let the user knows that the whole process not only started, but came to an end, with parsing, updating db... ? Because I cannot hang the user waiting for the whole process to finish.
Note: the user is authenticated before starting the process.
EDIT: the web service is accessed not only via web browser, but clients can access it with any client built with the language they want, as long开发者_如何学Go as they refer to the wsdl.
thanks
For your scenario, You should implement asynchronous web services. With asynchronous web services, you have two options.
- (as someone already mentioned) you can do polling of the service to see if the process is done
- You can ask the server to callback
The first approach is easy to implement and it would work with many WS- libraries. However this comes with a drawback of your server having to waste bandwidth for client's polling requests. After all, it's polling. and most of the times, polling is a bad idea.
The latter one wouldn't be as straightforward as the former one to implement. And you I don't know if every JAX-WS library would support it. To support you, i did a quick google search and found this link.
http://www.developer.com/java/web/article.php/3863416/Using-Axis2-and-Java-for-Asynchronous-Web-Service-Invocation-on-the-Client-Side.htm
That might help you a little.
I would give the user a token when they submit the data and have them poll (asynchronously) to another method with their token.
Then, in the 1st method I would add the data to a queue and have another process running on the server check that queue and perform the processing. Then update the token in the DB to notify the user that their job has completed.
I've worked on things like this before and found that firing off a long-running process on a system that is based on HTTP/web is a bad idea as the underlying systems can often time-out the calls to the webservice that are long-running.
精彩评论