Skip to content
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@

<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.10.6.Final</version>
<artifactId>netty-all</artifactId>
<version>4.1.12.Final</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@
import com.google.common.util.concurrent.ListenableFuture;
import com.metamx.http.client.auth.Credentials;
import com.metamx.http.client.response.HttpResponseHandler;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.joda.time.Duration;

import java.net.URL;

/**
*/
public class CredentialedHttpClient extends AbstractHttpClient
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/metamx/http/client/HttpClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public String getEncodingString()
*
* @return encoding name
*/
public abstract String getEncodingString();
public abstract String getEncodingString(); /*TODO use for Content-Encoding*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please file a Github/Jira issue about that

}

public static final CompressionCodec DEFAULT_COMPRESSION_CODEC = CompressionCodec.GZIP;
Expand Down
111 changes: 21 additions & 90 deletions src/main/java/com/metamx/http/client/HttpClientInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,24 @@
import com.metamx.http.client.pool.ChannelResourceFactory;
import com.metamx.http.client.pool.ResourcePool;
import com.metamx.http.client.pool.ResourcePoolConfig;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.socket.nio.NioClientBossPool;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioWorkerPool;
import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.logging.Log4JLoggerFactory;
import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.ThreadNameDeterminer;
import org.jboss.netty.util.Timer;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.internal.logging.InternalLoggerFactory;
import io.netty.util.internal.logging.Log4JLoggerFactory;
import org.joda.time.Duration;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
*/
Expand All @@ -55,47 +50,18 @@ public class HttpClientInit
public static HttpClient createClient(HttpClientConfig config, Lifecycle lifecycle)
{
try {
// We need to use the full constructor in order to set a ThreadNameDeterminer. The other parameters are taken
// from the defaults in HashedWheelTimer's other constructors.
final HashedWheelTimer timer = new HashedWheelTimer(
new ThreadFactoryBuilder().setDaemon(true)
.setNameFormat("HttpClient-Timer-%s")
.build(),
ThreadNameDeterminer.CURRENT,
100,
TimeUnit.MILLISECONDS,
512
);
lifecycle.addMaybeStartHandler(
new Lifecycle.Handler()
{
@Override
public void start() throws Exception
{
timer.start();
}

@Override
public void stop()
{
timer.stop();
}
}
);
return lifecycle.addMaybeStartManagedInstance(
new NettyHttpClient(
new ResourcePool<>(
new ChannelResourceFactory(
createBootstrap(lifecycle, timer, config.getBossPoolSize(), config.getWorkerPoolSize()),
createBootstrap(lifecycle, config.getBossPoolSize(), config.getWorkerPoolSize()),
config.getSslContext(),
timer,
config.getSslHandshakeTimeout() == null ? -1 : config.getSslHandshakeTimeout().getMillis()
),
new ResourcePoolConfig(config.getNumConnections())
),
config.getReadTimeout(),
config.getCompressionCodec(),
timer
config.getCompressionCodec()
)
);
}
Expand All @@ -114,17 +80,10 @@ public static HttpClient createClient(ResourcePoolConfig config, final SSLContex
}

@Deprecated // use createClient directly
public static ClientBootstrap createBootstrap(Lifecycle lifecycle, Timer timer)
public static Bootstrap createBootstrap(Lifecycle lifecycle)
{
final HttpClientConfig defaultConfig = HttpClientConfig.builder().build();
return createBootstrap(lifecycle, timer, defaultConfig.getBossPoolSize(), defaultConfig.getWorkerPoolSize());
}

@Deprecated // use createClient directly
public static ClientBootstrap createBootstrap(Lifecycle lifecycle)
{
final Timer timer = new HashedWheelTimer(new ThreadFactoryBuilder().setDaemon(true).build());
return createBootstrap(lifecycle, timer);
return createBootstrap(lifecycle, defaultConfig.getBossPoolSize(), defaultConfig.getWorkerPoolSize());
}

public static SSLContext sslContextWithTrustedKeyStore(final String keyStorePath, final String keyStorePassword)
Expand All @@ -144,60 +103,33 @@ public static SSLContext sslContextWithTrustedKeyStore(final String keyStorePath

return sslContext;
}
catch (CertificateException e) {
throw Throwables.propagate(e);
}
catch (NoSuchAlgorithmException e) {
throw Throwables.propagate(e);
}
catch (KeyStoreException e) {
throw Throwables.propagate(e);
}
catch (KeyManagementException e) {
throw Throwables.propagate(e);
}
catch (FileNotFoundException e) {
throw Throwables.propagate(e);
}
catch (IOException e) {
catch (CertificateException | NoSuchAlgorithmException | KeyStoreException | KeyManagementException | IOException e) {
throw Throwables.propagate(e);
}
finally {
CloseQuietly.close(in);
}
}

private static ClientBootstrap createBootstrap(Lifecycle lifecycle, Timer timer, int bossPoolSize, int workerPoolSize)
private static Bootstrap createBootstrap(Lifecycle lifecycle, int bossPoolSize, int workerPoolSize)
{
final NioClientBossPool bossPool = new NioClientBossPool(
Executors.newCachedThreadPool(
new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("HttpClient-Netty-Boss-%s")
.build()
),
final NioEventLoopGroup group = new NioEventLoopGroup(
bossPoolSize,
timer,
ThreadNameDeterminer.CURRENT
);

final NioWorkerPool workerPool = new NioWorkerPool(
Executors.newCachedThreadPool(
new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("HttpClient-Netty-Worker-%s")
.setNameFormat("HttpClient-Netty-Client-%s")
.build()
),
workerPoolSize,
ThreadNameDeterminer.CURRENT
)
);

final ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(bossPool, workerPool));

bootstrap.setOption("keepAlive", true);
bootstrap.setPipelineFactory(new HttpClientPipelineFactory());
final Bootstrap bootstrap = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.handler(new HttpClientPipelineFactory());

InternalLoggerFactory.setDefaultFactory(new Log4JLoggerFactory());
InternalLoggerFactory.setDefaultFactory(Log4JLoggerFactory.INSTANCE);

try {
lifecycle.addMaybeStartHandler(
Expand All @@ -211,7 +143,6 @@ public void start() throws Exception
@Override
public void stop()
{
bootstrap.releaseExternalResources();
}
}
);
Expand Down
Loading