DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Fluency and Control with HttpClient

Akber Choudhry user avatar by
Akber Choudhry
·
Dec. 11, 12 · Interview
Like (1)
Save
Tweet
Share
9.71K Views

Join the DZone community and get the full member experience.

Join For Free

The new HttpClient provides a fluent interface that is quite intuitive.  However, any non-trivial implementation soon starts to miss the flexibility of configuring the connection manager.  As a pre-configured HttpClient can be passed into the fluent Executor each time, it is overhead to configure it each time. 

Here is a sample of a simple fluent Request from the tutorial:

String page = Request.Get("http://somehost/")
     .connectTimeout(1000)
     .socketTimeout(1000)
     .execute().returnContent().asString();

To maintain client-side state (cookies, authentication) between requests, the fluent Executor helps by keeping a cookie store and setting up other types of authentication:

Executor executor = Executor.newInstance()
    .authPreemptive(new HttpHost("myproxy", 8080));
executor.execute(Request.Post("http://somehost/some-form))
    .addHeader("X-Custom-header", "stuff")
    .execute().saveContent(new File("result.dump"));
executor.execute(Request.Get("http://somehost/"))
    .returnContent().asString();

Executor provides a HttpClient constructor but should we be setting one up each time we want to use an Executor 'conversation'.  Extending it in the singleton pattern provided an instance that was configured just once:

Executor executor = Executor.newInstance(OurHttpClient.getInstance());
page = executor.execute(Request . . . . . 
doc = Jsoup.parse(page);
doLogout(excutor);  //passing around the Executor to different methods
executor.clearCookies(); // in case it needs to be cleared

And relevant snippets from the singleton OurHttpClient

private static OurHttpClient instance;
private static PoolingClientConnectionManager cm;

private OurHttpClient(ClientConnectionManager connMgr) { // singleton
    super(connMgr)
}

private static void initialize() { // set up SSL and regular ports with any quirks
    SchemeRegistry . . . . 
    cm = new PoolingClientConnectionManager(schemeRegistry);
    cm.setDefaultMaxPerRoute(MAX_CONNS_ROUTE);
    cm.setMaxTotal(MAX_CONNS_TOTAL);
}


// The public getInstance method
public static OurHttpClient getInstance() {
    if (instance == null) {
        initialize(); // will create the Connection Manager
    }
    instance = new OurHttpClient(cm);  
     // an example of some other customization 
    instance.setRedirectStrategy(new LaxRedirectStrategy());
     // an example of other features being set up
    if (PROXY_HOST != null !"".equals(PROXY_HOST)) {

    }
    return instance;
}

Executor (software) Singleton pattern Fluent interface Requests authentication Snippet (programming) Overhead (computing) Connection (dance) Interface (computing) Implementation

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Microservices Discovery With Eureka
  • 7 Awesome Libraries for Java Unit and Integration Testing
  • What Is a Kubernetes CI/CD Pipeline?
  • Bye Bye, Regular Dev [Comic]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: