Use daemon thread pool for AsyncHttpClient in emitters (#5057)

* use daemon thread pool for AsyncHttpClient in emitters

* changed to use existing helper methods

* refactored creation of AsyncHttpClient
This commit is contained in:
Goh Wei Xiang 2017-11-09 17:38:19 -08:00 committed by Gian Merlino
parent 2ecebb3173
commit 81f249874b
2 changed files with 28 additions and 21 deletions

View File

@ -32,6 +32,7 @@ import com.metamx.emitter.core.HttpPostEmitter;
import io.druid.guice.JsonConfigProvider;
import io.druid.guice.LazySingleton;
import io.druid.guice.ManageLifecycle;
import io.druid.java.util.common.concurrent.Execs;
import io.druid.java.util.common.lifecycle.Lifecycle;
import io.netty.handler.ssl.ClientAuth;
import io.netty.handler.ssl.JdkSslContext;
@ -68,6 +69,19 @@ public class HttpEmitterModule implements Module
binder.bind(SSLContext.class).toProvider(Providers.of(context)).in(LazySingleton.class);
}
static AsyncHttpClient createAsyncHttpClient(
String nameFormat,
@Nullable SSLContext sslContext
)
{
final DefaultAsyncHttpClientConfig.Builder builder = new DefaultAsyncHttpClientConfig.Builder()
.setThreadFactory(Execs.makeThreadFactory(nameFormat));
if (sslContext != null) {
builder.setSslContext(new JdkSslContext(sslContext, true, ClientAuth.NONE));
}
return new DefaultAsyncHttpClient(builder.build());
}
@Provides
@ManageLifecycle
@Named("http")
@ -78,13 +92,12 @@ public class HttpEmitterModule implements Module
ObjectMapper jsonMapper
)
{
final DefaultAsyncHttpClientConfig.Builder builder = new DefaultAsyncHttpClientConfig.Builder();
if (sslContext != null) {
builder.setSslContext(new JdkSslContext(sslContext, true, ClientAuth.NONE));
}
final AsyncHttpClient client = new DefaultAsyncHttpClient(builder.build());
lifecycle.addCloseableInstance(client);
return new HttpPostEmitter(config.get(), client, jsonMapper);
return new HttpPostEmitter(
config.get(),
lifecycle.addCloseableInstance(
createAsyncHttpClient("HttpPostEmitter-AsyncHttpClient-%d", sslContext)
),
jsonMapper
);
}
}

View File

@ -31,11 +31,6 @@ import com.metamx.emitter.core.ParametrizedUriEmitterConfig;
import io.druid.guice.JsonConfigProvider;
import io.druid.guice.ManageLifecycle;
import io.druid.java.util.common.lifecycle.Lifecycle;
import io.netty.handler.ssl.ClientAuth;
import io.netty.handler.ssl.JdkSslContext;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.DefaultAsyncHttpClient;
import org.asynchttpclient.DefaultAsyncHttpClientConfig;
import javax.annotation.Nullable;
import javax.net.ssl.SSLContext;
@ -59,13 +54,12 @@ public class ParametrizedUriEmitterModule implements Module
ObjectMapper jsonMapper
)
{
final DefaultAsyncHttpClientConfig.Builder builder = new DefaultAsyncHttpClientConfig.Builder();
if (sslContext != null) {
builder.setSslContext(new JdkSslContext(sslContext, true, ClientAuth.NONE));
}
final AsyncHttpClient client = new DefaultAsyncHttpClient(builder.build());
lifecycle.addCloseableInstance(client);
return new ParametrizedUriEmitter(config.get(), client, jsonMapper);
return new ParametrizedUriEmitter(
config.get(),
lifecycle.addCloseableInstance(
HttpEmitterModule.createAsyncHttpClient("ParmetrizedUriEmitter-AsyncHttpClient-%d", sslContext)
),
jsonMapper
);
}
}