Write a simple HeartBeat program by Vertx

351 views
Skip to first unread message

ziqi Zhong

unread,
Jul 16, 2015, 10:15:34 AM7/16/15
to ve...@googlegroups.com
the code is below

public class Client {

 
Vertx vertx;

 
public Client(Vertx vx) {
    vertx
= vx;
 
}

 
public void sendHeartPackage(String host, int port, long timeout) {

   
CompletableFuture<HttpClientResponse> cf = new CompletableFuture<HttpClientResponse>();

   
TimeoutStream ts = vertx.periodicStream(2000);

    ts
.handler(handler -> {

      vertx
.createHttpClient().getNow(port, host, "is_alive?", response -> {
        cf
.complete(response);
     
});

      vertx
.setTimer(timeout, result -> {
       
if (cf.isDone() == false) {
          ts
.cancel();
         
System.out.println("connect fail!");
       
}
       
System.out.println("connect OK!");
     
});

   
});

 
}
}

the server code is easy,just response something
I use CompletableFuture

Do anyone else have more nice vertx program about the heartbeat program

bytor99999

unread,
Jul 16, 2015, 12:49:36 PM7/16/15
to ve...@googlegroups.com
Yep, there are a WHOLE bunch of examples like this at


Mark

jordan.h...@gmail.com

unread,
Jul 16, 2015, 10:55:27 PM7/16/15
to ve...@googlegroups.com
BTW I know I preach this a lot but here goes again:
On the use of CompletableFuture, I'm a big fan of it, but it doesn't always fit well with Vert.x's threading model. Vert.x is very carefully designed to ensure user code doesn't have to be thread safe, but using CompletableFuture can break that promise if not used correctly. Specifically, its *Async callbacks are executed in a thread pool that's separate from Vert.x. To ensure you do not break the Vert.x threading model, you should use Vert.x's AsyncResult and Future or its RxJava API until a community driven API that uses CompletableFuture is released.
--
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.
Visit this group at http://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/6690b51b-2a06-4234-9408-dd4d9c50f7fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ziqi Zhong

unread,
Jul 18, 2015, 8:11:10 AM7/18/15
to ve...@googlegroups.com
Very thanks to you advise !!!
I will do more research about this

thanks again !!!

ziqi Zhong

unread,
Jul 19, 2015, 1:41:14 AM7/19/15
to ve...@googlegroups.com
  public void sendHeartPackage(String host, int port, long timeout) {


    TimeoutStream ts = vertx.periodicStream(2000);

    ts.handler(handler -> {

      Future<HttpClientResponse> future = Future.future();
      
      vertx.createHttpClient().getNow(port, host, "is_alive?", res -> {
        future.complete(res);
      });

      vertx.setTimer(timeout, result -> {
        if (future.isComplete() == false) {
          ts.cancel();
          System.out.println("connect fail !");
        } else {
          System.out.println("connect success !");
        }
      });

    });

  }

I have rewrite it use the Vert.x Future
It works well

Ronald van Raaphorst

unread,
Jul 19, 2015, 5:53:13 AM7/19/15
to ve...@googlegroups.com
Don't want to hijack this topic, but I didn't see this tip before. I've been using the CompletableFuture on several occasions. If it's so important, this should be described in the docs somewhere, with a good and a bad example...

ziqi Zhong

unread,
Jul 19, 2015, 10:01:03 PM7/19/15
to ve...@googlegroups.com
Agree with
Ronald van Raaphorst

it better to have some example !

Paulo Lopes

unread,
Jul 20, 2015, 3:05:43 AM7/20/15
to ve...@googlegroups.com
If you can define some good examples, then you can also submit some pull requests for inclusion on the examples repo.

Best regards,
Paulo
Reply all
Reply to author
Forward
0 new messages