J'ai un serveur RESTful qui prend une http POST entrée du client pour voter des chansons sur le serveur. J'ai utilisé Apache HTTPClient pour le client.
public boolean vote() {
HttpClient client = new DefaultHttpClient(getHttpParameters());
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout Limit
HttpResponse response;
try {
HttpPost post = new HttpPost("http://127.0.0.1:8080/ws/");
StringEntity se = new StringEntity("{ \"song_id\" : \"2\" }");
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
post.setEntity(se);
response = client.execute(post);
if (response != null) {
InputStream in = response.getEntity().getContent(); // Get the data in the Entity
String result = convertStreamToString(in);
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
public static HttpParams getHttpParameters() {
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 30000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
int timeoutSocket = 30000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
return httpParameters;
}
Lorsque je clique sur le bouton de vote sur la succession, après quelques votes (comme 7-8), j'obtiens Java.net.SocketTimeoutException: Read timed out
exception. Lorsque j'ai cherché la raison, j'ai trouvé que c'était parce que le client n'avait pas reçu de réponse du serveur sur la période de temporisation. Mais le problème est que lorsque j'utilise d'autres applications comme Chrome REST Console ou JMeter, je peux voter beaucoup de votes sur le même serveur avec les mêmes paramètres et chemin d'accès. Y a-t-il un problème avec mon Java. S'il vous plaît, aidez-moi à comprendre cela. Voici mon stacktrace:
Java.net.SocketTimeoutException: Read timed out
at Java.net.SocketInputStream.socketRead0(Native Method)
at Java.net.SocketInputStream.read(Unknown Source)
at org.Apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.Java:166)
at org.Apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.Java:90)
at org.Apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.Java:281)
at org.Apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.Java:92)
at org.Apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.Java:62)
at org.Apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.Java:254)
at org.Apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.Java:289)
at org.Apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.Java:252)
at org.Apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.Java:191)
at org.Apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.Java:300)
at org.Apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.Java:127)
at org.Apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.Java:715)
at org.Apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.Java:520)
at org.Apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.Java:906)
at org.Apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.Java:805)
at org.Apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.Java:784)
at notdefault.ServerStuffs.vote(ServerStuffs.Java:72)
at notdefault.MainClass.actionPerformed(MainClass.Java:105)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at Java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at Java.awt.Component.processEvent(Unknown Source)
at Java.awt.Container.processEvent(Unknown Source)
at Java.awt.Component.dispatchEventImpl(Unknown Source)
at Java.awt.Container.dispatchEventImpl(Unknown Source)
at Java.awt.Component.dispatchEvent(Unknown Source)
at Java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at Java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at Java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at Java.awt.Container.dispatchEventImpl(Unknown Source)
at Java.awt.Window.dispatchEventImpl(Unknown Source)
at Java.awt.Component.dispatchEvent(Unknown Source)
at Java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at Java.awt.EventQueue.access$400(Unknown Source)
at Java.awt.EventQueue$2.run(Unknown Source)
at Java.awt.EventQueue$2.run(Unknown Source)
at Java.security.AccessController.doPrivileged(Native Method)
at Java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at Java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at Java.awt.EventQueue$3.run(Unknown Source)
at Java.awt.EventQueue$3.run(Unknown Source)
at Java.security.AccessController.doPrivileged(Native Method)
at Java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at Java.awt.EventQueue.dispatchEvent(Unknown Source)
at Java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at Java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at Java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at Java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at Java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at Java.awt.EventDispatchThread.run(Unknown Source)
Java.net.SocketTimeoutException: Read timed out
at Java.net.SocketInputStream.socketRead0(Native Method)
at Java.net.SocketInputStream.read(Unknown Source)
at org.Apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.Java:166)
at org.Apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.Java:90)
at org.Apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.Java:281)
at org.Apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.Java:92)
at org.Apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.Java:62)
at org.Apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.Java:254)
at org.Apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.Java:289)
at org.Apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.Java:252)
at org.Apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.Java:191)
at org.Apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.Java:300)
at org.Apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.Java:127)
at org.Apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.Java:715)
at org.Apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.Java:520)
at org.Apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.Java:906)
at org.Apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.Java:805)
at org.Apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.Java:784)
at notdefault.ServerStuffs.vote(ServerStuffs.Java:72)
at notdefault.MainClass.actionPerformed(MainClass.Java:105)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at Java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at Java.awt.Component.processEvent(Unknown Source)
at Java.awt.Container.processEvent(Unknown Source)
at Java.awt.Component.dispatchEventImpl(Unknown Source)
at Java.awt.Container.dispatchEventImpl(Unknown Source)
at Java.awt.Component.dispatchEvent(Unknown Source)
at Java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at Java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at Java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at Java.awt.Container.dispatchEventImpl(Unknown Source)
at Java.awt.Window.dispatchEventImpl(Unknown Source)
at Java.awt.Component.dispatchEvent(Unknown Source)
at Java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at Java.awt.EventQueue.access$400(Unknown Source)
at Java.awt.EventQueue$2.run(Unknown Source)
at Java.awt.EventQueue$2.run(Unknown Source)
at Java.security.AccessController.doPrivileged(Native Method)
at Java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at Java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at Java.awt.EventQueue$3.run(Unknown Source)
at Java.awt.EventQueue$3.run(Unknown Source)
at Java.security.AccessController.doPrivileged(Native Method)
at Java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at Java.awt.EventQueue.dispatchEvent(Unknown Source)
at Java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at Java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at Java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at Java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at Java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at Java.awt.EventDispatchThread.run(Unknown Source)
Voici quelques conseils/suggestions d'enquête
vote
qui crée une nouvelle connexion HTTP.HttpClient
pour publier sur le serveur. De cette façon, il ne créera pas trop de connexions du côté client.HttpClient
doit être fermé et donc appeler httpclient.getConnectionManager().shutdown();
pour libérer les ressources utilisées par les connexions.Je ne pense pas que cela suffise simplement pour obtenir la réponse. Je pense que vous devez le lire (obtenir l'entité et le lire via EntityUtils.consume () ).
par exemple. (extrait du doc)
System.out.println("<< Response: " + response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));