Http POSTクライアントからの入力を受け取ってサーバー上の曲に投票するRESTfulサーバーがあります。クライアントにApache HTTPClientを使用しました。
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;
}
連続して投票ボタンをクリックすると、数回(7〜8回など)投票した後、Java.net.SocketTimeoutException: Read timed out
例外。理由を検索したところ、タイムアウト期間にクライアントがサーバーの応答を受け取らなかったためです。ただし、問題は、Chrome =REST ConsoleまたはJMeterのような他のアプリケーションを使用する場合、同じパラメーターを使用して同じサーバーで多くの票を投票できることです。パス。私のJavaコードに問題があります。これを理解するのを手伝ってください。以下は私のスタックトレースです。
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)
調査のためのいくつかのポインタ/提案があります
vote
メソッドを呼び出すことがわかります。HttpClient
インスタンスを使用してサーバーに投稿することをお勧めします。この方法では、クライアント側からの接続が多すぎることはありません。HttpClient
を閉じる必要があるため、httpclient.getConnectionManager().shutdown();
を呼び出して、接続で使用されているリソースを解放します。応答を得るだけでは不十分だと思います。読む必要があると思います(エンティティを取得し、 EntityUtils.consume() を使用して読み取ります)。
例えば(ドキュメントから)
System.out.println("<< Response: " + response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));