gpt4 book ai didi

java - 如何向不同的网络服务发送多个异步请求?

转载 作者:太空狗 更新时间:2023-10-29 23:02:58 25 4
gpt4 key购买 nike

我需要向许多不同的网络服务发送多个请求并接收结果。问题是,如果我一个一个地发送请求,只要我需要单独发送和处理所有请求,就会花费很长时间。

我想知道如何一次发送所有请求并接收结果。

如以下代码所示,我有三个主要方法,每个方法都有自己的子方法。每个子方法向其关联的 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

}

最佳答案

这是一种简单的 fork-join 方法,但为了清楚起见,您可以启动任意数量的线程并稍后在结果可用时检索结果,例如这种方法。

    ExecutorService pool = Executors.newFixedThreadPool(10);
List<Callable<String>> tasks = new ArrayList<>();
tasks.add(new Callable<String>() {
public String call() throws Exception {
Thread.sleep((new Random().nextInt(5000)) + 500);
return "Hello world";
}

});
List<Future<String>> results = pool.invokeAll(tasks);

for (Future<String> future : results) {
System.out.println(future.get());
}
pool.shutdown();

更新,完成:

这是一个冗长但可行的解决方案。我临时写的,并没有编译它。鉴于三个列表具有不同的类型,并且 WS 方法是独立的,因此不是确实是模块化的,但请尝试使用您最好的编程技能,看看是否可以更好地模块化它。

    ExecutorService pool = Executors.newFixedThreadPool(10);

List<Callable<List<StudentsResults>>> stasks = new ArrayList<>();
List<Callable<List<DoctorsResults>>> dtasks = new ArrayList<>();
List<Callable<List<PatientsResults>>> ptasks = new ArrayList<>();

stasks.add(new Callable<List<StudentsResults>>() {
public List<StudentsResults> call() throws Exception {
return retrieveStdWS1();
}

});
stasks.add(new Callable<List<StudentsResults>>() {
public List<StudentsResults> call() throws Exception {
return retrieveStdWS2();
}

});
stasks.add(new Callable<List<StudentsResults>>() {
public List<StudentsResults> call() throws Exception {
return retrieveStdWS3();
}

});

dtasks.add(new Callable<List<DoctorsResults>>() {
public List<DoctorsResults> call() throws Exception {
return retrieveDocWS4();
}

});
dtasks.add(new Callable<List<DoctorsResults>>() {
public List<DoctorsResults> call() throws Exception {
return retrieveDocWS5();
}

});
dtasks.add(new Callable<List<DoctorsResults>>() {
public List<DoctorsResults> call() throws Exception {
return retrieveDocWS6();
}

});

ptasks.add(new Callable<List<PatientsResults>>() {
public List<PatientsResults> call() throws Exception {
return retrievePtWS7();
}

});
ptasks.add(new Callable<List<PatientsResults>>() {
public List<PatientsResults> call() throws Exception {
return retrievePtWS8();
}

});
ptasks.add(new Callable<List<PatientsResults>>() {
public List<PatientsResults> call() throws Exception {
return retrievePtWS9();
}

});

List<Future<List<StudentsResults>>> sresults = pool.invokeAll(stasks);
List<Future<List<DoctorsResults>>> dresults = pool.invokeAll(dtasks);
List<Future<List<PatientsResults>>> presults = pool.invokeAll(ptasks);

for (Future<List<StudentsResults>> future : sresults) {
this.studentsResults.addAll(future.get());
}
for (Future<List<DoctorsResults>> future : dresults) {
this.doctorsResults.addAll(future.get());
}
for (Future<List<PatientsResults>> future : presults) {
this.patientsResults.addAll(future.get());
}
pool.shutdown();

每个 Callable 都会返回一个结果列表,并在其自己的单独线程中调用。
当您调用 Future.get() 方法时,您会将结果返回到主线程。
Callable 完成之前,结果NOT 不可用,因此不存在并发问题。

关于java - 如何向不同的网络服务发送多个异步请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21670451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com