简体   繁体   English

Java多线程与多线程的关系

[英]Java many threads to many threads relation

I have a service that gets requests from many clients and after some processing sends a response to the clients. 我有一项服务,可以从许多客户端获取请求,并经过一些处理后将响应发送给客户端。 I use a ThreadPoolExecutor (threadExecuterClient) to handle client requests and put them in a BlockingQueue (requestQueue). 我使用ThreadPoolExecutor(threadExecuterClient)处理客户端请求,并将其放入BlockingQueue(requestQueue)。 Many clients can send concurrent requests. 许多客户端可以发送并发请求。 I have another ThreadPoolExecutor (threadExecuterServer) that processes requests in requestQueue. 我有另一个ThreadPoolExecutor(threadExecuterServer)处理requestQueue中的请求。 This processing is basically consists of send that request to a server and get response. 该处理基本上包括将请求发送到服务器并获得响应。 After processing, I need to send that response to the client which has made that request. 处理后,我需要将该响应发送到发出该请求的客户端。 I am having difficulties to track which client has made which request. 我很难跟踪哪个客户提出了哪个请求。 I basically need to find a way to map the client request to the result of processing. 我基本上需要找到一种将客户端请求映射到处理结果的方法。 The service will be like a gateway. 该服务将像网关一样。

Any idea to handle this issue is appreciated. 任何解决此问题的想法表示赞赏。

Thanks 谢谢

I assume your service accepts requests via HTTP ? 我假设您的服务通过HTTP接受请求? Accept the request from your service and send back a HTTP 202 response. 接受来自您的服务的请求并发送回HTTP 202响应。 This response means that the request was accepted for processing. 该响应表示该请求已接受处理。 When you send the response, send the Location header to tell the client which URL to invoke to ascertain the status of your request. 发送响应时,发送Location标头以告诉客户端要调用哪个URL来确定请求的状态。 The client can poll this URL for status and a result. 客户端可以轮询此URL以获得状态和结果。

The URL should contain a unique ID for each request. 网址应包含每个请求的唯一ID。 Your server can track that and populate a response when it is ready. 您的服务器可以跟踪它,并在准备好后填充响应。

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

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