Skip to content

Commit 352d9e8

Browse files
committed
Improve HTTP connection
1 parent 9f9c98b commit 352d9e8

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

mathosphere-core/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@
175175
<version>1.3</version>
176176
<scope>test</scope>
177177
</dependency>
178+
<dependency>
179+
<groupId>org.apache.httpcomponents</groupId>
180+
<artifactId>httpclient</artifactId>
181+
<version>4.5</version>
182+
</dependency>
178183
</dependencies>
179184
<developers>
180185
<developer>

mathosphere-core/src/main/java/com/formulasearchengine/mathosphere/mlp/text/TexInfo.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
import org.apache.http.client.HttpClient;
1313
import org.apache.http.client.entity.UrlEncodedFormEntity;
1414
import org.apache.http.client.methods.HttpPost;
15-
import org.apache.http.impl.client.DefaultHttpClient;
15+
import org.apache.http.impl.client.HttpClients;
16+
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
1617
import org.apache.http.message.BasicNameValuePair;
1718
import org.xml.sax.SAXException;
1819

@@ -30,8 +31,16 @@
3031
* Created by Moritz on 28.09.2015.
3132
*/
3233
public class TexInfo {
34+
35+
private static HttpClient client = null;
36+
3337
private static String makeRequest(String tex, String url) {
34-
HttpClient client = new DefaultHttpClient();
38+
if (client == null) {
39+
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
40+
client = HttpClients.custom()
41+
.setConnectionManager(cm)
42+
.build();
43+
}
3544
//HttpPost post = new HttpPost("http://localhost/convert");
3645
HttpPost post = new HttpPost(url);
3746
try {
@@ -45,9 +54,12 @@ private static String makeRequest(String tex, String url) {
4554
while ((line = rd.readLine()) != null) {
4655
result += line;
4756
}
57+
post.releaseConnection();
4858
return result;
4959
} catch (IOException e) {
5060
e.printStackTrace();
61+
} finally {
62+
post.releaseConnection();
5163
}
5264
return "";
5365
}
@@ -56,6 +68,9 @@ public static Multiset<String> getIdentifiers(String tex, String url) throws XPa
5668
final Multiset<String> strings = HashMultiset.create();
5769
//long t0 = System.nanoTime();
5870
String json = makeRequest(tex, url);
71+
if (tex.length() == 0) {
72+
return strings;
73+
}
5974
//System.out.println((System.nanoTime()-t0)/1000000+"ms for "+tex);
6075
try {
6176
JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(json);

0 commit comments

Comments
 (0)