简体   繁体   English

如何使用Java将不同的Web服务请求同时发送到不同的目的地?

[英]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.

相关问题 如何向不同的Web服务发送多个异步请求? - How to send multiple asynchronous requests to different web services? 一次将数据包发送到不同的目的地 - Sending a packet to different destinations in one time 如何让服务器几乎同时向不同位置的 2 个客户端发送消息。 (在java中实现) - how to make server send a message to 2 clients at different location at almost same time. (implementation in java) 如何在Java Web Services中处理HTTP请求? - How are HTTP requests handled in Java Web Services? 如何在Java Spring中的不同请求中使用SET的相同实例 - How to use the same instance of a SET across different requests in Java Spring JMeter-多个请求同时但值不同 - JMeter - Multiple requests at the same time but with different values 如何使用Spring将并发请求发送到不同的Web服务 - How to send concurrent requests to different webservices using spring 在Eclipse中为不同项目保存不同的Java JAR导出目标 - Saving different Java JAR export destinations for different projects in Eclipse 为什么我的两个不同的angularjs服务解析为相同的静态Java EE Web服务? - Why are my two different angularjs services resolving to the same restful Java EE web service? 如何使用Java在硒中控制两个不同的帐户是否可以同时调用? - how can i control whether two different accounts can call or not at the same time in selenium using Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM