resttemplate.exchange() 如何在不同的线程上执行?
How does resttemplate.exchange() execute on a different thread?
据我了解,对 resttemplate 的 exchange 方法的调用是在不同的线程上执行的。基本上所有客户端库都在不同的线程上执行。
假设我的 servlet 容器是 tomcat。当向暴露的端点发出请求时,tomcat 线程接收请求,并且请求从同一线程上的控制器层到达服务层。在服务层,我使用 resttemplate 调用了第三方服务。当调用 exchange 方法时,在内部操作在不同线程上运行并获取操作结果。
我对此有疑问:
resttemplate 基本上是从哪个线程池中获取线程以在不同的线程上执行?
我想知道在不同线程上执行 resttemplate 是否与 tomcat 线程池有任何关系。
任何人都可以阐明这一点吗?
When a request is made to the endpoint exposed, tomcat thread recieves
the request and the request comes to service layer from controller
layer on the same thread.
只有当 tomcat 和 java 应用程序在同一个 JVM 中(如在嵌入式 tomcat 中)时才会发生这种情况。否则,默认情况下,Java 个线程会在不合并的情况下被创建和销毁。当然,你也可以创建一个java线程池。
每次通过 RestTemplate 调用第三方 API 时,它都会创建新的 Httpconnection 并在完成后关闭它。您可以使用 HttpComponentsClientHttpRequestFactory
创建 RestTemplate 自己的连接池,如下所示:
new org.springframework.web.client.RestTemplate(new HttpComponentsClientHttpRequestFactory())
据我了解,对 resttemplate 的 exchange 方法的调用是在不同的线程上执行的。基本上所有客户端库都在不同的线程上执行。
假设我的 servlet 容器是 tomcat。当向暴露的端点发出请求时,tomcat 线程接收请求,并且请求从同一线程上的控制器层到达服务层。在服务层,我使用 resttemplate 调用了第三方服务。当调用 exchange 方法时,在内部操作在不同线程上运行并获取操作结果。
我对此有疑问:
resttemplate 基本上是从哪个线程池中获取线程以在不同的线程上执行?
我想知道在不同线程上执行 resttemplate 是否与 tomcat 线程池有任何关系。
任何人都可以阐明这一点吗?
When a request is made to the endpoint exposed, tomcat thread recieves the request and the request comes to service layer from controller layer on the same thread.
只有当 tomcat 和 java 应用程序在同一个 JVM 中(如在嵌入式 tomcat 中)时才会发生这种情况。否则,默认情况下,Java 个线程会在不合并的情况下被创建和销毁。当然,你也可以创建一个java线程池。
每次通过 RestTemplate 调用第三方 API 时,它都会创建新的 Httpconnection 并在完成后关闭它。您可以使用 HttpComponentsClientHttpRequestFactory
创建 RestTemplate 自己的连接池,如下所示:
new org.springframework.web.client.RestTemplate(new HttpComponentsClientHttpRequestFactory())