从 servlet 生成线程?
我有一个 servlet,它调用 Web 服务并获取数据来填充 servlet 响应。 我也不想等太久。 我可以生成一个新线程并调用 Web 服务吗?如果不推荐这样做,有什么更好的方法?
谢谢
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 service? If this is not recommended, what is a better way ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
任何事情都可以做。
如果您生成一个新线程,这意味着您必须立即向客户端返回一些内容。会是什么?您的用户最终会想知道数据何时可用。让他们知道的回调机制是什么?他们会投票吗?你要发消息吗?是否有类似 Fedex 的跟踪 ID,他们可以用它来了解数据何时准备好?
您认为需要多少个线程?你会把这些线程集中起来吗?如果数据请求失败会发生什么?
这很复杂,比向上或向下“创建新线程”所暗示的要复杂得多。从头到尾想一想。
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.