简体   繁体   English

如何使Web服务使用多个线程?

[英]How to make a Web Service be use multiple threads?

I have a simple java web service. 我有一个简单的java Web服务。 I want to have the web service be threaded, where each call to a web method gets its own thread to do processing. 我想让Web服务具有线程,每次调用Web方法都有自己的线程进行处理。 The processing takes a long time and I don't want it to block, prevent other calls to the web method from happening. 处理需要很长时间,我不希望它阻止,阻止其他调用Web方法发生。 Instead, I want the web method to only create the Threads and for the Thread itself to respond/return a value to the client after its done processing. 相反,我希望web方法只创建线程,并且线程本身在完成处理后响应/返回值给客户端。 Is that possible? 那可能吗?

EDIT: 编辑:

Here's a semi-pseudocode of what I have right now: 这是我现在所拥有的半伪代码:

@WebService(endpointInterface="Service")
public class ServiceImpl {

    public ServiceImpl() 
    {
        // Initialization
    }

    public String GetResult(input)
    {
        // Does long processing

        return Result;
    }
}

What I want to do is instead of GetResult() doing the long processing, I want it only spawn the Runnable that will do the long processing and have GetResult() return and ready to service another request. 我想做的是代替GetResult()进行长处理,我希望它只生成Runnable,它将执行长处理并让GetResult()返回并准备好为另一个请求提供服务。 I also want the Runnable to respond to the waiting client. 我还希望Runnable响应等待的客户端。

EDIT 2: 编辑2:

I just realized that I'm asking a silly question. 我才意识到我在问一个愚蠢的问题。 I'm fairly new to implement WS's. 我很擅长实现WS。 I had thought that WS's only took one request at a time sequentially. 我原以为WS只按顺序一次请求一个请求。 I didn't know each request is already automatically threaded. 我不知道每个请求是否已经自动线程化。

Yes, you can use JMS for asynchronous calls. 是的,您可以使用JMS进行异步调用。 That's the best way. 这是最好的方式。

You could also fire up a thread, and manage them yourself. 你也可以启动一个线程,并自己管理它们。 That's not generally recommended. 通常不建议这样做。 Good management of the thread pool, and keeping track of which threads might be taking to long requires a lot of overhead code. 良好的线程池管理,以及跟踪哪些线程可能需要很长时间需要大量的开销代码。

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

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