我需要向许多不同的网络服务发送多个请求并接收结果。问题是,如果我一个一个地发送请求,那么需要逐个发送和处理,时间会很长。
我想知道如何一次性发送所有请求并接收结果。
如下所示的代码显示,我有三个主要方法,每个方法都有自己的子方法。 每个子方法都向其关联的网络服务发送请求并接收结果;因此,例如,要接收web服务9的结果,我必须等待1到8的所有网络服务完成,逐个发送请求并接收它们的结果需要很长时间。
如下所示,没有任何一个方法或子方法彼此相关,因此我可以调用它们所有并以任何顺序接收它们的结果,唯一重要的是接收每个子方法的结果并填充其关联的列表。
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
}