如何向不同的Web服务发送多个异步请求?
2022-09-03 00:11:00
我需要向许多不同的Web服务发送多个请求并接收结果。问题是,如果我逐个发送请求,只要我需要单独发送和处理所有请求,就需要。
我想知道如何一次发送所有请求并接收结果。
如下面的代码所示,我有三个主要方法,每个方法都有自己的子方法。每个子方法都向其关联的Web服务发送请求并接收结果;因此,例如,要接收Web服务9的结果,我必须等到从1到8的所有Web服务完成,逐个发送所有请求并接收其结果需要很长时间。
如下图所示,没有一个方法和子方法彼此相关,所以我可以调用它们并以任何顺序接收它们的结果,唯一重要的是接收每个子方法的结果并填充它们的关联列表。
private List<StudentsResults> studentsResults = new ArrayList();
private List<DoctorsResults> doctorsResults = new ArrayList();
private List<PatientsResults> patientsResults = new ArrayList();
main (){
retrieveAllLists();
}
retrieveAllLists(){
retrieveStudents();
retrieveDoctors();
retrievePatients();
}
retrieveStudents(){
this.studentsResults = retrieveStdWS1(); //send request to Web Service 1 to receive its list of students
this.studentsResults = retrieveStdWS2(); //send request to Web Service 2 to receive its list of students
this.studentsResults = retrieveStdWS3(); //send request to Web Service 3 to receive its list of students
}
retrieveDoctors(){
this.doctorsResults = retrieveDocWS4(); //send request to Web Service 4 to receive its list of doctors
this.doctorsResults = retrieveDocWS5(); //send request to Web Service 5 to receive its list of doctors
this.doctorsResults = retrieveDocWS6(); //send request to Web Service 6 to receive its list of doctors
}
retrievePatients(){
this.patientsResults = retrievePtWS7(); //send request to Web Service 7 to receive its list of patients
this.patientsResults = retrievePtWS8(); //send request to Web Service 8 to receive its list of patients
this.patientsResults = retrievePtWS9(); //send request to Web Service 9 to receive its list of patients
}