Grizzly Async HTTP Client

The Grizzly Async Http Client library purpose is to allow Java applications to easily execute HTTP requests and asynchronously process the HTTP responses.

License

License

Categories

Categories

GlassFish Container Application Servers CLI User Interface Grizzly Net Networking
GroupId

GroupId

org.glassfish.grizzly
ArtifactId

ArtifactId

grizzly-http-client
Last Version

Last Version

1.16
Release Date

Release Date

Type

Type

jar
Description

Description

Grizzly Async HTTP Client
The Grizzly Async Http Client library purpose is to allow Java applications to easily execute HTTP requests and asynchronously process the HTTP responses.
Project URL

Project URL

https://github.com/eclipse-ee4j/grizzly-ahc
Project Organization

Project Organization

Eclipse Foundation
Source Code Management

Source Code Management

https://github.com/eclipse-ee4j/grizzly-ahc

Download grizzly-http-client

How to add to project

<!-- https://jarcasting.com/artifacts/org.glassfish.grizzly/grizzly-http-client/ -->
<dependency>
    <groupId>org.glassfish.grizzly</groupId>
    <artifactId>grizzly-http-client</artifactId>
    <version>1.16</version>
</dependency>
// https://jarcasting.com/artifacts/org.glassfish.grizzly/grizzly-http-client/
implementation 'org.glassfish.grizzly:grizzly-http-client:1.16'
// https://jarcasting.com/artifacts/org.glassfish.grizzly/grizzly-http-client/
implementation ("org.glassfish.grizzly:grizzly-http-client:1.16")
'org.glassfish.grizzly:grizzly-http-client:jar:1.16'
<dependency org="org.glassfish.grizzly" name="grizzly-http-client" rev="1.16">
  <artifact name="grizzly-http-client" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.glassfish.grizzly', module='grizzly-http-client', version='1.16')
)
libraryDependencies += "org.glassfish.grizzly" % "grizzly-http-client" % "1.16"
[org.glassfish.grizzly/grizzly-http-client "1.16"]

Dependencies

compile (4)

Group / Artifact Type Version
org.glassfish.grizzly : connection-pool jar 2.4.3
org.glassfish.grizzly : grizzly-websockets jar 2.4.3
org.slf4j : slf4j-api jar 1.7.12
com.google.guava : guava Optional jar 11.0.2

test (10)

Group / Artifact Type Version
org.glassfish.grizzly : grizzly-http-server jar 2.4.3
ch.qos.logback : logback-classic jar 1.2.3
log4j : log4j jar 1.2.17
org.testng : testng jar 6.8.8
org.mockito : mockito-all jar 1.10.19
org.eclipse.jetty : jetty-server jar 9.4.9.v20180320
org.eclipse.jetty : jetty-proxy jar 9.4.9.v20180320
org.eclipse.jetty.websocket : websocket-server jar 9.4.9.v20180320
commons-io : commons-io jar 2.0.1
commons-fileupload : commons-fileupload jar 1.2.2

Project Modules

There are no modules declared in this project.

Async Http Client

The Grizzly Async Http Client (GAHC) library purpose is to allow Java applications to easily execute HTTP requests and asynchronously process the HTTP responses. The library also supports the WebSocket Protocol. The Async HTTP Client library is simple to use. First, in order to add it to your Maven project, simply add this dependency:

         <dependency>
             <groupId>org.glassfish.grizzly</groupId>
             <artifactId>grizzly-http-client</artifactId>
             <version>1.15</version>
         </dependency>

You can also download the artifact

Maven Search

Then in your code you can simply do (Javadoc)

    import com.ning.http.client.*;
    import java.util.concurrent.Future;

    AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
    Future<Response> f = asyncHttpClient.prepareGet("http://www.ning.com/ ").execute();
    Response r = f.get();

You can also accomplish asynchronous operation without using a Future if you want to receive and process the response in your handler:

    import com.ning.http.client.*;
    import java.util.concurrent.Future;

    AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
    asyncHttpClient.prepareGet("http://www.ning.com/ ").execute(new AsyncCompletionHandler<Response>(){
        
        @Override
        public Response onCompleted(Response response) throws Exception{
            // Do something with the Response
            // ...
            return response;
        }
        
        @Override
        public void onThrowable(Throwable t){
            // Something wrong happened.
        }
    });

You can also mix Future with AsyncHandler to only retrieve part of the asynchronous response

    import com.ning.http.client.*;
    import java.util.concurrent.Future;

    AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
    Future<Integer> f = asyncHttpClient.prepareGet("http://www.ning.com/ ").execute(
       new AsyncCompletionHandler<Integer>(){
        
        @Override
        public Integer onCompleted(Response response) throws Exception{
            // Do something with the Response
            return response.getStatusCode();
        }
        
        @Override
        public void onThrowable(Throwable t){
            // Something wrong happened.
        }
    });
    
    int statuѕCode = f.get();

You have full control on the Response life cycle, so you can decide at any moment to stop processing what the server is sending back:

      import com.ning.http.client.*;
      import java.util.concurrent.Future;

      AsyncHttpClient c = new AsyncHttpClient();
      Future<String> f = c.prepareGet("http://www.ning.com/ ").execute(new AsyncHandler<String>() {
          private StringBuilder builder = new StringBuilder();

          @Override
          public STATE onStatusReceived(HttpResponseStatus status) throws Exception {
              int statusCode = status.getStatusCode();
               // The Status have been read
               // If you don't want to read the headers,body or stop processing the response
               return STATE.ABORT;
          }

          @Override
          public STATE onHeadersReceived(HttpResponseHeaders h) throws Exception {
              Headers headers = h.getHeaders();
               // The headers have been read
               // If you don't want to read the body, or stop processing the response
               return STATE.ABORT;
          }

          @Override
          public STATE onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
               builder.append(new String(bodyPart.getBodyPartBytes()));
               return STATE.CONTINUE
          }

          @Override
          public String onCompleted() throws Exception {
               // Will be invoked once the response has been fully read or a ResponseComplete exception
               // has been thrown.
               return builder.toString();
          }

          @Override
          public void onThrowable(Throwable t) {
          }
      });
      
      String bodyResponse = f.get();

Finally, you can also configure the AsyncHttpClient via it's AsyncHttpClientConfig object:

        AsyncHttpClientConfig cf = new AsyncHttpClientConfig.Builder()
            S.setProxyServer(new ProxyServer("127.0.0.1", 38080)).build();
        AsyncHttpClient c = new AsyncHttpClient(cf);

Async Http Client also support WebSocket by simply doing:

         WebSocket websocket = c.prepareGet(getTargetUrl())
                .execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(
                    new WebSocketTextListener() {

                    @Override
                    public void onMessage(String message) {
                    }

                    @Override
                    public void onOpen(WebSocket websocket) {
                        websocket.sendTextMessage("...").sendBinaryMessage("...");
                    }

                    @Override
                    public void onClose(.WebSocket websocket) {
                        latch.countDown();
                    }

                    @Override
                    public void onError(Throwable t) {
                    }
                }).build()).get();

Keep up to date on the library development by joining the Grizzly discussion group

Grizzly Discussion Group.

This code has been forked from the original AHC 1.9.x branch.

org.glassfish.grizzly

Eclipse EE4J

The Eclipse EE4J Project

Versions

Version
1.16
1.15
1.14
1.13
1.12
1.11
1.10
1.9
1.8
1.7
1.6
1.5
1.4
1.3
1.2
1.1
1.0