I have a working program that loops through a list (20,000+ items) of UID's, building, connecting, serializing and saving the item properties that were found. Works fine.
What I would like to achieve is to speed it up. Those 20,000+ HTTP requests it has to make and everything after.. its not particularly fast.
Ive tried reading into multithreading and below code, about the connectionManager. Re-using the HttpClient etc. But I'm unable to understand or apply the given code to my situation.
How can I create my code such that it sends out multiple HTTP requests at the same time to speed up the process?
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.build();
Below is my current code, how can I make this process faster?
JSONObject httpJSONObject;
for (int i = 0; i < missingUIDList.size(); i++)
try {
HttpGet get = new HttpGet("https://api.guildwars2.com/v2/items/" + missingUIDList.get(i));
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
httpJSONObject = new JSONObject(result);
itemRoot items = new Gson().fromJson(httpJSONObject.toString(), itemRoot.class);
String name = items.getName().replaceAll("'","''");
connection = DriverManager.getConnection("jdbc:sqlite:gw2.db");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
String cookie = "INSERT INTO item VALUES('" + name +
"','" + items.getDescription() +
"','" + items.getType() +
"'," + items.getLevel() +
",'" + items.getRarity() +
"'," + items.getVendor_value() +
"," + items.getDefault_skin() +
"," + items.getId() +
",'" + items.getChat_link() +
"','" + items.getIcon() +
"');";
System.out.println(cookie);
statement.executeUpdate(cookie);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
EDIT:
With tips from Vadim this is the, hopefully more, optimized code for single threaded.
private void addMissingItems(List<Integer> missingUIDList) {
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response;
HttpEntity entity;
String result;
try {
connection = DriverManager.getConnection("jdbc:sqlite:gw2.db");
statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
} catch (SQLException e) {
System.err.println(e.getMessage());
}
for (int i = 0; i < missingUIDList.size(); i++)
try {
HttpGet get = new HttpGet("https://api.guildwars2.com/v2/items/" + missingUIDList.get(i));
response = client.execute(get);
entity = response.getEntity();
result = EntityUtils.toString(entity);
JSONObject httpJSONObject = new JSONObject(result);
itemRoot items = new Gson().fromJson(httpJSONObject.toString(), itemRoot.class);
System.out.println(httpJSONObject.getInt("id"));
String cookie = "INSERT INTO item VALUES('" + items.getName().replaceAll("'","''") +
"','" + items.getDescription() +
"','" + items.getType() +
"'," + items.getLevel() +
",'" + items.getRarity() +
"'," + items.getVendor_value() +
"," + items.getDefault_skin() +
"," + items.getId() +
",'" + items.getChat_link() +
"','" + items.getIcon() +
"');";
statement.executeUpdate(cookie);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}