Giter Site home page Giter Site logo

http-client's Introduction

HTTP client

This project is a Java high performance and throughput oriented HTTP client library, with support for HTTP 1.1 pipelining. It was developed mostly towards server-side usage, where speed and low resource usage are the key factors, but can be used to build client applications as well.

Built on top of Netty and designed for high concurrency scenarios where multiple threads can use the same instance of a client without any worries for external or internal synchronization, it helps you reduce initialization and/or preparation times and resource squandering. Among many small optimizations, connections are reused whenever possible, which results in a severe reduction of total request execution times by cutting connection establishment overhead.

Version 1.1 is nearly ready

Be sure to check the change log. The user-facing API has remained pretty much the same apart from some class renaming โ€” the transition should be smooth.

Dependencies

Usage examples

Synchronous mode

This example contains all the steps to execute a request, from creation to cleanup. This is the synchronous mode, which means that the calling thread will block until the request completes.

// Create & initialise the client
HttpClient client = new DefaultHttpClient();
client.init();

// Setup the request
HttpRequest request = new DefaultHttpRequest(HTTP_1_0, GET, "/");

// Execute the request, turning the result into a String
RequestFuture<String> future = client.execute("biasedbit.com", 80, request, new BodyAsStringProcessor());
future.awaitUninterruptibly();
// Print some details about the request
System.out.println(future);
    
// If response was >= 200 and <= 299, print the body (a String)
if (future.hasSuccessfulResponse()) System.out.println(future.getProcessedResult());

// Cleanup
client.terminate();

Asynchronous mode (recommended)

In asynchronous mode, an event listener is attached to the object returned by the http client when a request execution is submitted. Attaching this listener allows the programmer to define some computation to occur when the request finishes.

// Execute an asynchronous request, JDK 8 syntax
RequestFuture<String> future = client.execute("biasedbit.com", 80, request, new BodyAsStringProcessor());
future.addListener((future) -> {
        System.out.println(future);
        if (future.hasSuccessfulResponse()) System.out.println(future.getProcessedResult());
        client.terminate();
    }
});

// Execute an asynchronous request
RequestFuture<String> future = client.execute("biasedbit.com", 80, request, new BodyAsStringProcessor());
future.addListener(new RequestFutureListener<String>() {
    @Override public void operationComplete(RequestFuture<String> future)
            throws Exception {
        System.out.println(future);
        if (future.hasSuccessfulResponse()) System.out.println(future.getProcessedResult());
        client.terminate();
    }
});

NOTE: you should never perform non-CPU bound operations in the listeners.

Integration with IoC containers

IoC compliance was paramount when developing this library.

Here's a simple example of how to configure a client in Spring:

<bean id="httpClient" class="com.biasedbit.http.client.DefaultHttpClient"
      init-method="init" destroy-method="terminate">
  <property ... />
</bean>

Or using a client factory, in case you want multiple clients:

<bean id="httpClientFactory" class="com.biasedbit.http.client.factory.DefaultHttpClientFactory">
  <property ... />
</bean>

HttpClientFactory instances will configure each client produced exactly how they were configured - they have the same options as (or more than) the HttpClients they generate. Instead of having some sort of configuration object, you configure the factory and then call createClient() in it to obtain a pre-configured client.

You can also create a client for a component, based on a predefined factory:

<bean id="someBean" class="com.biasedbit.SomeComponent">
  <property name="httpClient">
    <bean factory-bean="httpClientFactory" factory-method="createClient" />
  </property>
  <property ... />
</bean>

Note that you can accomplish the same effect as the factory example by simply using an abstract definition of a HttpClient bean and then using Spring inheritance.

Test coverage status

This project has a battery of unit and integration tests created with Spock Framework.

The current coverage, as reported by IntelliJ IDEA is around 92%. Keep in mind this project uses Lombok, so there is some auto-generated code (getters & setters, constructors) that is not tested.

This project is also configured with pitest through gradle-pitest-plugin. The coverage reported by pitest is 81%, with 55% mutation coverage.

To run the mutation test target and check the report, run the following:

$ gradle pitest
$ open build/reports/pitest/<most recent date>/index.html

The difference in coverage reported by IDEA and pitest's reports is due to the use of Lombok (IDEA reports coverage on original source code and pitest reports coverage on de-lombok'd (expanded) code.

License

This project is licensed under the Apache License version 2.0 as published by the Apache Software Foundation.

http-client's People

Contributors

biasedbit avatar marcodalco avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.