2017-07-28 13 views
1

Javaサービスでretrofit2を使用して、REST APIに接続してデータを取得しています。Retrofit2 - OkHttp ConnectionPoolスレッドは100以上のスレッドになります。どうして?

コードは次のようになります。

Retrofit retrofit = 
     new Retrofit.Builder().baseUrl(endPoint).addConverterFactory(JacksonConverterFactory.create()) 
           .build(); 

SyncCentralLojaProxySvc svc = retrofit.create(SyncCentralLojaProxySvc.class); 

LogVerCentralLojaEntity entity = syncSvc.getLogVerByCdFilial(filial); 
long cd_log = (entity != null) ? entity.getCdLog() : 0; 

Call<LogCentralLojaCompactoCollectionDto> call = svc.getLogCompacto(filial, cd_log); 

Response<LogCentralLojaCompactoCollectionDto> response = call.execute(); 

//NOT_MODIFIED 
if (response.code() == 304) { 
    return 0; 
} 

if (!response.isSuccessful()) 
    throw new IOException(response.errorBody().string()); 

LogCentralLojaCompactoCollectionDto body = response.body(); 

そのシンプルなデータは、それが数秒ごとに(ない並行して)同期動作しますフェッチ。

私は、VisualVMがOkHttpのトレードが大きくなりすぎていることに気付きました。このアプリケーションは100回の操作を並列に行うことはありません。実際には、1つだけ必要です。

これを調整するにはどうすればよいですか?非常に多くのスレッドを持つのは当然ですか?

VisualVM Thread Monitor

答えて

2

が問題を解決:

ConnectionPool pool = new ConnectionPool(5, 10000, TimeUnit.MILLISECONDS); 

OkHttpClient client = new OkHttpClient.Builder() 
           .connectionPool(pool) 
           .build(); 

Retrofit retrofit = 
     new Retrofit.Builder().baseUrl(endPoint) 
           .client(client) 
           .addConverterFactory(JacksonConverterFactory.create()) 
           .build(); 
+0

キープアライブ期間では10秒がおそらく小さすぎます。 OkHttpはデフォルトでこれに5分を使用します。 –

1

これは正常ではありません。 Retrofitインスタンスを1つ作成して再利用していますか?またはリクエストごとに1つずつ作成しますか?接続プールの設定でグローバルクライアント設定

+0

私はリクエストごとに1つずつ作成するが、それは助けにはならない唯一の要求を作成するために変更することだ、それはまだ同じスレッドを作成します。 –

関連する問題