簡體   English   中英

如何使用 RestTemplate,json 使用 java 在同一時間多次發送相同的請求

[英]How to send the same request multiple times in the same time with java using RestTemplate,json

我需要多次發送相同的請求,例如同時發送請求 100 次。 我的代碼看起來像這樣

@SpringBootTest
public class FetchApi {

    String getUrl = "www.https://example/get";
    String postUrl = "www.https://example/post";
    String token = "adadfsdfgsdgdfgdfghdhdhdhdhdhdh";

    @Test
    public void getRequest() {
        try {
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            headers.set("Authorization", "Bearer " + token);

            HttpEntity<String> entity = new HttpEntity<>("", headers);
            Object res = restTemplate.exchange(getUrl, HttpMethod.GET, entity, Object.class);
            System.out.println(res);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void postRequest() {
        try {
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            headers.set("Authorization", "Bearer " + token);
            headers.setContentType(MediaType.APPLICATION_JSON);

            Workflow workflow = new Workflow("adadadasdadadad", "1adadadadadadada0","adafsadfsfsdgfdsfgdfgdg");

            String json = new ObjectMapper().writeValueAsString(workflow);
            HttpEntity<String> entity = new HttpEntity<>(json, headers);

            Object res = restTemplate.exchange(postUrl, HttpMethod.POST, entity, Object.class);
            System.out.println(res);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

如果我從我的班級工作流中獲得價值,但如何將價值從班級更改為 json 文件,則此代碼可以正常工作。 我的 json 文件看起來像

{
    "workflow":{
    "guid":"adadadadadadadad"
    },
    "formType":{
    "guid":"adadadadadadadad"
    },
    "formType":[
    {
    "guid":"adadadadadadadad",
    "svalue":"adadadadadada"
    },
        "guid":"adadadadadadadad",
    "svalue":"adadadadadada"
    },
        "guid":"adadadadadadadad",
    "svalue":"adadadadadada"
    }
 ]
}

有人可以告訴我如何使用 json 同時多次發送請求嗎?

你應該看看ExecutorService

   @Test
   public void postRequest() {
    ExecutorService executors = Executors.newFixedThreadPool(100);
    try {
        List<Callable<Object>> tasks = new ArrayList<>();

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + token);
        headers.setContentType(MediaType.APPLICATION_JSON);

        Workflow workflow = new Workflow("adadadasdadadad", "1adadadadadadada0", "adafsadfsfsdgfdsfgdfgdg");

        String json = new ObjectMapper().writeValueAsString(workflow);
        HttpEntity<String> entity = new HttpEntity<>(json, headers);

        for (int i = 0; i < 100; i++) {
            tasks.add(() ->
                    restTemplate.exchange(getUrl, HttpMethod.GET, entity, Object.class));
        }
        List<Future<Object>> futures = executors.invokeAll(tasks);
        executors.awaitTermination(1, TimeUnit.MINUTES);
        futures.forEach(objectFuture -> {
            if(objectFuture.isDone()){
                try {
                    System.out.println(objectFuture.get());
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        });
        
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        executors.shutdown();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM