Sto effettuando una chiamata all'URL REST e cercando di misurare quanto tempo è necessario per ottenere la risposta.Uso non valido di BasicClientConnManager: connessione ancora allocata
Sto utilizzando DefaultHttpClient
per ottenere la risposta dal REST URL
.
Nel mio programma di seguito, ogni thread lavorerà su un determinato intervallo. Come ogni thread funzionerà tra 1 - 100
e il secondo thread funzionerà tra 101 - 200
ecc.
SO nel mio codice qui sotto, per la prima volta funziona bene. Ma per la seconda volta, si sta gettando eccezione su questa linea httpclient.execute
per la seconda volta as-
java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated.
Make sure to release the connection before allocating another one.
qualcosa che non va che sto facendo qui? -
Qui di seguito è la mia code-
class Task implements Runnable {
private DefaultHttpClient httpclient = new DefaultHttpClient();
private HttpGet httpGet;
private HttpResponse response;
@Override
public void run() {
try {
httpGet = new HttpGet(
"http://localhost:8080/service/BEService/v1/get/USERID=10000/profile.ACCOUNT.SERVICE
httpGet.getRequestLine();
for (int userId = id; userId < id + noOfTasks; userId++) {
long start = System.nanoTime();
response = httpclient.execute(httpGet);
long end = System.nanoTime() - start;
}
} catch (Exception e) {
LOG.error("Threw a Exception in " + getClass().getSimpleName(), e);
}
}
}
Codice aggiornato: -
Se lo faccio qualcosa come questo-
012.351.class Task implements Runnable {
private DefaultHttpClient httpclient = new DefaultHttpClient();
private HttpGet httpGet;
private HttpResponse response;
@Override
public void run() {
try {
for (int userId = id; userId < id + noOfTasks; userId++) {
httpGet = new HttpGet("http://localhost:8080/service/BEService/v1/get/USERID=10000/profile.ACCOUNT.SERVICE");
httpGet.getRequestLine();
long start = System.nanoTime();
response = httpclient.execute(httpGet);
long end = System.nanoTime() - start;
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
}
} catch (Exception e) {
LOG.error("Threw a Exception in " + getClass().getSimpleName(), e);
}
}
}
quindi va bene o no?
Grazie Ryan per il suggerimento. Ho aggiornato il codice con le modifiche. Se lo faccio in questo modo, avrò qualche problema? – AKIWEB
È difficile dirlo dai frammenti che hai mostrato. Sembra che tu abbia un HttpClient che attraversa i thread, cosa che non è consentita con il gestore delle connessioni di base. Suppongo che potrebbe anche essere che non hai correttamente [rilasciare le risorse di basso livello] (http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e139) nel primo Astuccio. –
Correggetemi se ho torto ma nel suo codice l'httpClient non è condiviso. In C# un thread è un metodo ma in Java è l'intera classe (jj09.net/multithreading-csharp-vs-java). Quindi sì, il problema è il rilascio delle risorse (https://www.securecoding.cert.org/confluence/x/9gFqAQ, https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache /http/util/EntityUtils.html#consume(org.apache.http.HttpEntity)). –