2011-11-23 24 views
6

Restletを使用してWebサービスを実装しています。クライアントはRestletを使用して多数の連続した呼び出しを行いますが、少数の呼び出しが正常に完了した後、さらにメッセージが表示されます:RestletクライアントからRestletサーバーへの繰り返し呼び出しがハングする

INFO:新しい接続とトランザクションの受け入れを停止します。 。スレッドの最大数を増やすことを検討してください。

私が試した:

getContext().getParameters().add("maxThreads", "200"); 

が、それが解決しないし。いずれにしても、クライアントは無制限の通話を行えるようになり、maxThreadsを増加させることが限界に達しているようです。それは私がいくつかのリソースを解放していないか、各クライアント呼び出しの後に切断するように見えますが、私はそうする方法を知らない。

次の(私が作ることができるほど小さい)スタンドアロンプ​​ログラムは問題を示しています。これは、単純なサーバを起動し、クライアントは倍の束それを呼び出す:クライアントとすべて作品に

Response response = clientResource.getResponse(); 
    Status status = response.getStatus(); 
    clientResource.release(); // add this line 

:クライアントがリソースを解放するように

/** You may copy, modify, and re-use this code as you see fit - Jim Irrer */ 
import java.io.ByteArrayInputStream; 
import java.io.IOException; 
import java.io.InputStream; 

import org.restlet.Application; 
import org.restlet.Component; 
import org.restlet.Request; 
import org.restlet.Response; 
import org.restlet.Restlet; 
import org.restlet.Server; 
import org.restlet.data.MediaType; 
import org.restlet.data.Method; 
import org.restlet.data.Protocol; 
import org.restlet.data.Status; 
import org.restlet.representation.InputRepresentation; 
import org.restlet.representation.Representation; 
import org.restlet.resource.ClientResource; 
import org.restlet.resource.Directory; 

public class SimpleServerPut extends Component implements Runnable { 
    private static final int PORT = 8080; 

    private static int readToByteArray(InputStream inputStream, byte[] buf) throws IOException { 
     int length = 0; 
     int b; 
     while ((b = inputStream.read()) != -1) { 
      buf[length++] = (byte)b; 
     } 
     return length; 
    } 

    @Override 
    public void run() { 
     getContext().getParameters().add("maxThreads", "200"); 

     // Create the HTTP server and listen on port PORT 
     SimpleServerPut simpleServer = new SimpleServerPut(); 
     Server server = new Server(Protocol.HTTP, PORT, simpleServer); 
     simpleServer.getClients().add(Protocol.FILE); 

     // Create an application 
     Application application = new Application(simpleServer.getContext()) { 
      @Override 
      public Restlet createRoot() { 
       return new Directory(getContext(), "C:"); 
      } 
     }; 

     // Attach the application to the component and start it 
     simpleServer.getDefaultHost().attach("/stuff/", application); 
     try { 
      server.start(); 
     } 
     catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    @Override 
    public void handle(Request request, Response response) { 
     // assume the worst 
     response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED); 
     response.setEntity("No no - Bad client! Only do PUTs.", MediaType.TEXT_PLAIN); 

     try { 
      if (request.getMethod() == Method.PUT) { 
       InputStream inputStream = request.getEntity().getStream(); 
       byte[] buf = new byte[64*1024]; 
       int totalLength = readToByteArray(inputStream, buf); 
       response.setStatus(Status.SUCCESS_OK); 
       String msg = "Number of bytes received: " + totalLength; 
       response.setEntity(msg, MediaType.TEXT_PLAIN); 
       System.out.println("server: " + msg); 
       return; 
      } 
     } 
     catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    private static String callServer() throws IOException { 
     String urlText = "http://localhost:" + PORT + "/"; 
     ClientResource clientResource = new ClientResource(urlText); 
     clientResource.setReferrerRef(urlText); 

     byte[] buf = new byte[1000]; 
     for (int i = 0; i < buf.length; i++) { 
      buf[i] = (byte)((int)'a' + (i%26)); 
     } 
     ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buf); 
     Representation representation = new InputRepresentation(byteArrayInputStream, MediaType.APPLICATION_OCTET_STREAM); 
     Representation representation2 = clientResource.put(representation); 
     byte[] responseBuf = new byte[16*1024]; 
     int length = readToByteArray(representation2.getStream(), responseBuf); 
     Response response = clientResource.getResponse(); 
     Status status = response.getStatus(); 
     return "status: " + status + " message: " + new String(responseBuf, 0, length); 
    } 

    // Start server and call it a bunch of times 
    public static void main(String[] args) throws Exception { 
     SimpleServerPut simpleServer = new SimpleServerPut(); 
     new Thread(simpleServer).start(); 
     Thread.sleep(200); // cheap trick to make sure that server is running 
     // make a bunch of client calls 
     for (int t = 0; t < 100; t++) { 
      System.out.println("client count: " + (t+1) + " " + callServer()); 
     } 
     System.exit(0); 
    } 
} 
+1

maxThreadsキー値ペアparam org.restlet.Serverではなく、org.restlet.Serverにeterを設定する必要があります。このように: 'Server server = mycomponent.getServers()。add(Protocol.HTTP、" localhost "、9090); server.getContext()。getParameters()。add( "maxThreads"、 "20"); ' –

答えて

4

は、行を追加します。最終的に、クライアントが終了するとサーバーはタイムアウトしますが、それはしばらく時間がかかります。

0

ClientResource.release()を呼び出すことに加えて、表現に対してexhaust()を呼び出すことができます。

Representation responseRepresentation = response.getEntity(); 
if (responseRepresentation != null) { 
    try { 
     responseRepresentation.exhaust(); 
    } catch (IOException e) { 
     // handle exception 
    } 
    responseRepresentation.release(); 
} 

this threadに関連するディスカッション。私は私が使用してきたRestlet API

どうやらの.jarの最後の安定版リリースをダウンロードし、私の問題を解決してきました

1

release()コマンドは、任意の効果がありません古いバージョンでした。

Sep 05, 2012 9:50:19 AM org.restlet.engine.http.connector.HttpClientHelper start 
INFO: Starting the default HTTP client 

は今、それはあまりにもストップをoutputingされています:

Sep 05, 2012 9:50:19 AM org.restlet.engine.http.connector.HttpClientHelper stop 
INFO: Stopping the default HTTP client 
+0

使用しているバージョンと問題を修正したバージョンを明示してもよろしいですか?それは他の人(私も含めて)が問題をデバッグするのに役立ちます。 –

5

我々は停止することによって、問題を解決することしかできなかったクライアントのログはクライアントだけの開始を出力し、更新する前に

ClientResourceの関連クライアントを直接(Restletバージョン2.0.15を使用):

Client c = (Client)clientResource.getNext(); 
try { 
    c.stop(); 
} catch (Exception e) { 
    //handle exception 
} 
+0

スレッドを速やかに終了させるためにこれをやらなければならなかったようですが、これは特定バージョンのRestletの問題でしょうか? (上記の@ kassius-vargas-prestesの答えをご覧ください) - あなたはどのバージョンを使用していますか? –

+1

使用したバージョンは2.0です。15、現時点では最新の安定バージョン – mahnkong

+0

ありがとう、私たちは同じバージョンを使用しているので、ここで明示的な.stop()をコードベースに残します。 –

関連する問題