2013-08-04 28 views
15

Sunucudaki şarkıları oylamak için istemciden bir http POST girdisi alan bir RESTful sunucum var. İstemci için Apache HTTPClient kullandım. (Gibi 7-8), ben java.net.SocketTimeoutException: Read timed out istisna olsun biraz oyla sonra fix java.net.SocketTimeoutException: Zaman aşımına uğradı

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; 
    } 

ben arkaya üzerinde oy butonuna tıklayın

. Nedeni araştırdığımda, bunun nedeni, istemcinin zaman aşımı süresinde sunucu yanıtı almadığı için bunu buldum. Ancak sorun şu ki, Chrome REST Console veya JMeter gibi diğer uygulamaları kullandığımda aynı sunucuda aynı parametreler ve yollarla çok sayıda oy kullanabilirim. Java kodumda bir sorun var mı? Lütfen bunu anlamama yardım et. Aşağıdaki 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) 
+1

Bir okuma zaman aşımı ayarlarsanız ve bir okuma zaman aşımı alırsanız ve okuma zaman aşımı istemezseniz, okuma zaman aşımını artırın. Sadece oluyor, çünkü sen istedin. Sunucunun olduğundan daha hızlı olması gerektiğini düşünüyorsanız, onlarla birlikte alın. – EJP

+2

Okuma zaman aşımını artırmaya çalıştım, ancak er ya da geç bu soruna koşuyordum. Aslında bağlantıları kapatmayı, bağlantıların canlı kalmasını ve sunucu kaynaklarını tüketmesini beklemekteydim. @Santosh tarafından cevap, çok yardımcı oldu. –

cevap

14

(doc itibaren) soruşturma

Ben sana oy her zaman, taze bir HTTP bağlantı oluşturur vote yöntemini çağırın görüyoruz
  1. için birkaç işaretçiler/önerilerdir.
  2. Bu , bir sorun olabilir. Sunucuya göndermek için tek bir HttpClient örneğini kullanmanızı öneririm. Bu şekilde istemci tarafında çok fazla bağlantı oluşturmaz.
  3. Her şeyin sonunda, HttpClient kapatılmalı ve bağlantıların kullandığı kaynakları serbest bırakmak için httpclient.getConnectionManager().shutdown(); numaralı telefonu aramalıdır.
+1

Teşekkürler, sen benim günümü yaptın. Bağlantıyı en sonunda kapatıyorum ve çok yardımcı oldu. –

2

Sadece yanıt almak için yeterli olduğunu sanmıyorum. Bence onu okumalısın (varlığı al ve EntityUtils.consume() aracılığıyla oku).

örn. İşte

 System.out.println("<< Response: " + response.getStatusLine()); 
    System.out.println(EntityUtils.toString(response.getEntity())); 
+0

Aslında buna sahibim, sadece işleri kısaltmaya çalıştım. Sorumu düzenleyeceğim. Teşekkürler. –

İlgili konular