Skip to content

Configuring ZombieLink

Lahiru Sahan Jayasinghe edited this page Jan 5, 2014 · 2 revisions

Every endpoint uses an existing configuration for request execution. Such a configuration usually pertains to the settings applied on Apache HC. Familiarization with Apache HC should be a prerequisite for building a custom configuration.


The default configuration employs a [DefaultHttpClient](http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/DefaultHttpClient.html) which uses a [PoolingClientConnectionManager](http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/PoolingClientConnectionManager.html) with the following parameters.
Parameter Configuration
Max. Connections 200
Default Max. Connections Per Route 20

Additionally, two schemes are registered.
Scheme Port Socket Factory
HTTP 80 PlainSocketFactory
HTTPS 443 SSLSocketFactory

A configuration can be tailored for a specific endpoint by creating an implementation of `Zombie.Configuration`. Currently the only configurable property is the `HttpClient`. Once a configuration is created, it can be applied on an endpoint using the `@Config` annotation by specifying the `Class` of the implementation.
public class GitHubConfig extends Zombie.Configuration {

    @Override
    public HttpClient httpClient() {
        
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
                    
        PoolingClientConnectionManager pccm = new PoolingClientConnectionManager(schemeRegistry);
        pccm.setMaxTotal(100);
        
        HttpHost gitHubHost = new HttpHost("https://api.github.com", 443);
        pccm.setMaxPerRoute(new HttpRoute(gitHubHost), 50);
                    
        return new DefaultHttpClient(pccm);
    }
}

@Config(GitHubConfig.class)
@Endpoint("https://api.github.com")
public interface GitHubEndpoint {

    ...
}

You can tweak the preconfigured DefaultHttpClient as well.

public class TweakedConfig extends Zombie.Configuration {

    @Override
    public HttpClient httpClient() {
        
        HttpClient httpClient = super.httpClient();
        PoolingClientConnectionManager pccm = (PoolingClientConnectionManager) httpClient.getConnectionManager();
        
        HttpHost gitHubHost = new HttpHost("https://api.github.com", 443);
        pccm.setMaxPerRoute(new HttpRoute(gitHubHost), 50);

        return httpClient;
    }
}

Clone this wiki locally