hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mohan.radhakrish...@polarisFT.com
Subject Async API call
Date Wed, 09 Oct 2013 06:53:46 GMT
Hi,
       I am using this JUnit test( example from Apache HTTP Components as 
it is ). I am sending JSON to a Netty server which is able to receive 2 
POSTS
and also send responses. I believe Netty can receive more requests but 
that might be a different issue.

Here even for the 2 POSTS that Netty receives and sends responses the 
callback methods are not called. I was thinking this unit test will 
complete and the JVM will exit. But it doesn't happen.

When does 'future.get' return ? Does it return as soon as it sends and 
receives a response ?

Since I am a new user I could be making some minor mistakes  but the code 
is able to send and the server receives the JSON.

Thanks,
Mohan

        @Test
    public void testAsyncThread() throws InterruptedException {
        // Use pool of two threads
        ExecutorService threadpool = Executors.newFixedThreadPool(2);
        Async async = Async.newInstance().use(threadpool);

 
        Request[] requests = new Request[] {
                        Request.Post("http://localhost:9090/print"
).bodyString(getJSon(), ContentType.APPLICATION_JSON),
                        Request.Post("http://localhost:9090/print"
).bodyString(getJSon(), ContentType.APPLICATION_JSON),
        };


        Queue<Future<Content>> queue = new LinkedList<Future<Content>>();
        // Execute requests asynchronously
        for (final Request request: requests) {

                Future<Content> future = async.execute(request, new 
FutureCallback<Content>() {

                public void failed(final Exception ex) {
                        logger.info(ex.getMessage() + ": " + request);
                }

                public void completed(final Content content) {
                        logger.info("Request completed: " + request);
                }

                public void cancelled() {
                        logger.info("Request cancelled: " + request);
                }

            });
            queue.add(future);
        }

        while(!queue.isEmpty()) {
            Future<Content> future = queue.remove();
            try {
 
                Content c = future.get();
                logger.info( "Response " + c.asString() );
            } catch (ExecutionException ex) {
 
                        logger.info( "ExecutionException [" + 
getExceptionAsString( ex ) + "]" );
            }
        }
        logger.info("Done");
        threadpool.shutdown();
    }


This e-Mail may contain proprietary and confidential information and is sent for the intended
recipient(s) only.  If by an addressing or transmission error this mail has been misdirected
to you, you are requested to delete this mail immediately. You are also hereby notified that
any use, any form of reproduction, dissemination, copying, disclosure, modification, distribution
and/or publication of this e-mail message, contents or its attachment other than by its intended
recipient/s is strictly prohibited.

Visit us at http://www.polarisFT.com

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message