Spawn thread from servlet?
I have a servlet, that calls a web service, and gets data to populate the servlet response. I also don't want to wait too long. Can I just spawn a new thread and call the web se开发者_JAVA技巧rvice? If this is not recommended, what is a better way ?
Thanks
Anything can be done.
If you spawn a new thread, that means that you'll have to return something immediately to the client. What will it be? Your user will eventually want to know when the data is available. What's the callback mechanism for letting them know? Will they poll for it? Are you going to send a message? Is there a Fedex-like tracking ID that they can use to find out when the data's ready?
How many threads do you think you'll need? Will you pool those threads? What happens if a data request fails?
It's complicated, much more so than a "create a new thread" up or down would imply. Think it all the way through.
精彩评论