我正在使用Kotlin / Vertx / RxJava / Retrofit服务器,但是在调用耗时太长的外部API时,某些调用阻止了Vertx。
原始处理程序进行呼叫:
val response = weatherService.getWeatherSummaryByCity(countryCode = queryParams[0], adminCode = queryParams[1], cityName = queryParams[2])
这依次执行外部调用:
fun getWeatherSummaryByCity(countryCode: String, adminCode: String, cityName: String): WeatherSummary? {
val citiesList = externalAPI.getLocationByCityName(countryCode = countryCode, adminCode = adminCode, cityName = cityName)
var weatherSummary : WeatherSummary? = null
citiesList
.doOnError { error -> print(error) }
.filter { cityList -> !cityList.isEmpty() }
.map { cityList -> cityList[0] }
.filter { city -> city.Key != null && !city.Key.isEmpty() }
.subscribe( { city: City -> weatherSummary = createWeatherSummary(city) } )
return weatherSummary
}
这是Retrofit使用的界面
interface ExternalAPI {
@GET("/locations/{version}/cities/{countryCode}/{adminCode}/search.json")
fun getLocationByCityName(
@Path("version") version: String = "v1",
@Path("countryCode") countryCode: String,
@Path("adminCode") adminCode: String,
@Query("q") cityName: String,
@Query("apikey") apiKey: String = key,
@Query("details") details: String = "true",
@Query("language") language: String = "en-US"): Observable<List<City>>
}
代码按原样工作,但是如果externalAPI用的时间太长,它将阻止Vertx。当我尝试这样做时,也会发生同样的情况:
Json.encodePrettily(response)
,响应太大。有什么想法可以避免阻塞吗?
答案 0 :(得分:0)
我看到两种解决您的问题的方法:
我建议使用选项1,因为它更干净。