jnan77 / jsonrpc4j

Automatically exported from code.google.com/p/jsonrpc4j
0 stars 0 forks source link

Add asynchronous HTTP client #43

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Our project had a need for an asynchronous HTTP client, so I've created one.

The new class is called JsonRpcHttpAsyncClient, and it's use has a dependency 
on Apache HttpComponent's HttpCore NIO library (which itself requires Apache 
Commons Codec).  Ordinary user's of jsonrpc4j who do not use 
JsonRpcHttpAsyncClient do not need these libraries.

The JsonRpcHttpAsyncClient provides for two different invocation patterns: 
Future-based and Callback-based.  It offers several access patterns for each.

Here is an example use of the Future pattern:
-----------------------------------------------------------
JsonRpcHttpAsyncClient client = new JsonRpcHttpAsyncClient(
   new URL("https://localhost/rest?j_username=admin&j_password=password"));

Future<Foo> fooFuture = client.invoke(
   "getFoo", new String[] { "param1", "param2" },
   Foo.class);

Future<Bar> fooFuture = client.invoke(
   "getBar", new String[] { "param1" },
   Bar.class);

Foo foo = fooFuture.get();
Bar bar = barFuture.get();
-----------------------------------------------------------

Here is an example of the callback pattern:
-----------------------------------------------------------
JsonRpcHttpAsyncClient client = new JsonRpcHttpAsyncClient(
   new URL("https://localhost/rest?j_username=admin&j_password=password"));

client.invoke("getFoo", new String[] { "param1", "param2" },
   Foo.class, new JsonRpcCallback<Foo>() {
       public void onComplete(Foo foo) {
           System.out.println("Got a foo!");
       }

       public void onError(Throwable t) {
           t.printStackTrace();
       }
});
-----------------------------------------------------------

The JsonRpcHttpAsyncClient supports both HTTP and HTTPS and takes full 
advantage of the Apache HttpComponent library's connection pooling abilities 
(using HTTP/1.1 keep-alive).  And because the library is NIO-based, there are 
(by default) only 2 reactor threads needed to handle hundreds of outstanding 
requests.

The two new classes are attached, as well as the pom.xml containing the 
(compile-time) dependencies for Apache HTTPComponent.

Original issue reported on code.google.com by brett.wo...@gmail.com on 22 Nov 2012 at 6:29

Attachments:

GoogleCodeExporter commented 8 years ago
Thanks, i'll take a look and consider it for a future releae

Original comment by brian.di...@gmail.com on 28 Nov 2012 at 7:52

GoogleCodeExporter commented 8 years ago
included in next release

Original comment by brian.di...@gmail.com on 28 Nov 2012 at 8:34

GoogleCodeExporter commented 8 years ago
Awesome!  Thanks.

Original comment by brett.wo...@gmail.com on 29 Nov 2012 at 4:21