简体   繁体   English

等待服务返回的不良做法?

[英]Bad practice to wait for service to return?

I'm calling a Spring web service which takes approx 5 seconds to return a response. 我正在调用Spring Web服务,大约需要5秒钟才能返回响应。 The webservice call takes place within the Tomcat container prior to being deployed to websphere. webservice调用在部署到websphere之前在Tomcat容器中进行。 I've been told by another developer that the call to this service should be handled in a separate java thread as its "bad practice to have the JVM stuck on one line of code for too long" in this case its stuck on the line of code which calls the web service. 我被另一个开发人员告知,对这个服务的调用应该在一个单独的java线程中处理,因为它是“将JVM卡在一行代码上太长时间的不良做法”,在这种情况下它被卡在了调用Web服务的代码。 I disagree and think that if the web service takes 5 or 10 seconds to complete then that's fine, the response will be received and code execution will continue as normal. 我不同意并认为,如果Web服务需要5或10秒才能完成,那么这很好,将收到响应并且代码执行将继续正常进行。 Am I right ? 我对吗 ?

I'd make two points: 我提出两点:

  1. If the subsequent computation depends on the result of that web service, then you don't have a choice anyway and spawning a new thread is just a waste of resources; 如果后续计算取决于该Web服务的结果,那么您无论如何都没有选择,并且产生新线程只是浪费资源;

  2. if this call is fully independent, then there may be a good reason to offload it to a separate thread. 如果此调用完全独立,则可能有充分的理由将其卸载到单独的线程中。 Even if five seconds is not much on its own, this may not be (now or in the future of your codebase) the only point where the system pointlessly waits for independent tasks to complete. 即使五秒钟本身并不多,这可能不是(现在或代码库的未来)系统毫无意义地等待独立任务完成的唯一点。 It is a generally good practice to let independent tasks run independently. 让独立任务独立运行通常一种很好的做法。

However, if your call is happening within an application deployed into the Tomcat container (this detail is not fully clear from your question), then it is generally a bad practice for a web application to spawn its own child threads. 但是,如果您的调用发生在部署到Tomcat容器中的应用程序中(此问题从您的问题中未完全清楚),那么Web应用程序生成其自己的子线程通常是一种不好的做法。 The tasks should be submitted to a container-provided executor service, if there is one. 如果有任务,则应将任务提交给容器提供的执行程序服务。

Synchronous calling webservices is bad idea, it may be work on integration or testing environment but the response will be exponentiate increasing with load on system. 同步调用web服务是个坏主意,它可能在集成或测试环境上工作,但响应将随着系统负载而增加。 You should look at some kind of asynchronous communication. 你应该看看某种异步通信。

On ours project we used to a lot of sync communication, all was rewritten to async. 在我们的项目中,我们习惯了大量的同步通信,所有这些都被重写为异步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM