Skip to content

Commit

Permalink
Use VerticleBase in vertx-core examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Oct 22, 2024
1 parent c35c1cf commit 00b121c
Show file tree
Hide file tree
Showing 54 changed files with 646 additions and 550 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.vertx.example.core.eventbus.messagecodec;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.eventbus.EventBus;
import io.vertx.example.core.eventbus.messagecodec.util.CustomMessage;
import io.vertx.example.core.eventbus.messagecodec.util.CustomMessageCodec;
Expand All @@ -10,14 +11,14 @@
* Cluster receiver
* @author Junbong
*/
public class ClusterReceiver extends AbstractVerticle {
public class ClusterReceiver extends VerticleBase {
public static void main(String[] args) {
VertxApplication.main(new String[]{ClusterReceiver.class.getName(), "-cluster"});
}

@Override
public void start() throws Exception {
EventBus eventBus = getVertx().eventBus();
public Future<?> start() throws Exception {
EventBus eventBus = vertx.eventBus();

// Register codec for custom message
eventBus.registerDefaultCodec(CustomMessage.class, new CustomMessageCodec());
Expand All @@ -32,5 +33,7 @@ public void start() throws Exception {
CustomMessage replyMessage = new CustomMessage(200, "a00000002", "Message sent from cluster receiver!");
message.reply(replyMessage);
});

return super.start();
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package io.vertx.example.core.eventbus.messagecodec;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.eventbus.EventBus;
import io.vertx.example.core.eventbus.messagecodec.util.CustomMessage;

/**
* Local receiver
* @author Junbong
*/
public class LocalReceiver extends AbstractVerticle {
public class LocalReceiver extends VerticleBase {

@Override
public void start() throws Exception {
EventBus eventBus = getVertx().eventBus();
public Future<?> start() throws Exception {
EventBus eventBus = vertx.eventBus();

// Does not have to register codec because sender already registered
/*eventBus.registerDefaultCodec(CustomMessage.class, new CustomMessageCodec());*/
Expand All @@ -26,5 +28,7 @@ public void start() throws Exception {
CustomMessage replyMessage = new CustomMessage(200, "a00000002", "Message sent from local receiver!");
message.reply(replyMessage);
});

return super.start();
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package io.vertx.example.core.eventbus.messagecodec;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.eventbus.EventBus;
import io.vertx.example.core.eventbus.messagecodec.util.CustomMessage;
import io.vertx.example.core.eventbus.messagecodec.util.CustomMessageCodec;
import io.vertx.launcher.application.VertxApplication;

/**
* Publisher
*
* @author Junbong
*/
public class Sender extends AbstractVerticle {
public class Sender extends VerticleBase {
public static void main(String[] args) {
VertxApplication.main(new String[]{Sender.class.getName(), "-cluster"});
}

@Override
public void start() throws Exception {
EventBus eventBus = getVertx().eventBus();
public Future<?> start() throws Exception {
EventBus eventBus = vertx.eventBus();

// Register codec for custom message
eventBus.registerDefaultCodec(CustomMessage.class, new CustomMessageCodec());
Expand All @@ -27,10 +29,8 @@ public void start() throws Exception {
CustomMessage localMessage = new CustomMessage(200, "a0000001", "Local message!");

// Send a message to [cluster receiver] every second
getVertx().setPeriodic(1000, _id -> {
eventBus
.request("cluster-message-receiver", clusterWideMessage)
.onComplete(reply -> {
vertx.setPeriodic(1000, _id -> {
eventBus.request("cluster-message-receiver", clusterWideMessage).onComplete(reply -> {
if (reply.succeeded()) {
CustomMessage replyMessage = (CustomMessage) reply.result().body();
System.out.println("Received reply: " + replyMessage.getSummary());
Expand All @@ -42,12 +42,11 @@ public void start() throws Exception {


// Deploy local receiver
getVertx().deployVerticle(LocalReceiver.class.getName()).onComplete(deployResult -> {
// Deploy succeed
return vertx.deployVerticle(LocalReceiver.class.getName()).andThen(deployResult -> {
if (deployResult.succeeded()) {
// Send a message to [local receiver] every 2 second
getVertx().setPeriodic(2000, _id -> {
eventBus.request("local-message-receiver", localMessage).onComplete( reply -> {
vertx.setPeriodic(2000, _id -> {
eventBus.request("local-message-receiver", localMessage).onComplete(reply -> {
if (reply.succeeded()) {
CustomMessage replyMessage = (CustomMessage) reply.result().body();
System.out.println("Received local reply: " + replyMessage.getSummary());
Expand All @@ -56,10 +55,6 @@ public void start() throws Exception {
}
});
});

// Deploy failed
} else {
deployResult.cause().printStackTrace();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package io.vertx.example.core.eventbus.pointtopoint;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.eventbus.EventBus;
import io.vertx.launcher.application.VertxApplication;

/*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class Receiver extends AbstractVerticle {
public class Receiver extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{Receiver.class.getName(), "-cluster"});
}

@Override
public void start() throws Exception {
public Future<?> start() throws Exception {

EventBus eb = vertx.eventBus();

Expand All @@ -26,5 +27,6 @@ public void start() throws Exception {
});

System.out.println("Receiver ready!");
return super.start();
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package io.vertx.example.core.eventbus.pointtopoint;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.eventbus.EventBus;
import io.vertx.launcher.application.VertxApplication;

/*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class Sender extends AbstractVerticle {
public class Sender extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{Sender.class.getName(), "-cluster"});
}

@Override
public void start() throws Exception {
public Future<?> start() throws Exception {
EventBus eb = vertx.eventBus();

// Send a message every second
Expand All @@ -31,5 +32,7 @@ public void start() throws Exception {
});

});

return super.start();
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package io.vertx.example.core.eventbus.pubsub;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.eventbus.EventBus;
import io.vertx.launcher.application.VertxApplication;

/*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class Receiver extends AbstractVerticle {
public class Receiver extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{Receiver.class.getName(), "-cluster"});
}


@Override
public void start() throws Exception {
public Future<?> start() throws Exception {

EventBus eb = vertx.eventBus();

Expand All @@ -26,5 +27,7 @@ public void start() throws Exception {
eb.consumer("news-feed", message -> System.out.println("Received news on consumer 3: " + message.body()));

System.out.println("Ready!");

return super.start();
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package io.vertx.example.core.eventbus.pubsub;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.eventbus.EventBus;
import io.vertx.launcher.application.VertxApplication;

/*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class Sender extends AbstractVerticle {
public class Sender extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{Sender.class.getName(), "-cluster"});
}

@Override
public void start() throws Exception {
public Future<?> start() throws Exception {

EventBus eb = vertx.eventBus();

// Send a message every second

vertx.setPeriodic(1000, v -> eb.publish("news-feed", "Some news!"));

return super.start();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.vertx.example.core.eventbus.ssl;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.eventbus.EventBusOptions;
import io.vertx.core.net.JksOptions;
Expand All @@ -11,7 +12,7 @@
/*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class Receiver extends AbstractVerticle {
public class Receiver extends VerticleBase {

public static void main(String[] args) {
VertxApplication application = new VertxApplication(new String[]{Receiver.class.getName(), "-cluster"}, new VertxApplicationHooks() {
Expand All @@ -27,7 +28,7 @@ public void beforeStartingVertx(HookContext context) {
}

@Override
public void start() throws Exception {
public Future<?> start() throws Exception {

EventBus eb = vertx.eventBus();

Expand All @@ -39,5 +40,7 @@ public void start() throws Exception {
});

System.out.println("Receiver ready!");

return super.start();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.vertx.example.core.eventbus.ssl;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.eventbus.EventBusOptions;
import io.vertx.core.net.JksOptions;
Expand All @@ -11,7 +12,7 @@
/*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class Sender extends AbstractVerticle {
public class Sender extends VerticleBase {

public static void main(String[] args) {
VertxApplication application = new VertxApplication(new String[]{Sender.class.getName(), "-cluster"}, new VertxApplicationHooks() {
Expand All @@ -27,7 +28,7 @@ public void beforeStartingVertx(HookContext context) {
}

@Override
public void start() throws Exception {
public Future<?> start() throws Exception {
EventBus eb = vertx.eventBus();

// Send a message every second
Expand All @@ -43,5 +44,7 @@ public void start() throws Exception {
});

});

return super.start();
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package io.vertx.example.core.execblocking;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.launcher.application.VertxApplication;

/*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class ExecBlockingExample extends AbstractVerticle {
public class ExecBlockingExample extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{ExecBlockingExample.class.getName()});
}

@Override
public void start() throws Exception {
public Future<?> start() throws Exception {

vertx.createHttpServer().requestHandler(request -> {
return vertx.createHttpServer().requestHandler(request -> {

// Let's say we have to call a blocking API (e.g. JDBC) to execute a query for each
// request. We can't do this directly or it will block the event loop
// But you can do this using executeBlocking:

vertx.<String>executeBlocking(() -> {
vertx.executeBlocking(() -> {

// Do the blocking operation in here

Expand All @@ -37,9 +38,11 @@ public void start() throws Exception {
.response()
.putHeader("content-type", "text/plain")
.end(res))
.onFailure(Throwable::printStackTrace);
.onFailure(err -> request
.response()
.setStatusCode(500)
.end());

}).listen(8080);

}
}
Loading

0 comments on commit 00b121c

Please sign in to comment.