Call HttpClientRequest in synchronous way

2,122 views
Skip to first unread message

ROHIT KOUSHAL

unread,
Jun 19, 2015, 2:04:40 PM6/19/15
to ve...@googlegroups.com, ROHIT KOUSHAL
Hello all,

I am new to the vert.x and currently working on assignment in which developing vert.x application. I wrote one Utility class for making hit to authenticate server which provide authentication token. When I call login method by creating it object the method returns null to me but after few seconds response is getting printed on console. So here I think HttpClientRequest is been called asynchronously. 

Below is the code snippet for the Utility class :
public class AuthVertxAPI extends AbstractVerticle {
    String token;
    public String login(String username, String password, Vertx vertx) {
        HttpClientOptions clientOptions = new HttpClientOptions();
        HttpClient httpClient = vertx.createHttpClient(clientOptions);
        final HttpClientRequest req = httpClient.request(HttpMethod.POST, 8080, "localhost", "/some-url", response -> {
                   System.out.println("Got response: " + response.statusCode());
                   response.bodyHandler(body -> {
                       token = body.toString("ISO-8859-1");
                       System.out.println("Got data " + body.toString("ISO-8859-1"));
                   });
            });
        String jsonMessage = "{\"username\": \"admin\", \"password\": \"ofbiz\"}";
        req.putHeader("Content-Type", "application/json")
                .putHeader("Content-Length", "" + jsonMessage.length())
                .write(jsonMessage)
                .end();
      return token;
    }
}


So please help me here, I need call this method in synchronous way(If it is not the right way of doing things then please guide me for the right approach)

Thanks in advance !!!!!!

Arnaud Estève

unread,
Jun 19, 2015, 9:37:27 PM6/19/15
to ve...@googlegroups.com
This is not the right way, indeed.

You're calling an asynchronous method (request.end()) and then return immediately, obviously the token is null at this point.


What Vert.x does, for dealing with asynchronous stuff, is to provide a Handler<AsyncResult<?>>, this handler will be called once and only once the async treatment is finished.
You should do something similar, and in your case the "handler" will be responsible for setting the value of the token.

ROHIT KOUSHAL

unread,
Jun 19, 2015, 10:54:59 PM6/19/15
to ve...@googlegroups.com
Hi Arnaud,

Thanks for your reply. But I don't want to execute the above logic asynchronously. So can you please guide me how I achieve the same in vert.x in synchronous way.

Any example will we a great help for me.

Thanks again !!!!!!

Ronald van Raaphorst

unread,
Jun 20, 2015, 2:44:04 AM6/20/15
to ve...@googlegroups.com
Check vertx.executeBlocking, see http://vert-x3.github.io/docs/vertx-core/java/#blocking_code
Another option is to run this verticle as a worker,see http://vert-x3.github.io/docs/vertx-core/java/#_verticle_types

Tim Fox

unread,
Jun 20, 2015, 3:35:24 AM6/20/15
to ve...@googlegroups.com
On 20/06/15 03:54, ROHIT KOUSHAL wrote:
Hi Arnaud,

Thanks for your reply. But I don't want to execute the above logic asynchronously.


Why?

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ronald van Raaphorst

unread,
Jun 20, 2015, 3:49:28 AM6/20/15
to ve...@googlegroups.com
I suppose you don't want to execute subsequent business logic code while the client is still waiting for the response. But running asynchronous doesn't mean that subsequent code can't be 'on hold' while your authentication call is waiting for an answer. 
Running asynchronous means that the thread your client http call is running on can be assigned to other stuff for *other* users/calls and is not waiting for the response of the httpclientrequest call while doing nothing. When the answer is received, the thread will be reassigned to this specific call and will continue with whatever you code next.

ROHIT KOUSHAL

unread,
Jun 23, 2015, 4:59:39 AM6/23/15
to ve...@googlegroups.com
Thank you for your help Ronald van Raaphorst. I will look into the shared details..


Tim : I am writing one utilty class which will hit the authentication server and provide us a token for a valid user. So that I can use that token after immediately calling of utility methods.

Tim Fox

unread,
Jun 23, 2015, 5:16:02 AM6/23/15
to ve...@googlegroups.com
On 23/06/15 09:59, ROHIT KOUSHAL wrote:
Thank you for your help Ronald van Raaphorst. I will look into the shared details..


Tim : I am writing one utilty class which will hit the authentication server and provide us a token for a valid user. So that I can use that token after immediately calling of utility methods.

Yes, but why does it need to be synchronous? Blocking harms scalability.



On Saturday, June 20, 2015 at 1:19:28 PM UTC+5:30, Ronald van Raaphorst wrote:
I suppose you don't want to execute subsequent business logic code while the client is still waiting for the response. But running asynchronous doesn't mean that subsequent code can't be 'on hold' while your authentication call is waiting for an answer. 
Running asynchronous means that the thread your client http call is running on can be assigned to other stuff for *other* users/calls and is not waiting for the response of the httpclientrequest call while doing nothing. When the answer is received, the thread will be reassigned to this specific call and will continue with whatever you code next.

Reply all
Reply to author
Forward
0 new messages