Erik
Erik

Reputation: 2051

Vertx.io GET silently fails


I'm writing a POC using vertx, looking for alternatives when we have to migrate Spring Web from 4.x to 5 to be java 9 compliant. I've written a simple client, just a GET towards a publicly available server just to get something working but it silently fails me.

    public List<String> pull() {

      Vertx vertx = Vertx.vertx();

      HttpClientOptions options = new HttpClientOptions().setLogActivity(true);
      HttpClient hc = vertx.createHttpClient(options);
      hc.getNow(80, "http://sunet.se", "/",r -> {
        System.out.println("\n****** Handler called! ***\n");
      });
      return new ArrayList<>();
  }

This will silently fail and I cannot understand why. As far as I can tell, I do exactly as in the examples given in the docs. In desperation I fired up wire shark and according to WS, there is no actual call (when I use the browser WS captures that). So, it seems my call is never actually done. I don't get any exceptions or anything. Setting the log level to debug gives nothing noteworthy other than

Failed to get SOMAXCONN from sysctl and file /proc/sys/net/core/somaxconn. Default: 128

And that should not fail the call. I've also tried using vertx.io WebClient but that fails also, in the same manner.
UPDATE
I've managed to get it to work but with a caveat.
As @tsegismont states in his answer, the protocol part of the URI shouldn't be there, that was not in the examples, I just missed it myself.
I ran my example as a stand-alone and then it worked.
My original example was run as a junit test (it's an easy way to test code and I usually try to write the test code first) and when it's run as a junit test it still doesn't work. Why that is, I have no idea. I would greatly appreciate if someone could tell me how to get that to work.

Upvotes: 1

Views: 1347

Answers (1)

tsegismont
tsegismont

Reputation: 9128

The getNow variant you use expects the server host, not a URL. It should be:

hc.getNow(80, "sunet.se", "/",r -> {
  System.out.println("\n****** Handler called! ***\n");
}

If you found a snippet like this in the Vert.x docs it's a bug. Would you mind to report it?

Now a few comments.

1/ The HttpClient is a low-level client.

Most users should prefer the Vert.x Web Client

Here's an example for your use case:

WebClient client = WebClient.create(vertx);

client
  .get(80, "sunet.se", "/")
  .send(ar -> {
    if (ar.succeeded()) {
      // Obtain response
      HttpResponse<Buffer> response = ar.result();
      System.out.println("Received response with status code" + response.statusCode());
    } else {
      System.out.println("Something went wrong " + ar.cause().getMessage());
    }
  });

2/ Create a single Vert.x and WebClient instance

Do not create a Vert.x and WebClient instance on every method call. It wastes resources and is inefficient.

Upvotes: 2

Related Questions