I am struggling in sending multiple API requests simultaneously, below are the steps of which I am doing in order to send multiple API calls simultaneously:
- Creating hashmap with 40 authorization values.
- iterating over the hashmap in order to retrieve different authorization value for each call.
I know how to make it sequentially, I want to be able to do it with threads.
This is my code:
private void createUsers() throws ApiException {
AuthAPI authAPI = new AuthAPI(HOST);
usersMap = new HashMap<>();
for (int i = 0; i < 40 - 1; i++) {
String USER_NAME = "Introductory Content User " + StringUtils.getRandomString(5, true, false);
usersAPIUnitTest.createUser(USER_NAME, UserRole.ADMIN, "", "", "");
String email = UsersAPIUnitTest.getEmail();
System.out.println(email);
String userToken = authAPI.generateToken(email);
usersMap.put(USER_NAME, userToken);
}
}
Then I use this method to send the api calls:
public void sendMultipleAPIRequests() throws IOException {
for(Map.Entry mapElement : usersMap.entrySet()){
LabAPIChecker labAPIChecker = new LabAPIChecker(HOST, mapElement.getValue());
JsonObject jsonObject = labAPIChecker.launchLabGetRequest();//this line perform simple 'get' request
}
}