【发布时间】:2021-05-17 04:06:27
【问题描述】:
我在我的服务器应用程序上使用 okhttp 和 ktor 并像这样运行。
runBlocking {
launch {
okhttpClient.get(url)
}
}
这是记录结果。
2021-05-17 11:18:21.620 DEBUG 11928 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : GET "/test/test2", parameters={}
2021-05-17 11:18:21.621 DEBUG 11928 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.example.kotlinstudy.TestController#test2()
2021-05-17 11:18:21.788 DEBUG 11928 --- [alhost:8081/...] okhttp3.internal.concurrent.TaskRunner : Q10000 scheduled after 0 µs: OkHttp ConnectionPool
2021-05-17 11:18:21.789 DEBUG 11928 --- [Http TaskRunner] okhttp3.internal.concurrent.TaskRunner : Q10000 starting : OkHttp ConnectionPool
2021-05-17 11:18:21.789 DEBUG 11928 --- [ ConnectionPool] okhttp3.internal.concurrent.TaskRunner : Q10000 run again after 300 s : OkHttp ConnectionPool
2021-05-17 11:18:21.789 DEBUG 11928 --- [Http TaskRunner] okhttp3.internal.concurrent.TaskRunner : Q10000 finished run in 287 µs: OkHttp ConnectionPool
2021-05-17 11:18:21.828 DEBUG 11928 --- [@call-context#5] okhttp3.internal.concurrent.TaskRunner : Q10000 scheduled after 0 µs: OkHttp ConnectionPool
2021-05-17 11:18:21.828 DEBUG 11928 --- [Http TaskRunner] okhttp3.internal.concurrent.TaskRunner : Q10000 starting : OkHttp ConnectionPool
2021-05-17 11:18:21.828 DEBUG 11928 --- [ ConnectionPool] okhttp3.internal.concurrent.TaskRunner : Q10000 run again after 300 s : OkHttp ConnectionPool
2021-05-17 11:18:21.828 DEBUG 11928 --- [Http TaskRunner] okhttp3.internal.concurrent.TaskRunner : Q10000 finished run in 92 µs: OkHttp ConnectionPool
2021-05-17 11:18:21.883 DEBUG 11928 --- [nio-8080-exec-2] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/plain', given [*/*] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json, application/json, application/*+json]
2021-05-17 11:18:21.883 DEBUG 11928 --- [nio-8080-exec-2] m.m.a.RequestResponseBodyMethodProcessor : Writing ["test1"]
2021-05-17 11:18:21.890 DEBUG 11928 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed 200 OK
它已启动 tomcat nio 线程并在 okhttp 线程上运行。 你知道协程是轻线程。我希望 runBlocking 代码在协程线程上工作。 但它似乎是另一个线程。
那么 okhttpClent 在协程线程上运行?还是普通线程?
使用协程线程应该怎么做?
使用 CIO 客户端?
谢谢大家。
这是 CIO 通话记录。它看起来像工作协程线程
2021-05-17 13:05:16.271 DEBUG 20068 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : GET "/test/test3", parameters={}
2021-05-17 13:05:16.273 DEBUG 20068 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.example.kotlinstudy.TestController#test3()
2021-05-17 13:05:16.306 INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient : REQUEST: http://localhost:8081/server1
2021-05-17 13:05:16.307 INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient : METHOD: HttpMethod(value=GET)
2021-05-17 13:05:16.307 INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient : COMMON HEADERS
2021-05-17 13:05:16.308 INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient : -> Accept: application/json
2021-05-17 13:05:16.309 INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient : -> Accept-Charset: UTF-8
2021-05-17 13:05:16.309 INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient : -> User-Agent: Ktor client/1.3.1(npay-point)
2021-05-17 13:05:16.309 INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient : CONTENT HEADERS
2021-05-17 13:05:16.309 INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient : -> Content-Length: 0
2021-05-17 13:05:16.423 INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient : RESPONSE: 200 OK
2021-05-17 13:05:16.423 INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient : METHOD: HttpMethod(value=GET)
2021-05-17 13:05:16.423 INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient : FROM: http://localhost:8081/server1
2021-05-17 13:05:16.423 INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient : COMMON HEADERS
2021-05-17 13:05:16.423 INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient : -> Content-Length: 20
2021-05-17 13:05:16.423 INFO 20068 --- [-2 @coroutine#5] io.ktor.client.HttpClient : -> Content-Type: text/plain; charset=UTF-8
Hello world! server1
2021-05-17 13:05:16.467 DEBUG 20068 --- [nio-8080-exec-2] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/plain', given [*/*] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json, application/json, application/*+json]
2021-05-17 13:05:16.468 DEBUG 20068 --- [nio-8080-exec-2] m.m.a.RequestResponseBodyMethodProcessor : Writing ["test1"]
2021-05-17 13:05:16.475 DEBUG 20068 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed 200 OK
【问题讨论】: