简体   繁体   English

Java-同时使用Java中的多线程处理多个HTTP请求

[英]java - multiple http requests at same time With multi threading in java

I'm calling multiple request(10) at same time HTTP GET method.Here calling method will create different threads (Like Thread 1,Thread 2 ....) 我在同一时间通过HTTP GET方法调用多个request(10)。这里调用方法将创建不同的线程(如线程1,线程2 ....)

Caller Method: 呼叫者方法:

enter code here: for(int i=0;i<10;i++){Thread.currentThread().getId();HttpClient httpClient = new HtpClient(url);res = httpClient.get(5000);}

Then Request will hit application entry point.The entry point will create new threads for each and every request (Like Thread 11,Thread 12 ....). 然后Request将命中应用程序入口点。入口点将为每个请求创建新线程(类似于Thread 11,Thread 12 ....)。

enter code here//public void DoProcess(){Thread.currentThread().getId();// New threads for each request.else........}

But i want to know which caller thread request created application thread. 但是我想知道哪个调用者线程请求创建了应用程序线程。

Like Thread 1 belongs Thread 11 就像线程1属于线程11

 Thread 2 belongs Thread 12

Please let me know,how to achieved this. 请让我知道如何实现这一目标。

Client connects through TCP, so there is a socket client ip and port involved. 客户端通过TCP连接,因此涉及套接字客户端ip和端口。 I don't know HTTPClient api by heart but if there is a getClientPort() then you should be able to printout the time, thread name and client ip+port. 我不是很了解HTTPClient api,但是如果有一个getClientPort(),那么您应该可以打印出时间,线程名称和客户端ip + port。 On the server, whatever accept the socket will have the client ip and port too. 在服务器上,接受套接字的任何内容也将具有客户端ip和端口。 If this is a servlet container, the servlet request has getRemoteAddress() and getRemotePort(). 如果这是一个servlet容器,则servlet请求具有getRemoteAddress()和getRemotePort()。 There too you can print out the time, ip+port and thread name. 您也可以在其中打印时间,ip +端口和线程名称。 If you pile those events in 2 tables, you should be able to join by ip+port with a tolerance on the client time vs server time (try less than 2 seconds apart, assuming C and S are on same time with NTP). 如果将这些事件堆放在2个表中,则应该能够通过ip + port加入,并且对客户端时间与服务器时间有一定的容限(假设C和S与NTP同时在同一时间,则尝试相隔少于2秒)。

The other trivial way (but it changes the http payload) is to inject a HTTP header from the client into the http request, stating the current thread name/tid. 另一个简单的方法(但它更改了http有效负载)是将来自客户端的HTTP标头注入http请求,说明当前线程名称/ tid。 Ex: "my_custom_remote_thread_id: Thread-11". 例如:“ my_custom_remote_thread_id:线程11”。 This way on the server you can pull the request header to figure the client thread name/tid. 这样,在服务器上,您可以拉出请求标头以计算客户端线程名称/ tid。

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

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