Java webservices中的多线程
我在我的应用程序中使用Web服务,但我面临以下性能瓶颈问题。 正在开发旅游领域应用程序。正在搜索旅游服务。 我在寻找航班、酒店、汽车和外汇服务 航班服务包含3个Web服务 1.galileo、Ezego和makemy trip Web服务需要15秒才能获得响应 酒店服务包含3个Web服务 1.galileo、Hotelspro和Bookings.com Web服务需要25秒才能获得响应 汽车服务包含3个Web服务 1.cartrwaler WebServices需要15秒才能获得响应 总共需要60秒才能得到所有服务的全部响应 我以一种顺序的方式使用JAVA编程,我正在使用api。 有谁能建议我如何使用Java技术将性能时间缩短5秒Java webservices中的多线程,java,multithreading,web-services,Java,Multithreading,Web Services,我在我的应用程序中使用Web服务,但我面临以下性能瓶颈问题。 正在开发旅游领域应用程序。正在搜索旅游服务。 我在寻找航班、酒店、汽车和外汇服务 航班服务包含3个Web服务 1.galileo、Ezego和makemy trip Web服务需要15秒才能获得响应 酒店服务包含3个Web服务 1.galileo、Hotelspro和Bookings.com Web服务需要25秒才能获得响应 汽车服务包含3个Web服务 1.cartrwaler WebServices需要15秒才能获得响应 总共需要6
我认为多线程是一种方法,但在我必须使用的多线程概念中,请告诉我。使用ExecutorService提交任务,以调用独立线程中的各个web服务,然后等待返回的未来产生结果。我将执行以下操作: 请记住,ClosableHttpAsyncClient有一堆玩具,您可以使用它们进行调整。 我会仔细阅读HttpClient和closable的文档 这还允许您将池连接管理器与其他3个服务一起使用,并将客户机传递给每个服务,使它们成为单例。这使您可以在一个地方配置和微调所有这些客户机,并以异步方式提供数据
public class FlightService {
CloseableHttpAsyncClient httpClient;
public FlightService(CloseableHttpAsyncClient httpClient){
this.httpClient = httpClient;
}
public static class FlightInfo{
private Map<String, String> airlineDataForService1;
private Map<String, String> airlineDataForService2;
private Map<String, String> airlineDataForService3;
FlightInfo(HttpResponse service1, HttpResponse Service2, HttpResponse service3){
//do stuff with data here
}
}
public FlightInfo getFlightInfo(){
return new FlightInfo(callFlightService1().get(), callFlightService2().get(), callFlightService3().get());
}
private Future<HttpResponse> callFlightService1(){
return callService("flightService1.com");
}
private Future<HttpResponse> callFlightService2(){
return callService("flightService2.com");
}
private Future<HttpResponse> callFlightService3(){
return callService("flightService3.com");
}
private Future<HttpResponse> callService(String serviceUrl){
try{
HttpGet request = new HttpGet(serviceUrl);
httpClient.open();
return httpClient.execute(request, null);
} catch (InterruptedException e) {
//handle retries here
} catch (ExecutionException e) {
//throw 500 here
} finally {
try {
httpClient.close();
} catch (IOException e) {
// throw 500
}
}
return null;
}
一个非常重要的问题。。。哪些框架支持您的服务呼叫?尽管Matt的回答是正确的;这可能会更安全,特别是因为您是多线程新手,可以确保您支持一些关于连接池、超时、重试等的基础知识。然后确保您无法进一步优化http连接。。。一旦这样做了,你就可以自由地移动到多线程,但是当你这么做的时候要考虑的事情。似乎您仍然希望此操作是原子的,因此协调是您需要关注的问题,特别是如果您将所有调用作为一个结果返回给用户。感谢您的回复,请您以详细的方式给我一个简单的示例。您使用什么框架来执行rest调用?只是httpClient?使用弹簧?使用不同的工具?我使用的是httpclient