[英]How to send different web services requests to different destinations at the same time using Java?
I need to make different types of requests (RESTful and SOAP) at the same time using my Web application. 我需要使用Web应用程序同时发出不同类型的请求(RESTful和SOAP)。
As I am newbie to this topic have been overwhelmed with information that I have found. 由于我是这个主题的新手,所以我发现的信息不知所措。 I am wondering if it is possible, if yes how to implement it or what exactly should I look for? 我想知道是否有可能,如果可以的话,该如何执行呢?我到底应该寻找什么?
Example 例
two method from Class A A类的两种方法
a method from Class B B类的方法
a method from Class C C类的方法
User make a request and all these four methods make their request based on user's provided criteria send their requests at the same time to the their respective destination and receive the response. 用户发出请求,所有这四种方法都根据用户提供的条件发出请求,同时将其请求发送到各自的目的地并接收响应。
You would need to multi-thread your application. 您将需要对应用程序进行多线程处理。 Some helpful tutorials are available here and here . 此处和此处提供了一些有用的教程。
Essentially, you would need to have 4 classes which extend the Thread
class or implement the Runnable
interface. 本质上,您将需要具有4个类来扩展Thread
类或实现Runnable
接口。 The purpose of these classes would be to execute a request and eventually process the response from your services. 这些类的目的是执行一个请求,并最终处理来自您的服务的响应。
In your main class, you would simply need to create new instances of these 4 classes and spawn them off. 在您的主类中,您只需要创建这4个类的新实例并生成它们即可。
Have a look at the following code which I've found here 看一下我在这里找到的以下代码
package com.scranthdaddy;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
public static void main(String[] args) throws Exception {
Main main = new Main();
main.runBatchThreads();
}
private void runBatchThreads() {
// initialize list of WebServiceTask objects
List<WebServiceTask> webServiceTasks = new ArrayList<WebServiceTask>();
for (int i = 0; i < 5000; i++) {
WebServiceTask webServiceTask = new WebServiceTask();
webServiceTasks.add(webServiceTask);
}
System.out.println("Starting threads");
// create ExecutorService to manage threads
ExecutorService executorService = Executors.newFixedThreadPool(20);
for (WebServiceTask webServiceTask : webServiceTasks) {
// start thread
executorService.execute(webServiceTask);
}
// shutdown worker threads when complete
executorService.shutdown();
System.out.println("Threads started, main ended");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.