Skip to content

Commit

Permalink
More verticle base
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Nov 7, 2024
1 parent ed1e84d commit 375763f
Show file tree
Hide file tree
Showing 22 changed files with 245 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import io.vertx.camel.CamelBridge;
import io.vertx.camel.CamelBridgeOptions;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.Vertx;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
Expand All @@ -16,7 +17,7 @@
/**
* @author <a href="http://escoffier.me">Clement Escoffier</a>
*/
public class FeedExample extends AbstractVerticle {
public class FeedExample extends VerticleBase {

private final static String VERTX_BLOG_ATOM = "http://vertx.io/feed.xml";

Expand All @@ -27,7 +28,7 @@ public static void main(String[] args) {


@Override
public void start() throws Exception {
public Future<?> start() throws Exception {
vertx.eventBus().consumer("announce", message -> {
System.out.println("ANNOUNCE >> " + message.body());
});
Expand All @@ -46,7 +47,7 @@ public void start() throws Exception {

camelContext.start();

CamelBridge.create(vertx, new CamelBridgeOptions(camelContext)
return CamelBridge.create(vertx, new CamelBridgeOptions(camelContext)
.addInboundMapping(fromCamel("seda:announce").toVertx("announce"))
.addInboundMapping(fromCamel("seda:errors").toVertx("errors")))
.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import io.vertx.camel.CamelBridge;
import io.vertx.camel.CamelBridgeOptions;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServerRequest;
import org.apache.camel.CamelContext;
Expand All @@ -14,7 +15,7 @@
/**
* @author <a href="http://escoffier.me">Clement Escoffier</a>
*/
public class RMIExample extends AbstractVerticle {
public class RMIExample extends VerticleBase {

public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
Expand All @@ -23,18 +24,17 @@ public static void main(String[] args) {


@Override
public void start() {
public Future<?> start() {
ApplicationContext app = new ClassPathXmlApplicationContext("META-INF/spring/camelContext.xml");
CamelContext camel = app.getBean("camel", CamelContext.class);

CamelBridge.create(vertx, new CamelBridgeOptions(camel)
.addOutboundMapping(fromVertx("invocation").toCamel("rmiService")))
.start();

vertx.createHttpServer()
return vertx.createHttpServer()
.requestHandler(this::invoke)
.listen(8080);

}

private void invoke(HttpServerRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import io.vertx.circuitbreaker.CircuitBreaker;
import io.vertx.circuitbreaker.CircuitBreakerOptions;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClientAgent;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpResponseExpectation;
Expand Down
19 changes: 7 additions & 12 deletions junit5-examples/src/main/java/hello/SampleVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,28 @@

package hello;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author <a href="https://julien.ponge.org/">Julien Ponge</a>
*/
public class SampleVerticle extends AbstractVerticle {
public class SampleVerticle extends VerticleBase {

private final Logger logger = LoggerFactory.getLogger(SampleVerticle.class);

@Override
public void start(Promise<Void> startPromise) {
vertx.createHttpServer()
public Future<?> start() {
return vertx
.createHttpServer()
.requestHandler(req -> {
req.response()
.putHeader("Content-Type", "plain/text")
.end("Yo!");
logger.info("Handled a request on path {} from {}", req.path(), req.remoteAddress().host());
})
.listen(11981).onComplete(ar -> {
if (ar.succeeded()) {
startPromise.complete();
} else {
startPromise.fail(ar.cause());
}
});
.listen(11981);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package io.vertx.example.micrometer.verticles;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;

/**
* @author Joel Takvorian, [email protected]
*/
public class EventbusConsumer extends AbstractVerticle {
public class EventbusConsumer extends VerticleBase {
@Override
public void start() throws Exception {
vertx.eventBus().<String>consumer("greeting", message -> {
String greeting = message.body();
System.out.println("Received: " + greeting);
Greetings.get(vertx)
.onComplete(greetingResult -> message.reply(greetingResult.result()));
});
public Future<?> start() {
return vertx
.eventBus()
.<String>consumer("greeting", message -> {
String greeting = message.body();
System.out.println("Received: " + greeting);
Greetings.get(vertx)
.onComplete(greetingResult -> message.reply(greetingResult.result()));
})
.completion();
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package io.vertx.example.micrometer.verticles;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;

/**
* @author Joel Takvorian, [email protected]
*/
public class EventbusProducer extends AbstractVerticle {
public class EventbusProducer extends VerticleBase {

@Override
public void start() throws Exception {
public Future<?> start() throws Exception {
vertx.setPeriodic(1000, x -> {
Greetings.get(vertx)
.onComplete(greetingResult -> vertx.eventBus().send("greeting", greetingResult.result()));
});
return super.start();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package io.vertx.example.micrometer.verticles;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.ext.web.Router;

/**
* @author Joel Takvorian, [email protected]
*/
public class SimpleWebServer extends AbstractVerticle {
public class SimpleWebServer extends VerticleBase {
@Override
public void start() throws Exception {
public Future<?> start() throws Exception {
Router router = Router.router(vertx);
router.get("/").handler(ctx -> {
Greetings.get(vertx).onComplete(greetingResult -> ctx.response().end(greetingResult.result()));
});
vertx.createHttpServer().requestHandler(router).listen(8080);
return vertx
.createHttpServer()
.requestHandler(router)
.listen(8080);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package io.vertx.example.micrometer.verticles;

import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.ext.web.Router;
import io.vertx.micrometer.backends.BackendRegistries;

import java.util.function.Consumer;

/**
* @author Joel Takvorian, [email protected]
*/
public class WebServerForBoundPrometheus extends AbstractVerticle {
public class WebServerForBoundPrometheus extends VerticleBase {

@Override
public void start() throws Exception {
public Future<?> start() {
Router router = Router.router(vertx);
PrometheusMeterRegistry registry = (PrometheusMeterRegistry) BackendRegistries.getDefaultNow();
// Setup a route for metrics
Expand All @@ -24,6 +23,9 @@ public void start() throws Exception {
router.get("/").handler(ctx -> {
Greetings.get(vertx).onComplete(greetingResult -> ctx.response().end(greetingResult.result()));
});
vertx.createHttpServer().requestHandler(router).listen(8080);
return vertx
.createHttpServer()
.requestHandler(router)
.listen(8080);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static void main(String[] args) {
private MqttClient client;

@Override
public Future<?> start() throws Exception {
public Future<?> start() {
MqttClientOptions options = new MqttClientOptions().setKeepAliveInterval(2);

MqttClient client = MqttClient.create(Vertx.vertx(), options);
Expand Down
49 changes: 26 additions & 23 deletions mqtt-examples/src/main/java/io/vertx/example/mqtt/ssl/Client.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package io.vertx.example.mqtt.ssl;

import io.netty.handler.codec.mqtt.MqttQoS;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.buffer.Buffer;
import io.vertx.launcher.application.VertxApplication;
import io.vertx.mqtt.MqttClient;
import io.vertx.mqtt.MqttClientOptions;

public class Client extends AbstractVerticle {
public class Client extends VerticleBase {

private static final String MQTT_TOPIC = "/my_topic";
private static final String MQTT_MESSAGE = "Hello Vert.x MQTT Client";
Expand All @@ -18,31 +19,33 @@ public static void main(String[] args) {
VertxApplication.main(new String[]{Client.class.getName()});
}

private MqttClient mqttClient;

@Override
public void start() throws Exception {
public Future<?> start() {
MqttClientOptions options = new MqttClientOptions();
options.setSsl(true);
options.setTrustAll(true);
options.setSsl(true);

MqttClient mqttClient = MqttClient.create(vertx, options);
// In real life you would use a certificate and not trust any server
options.setTrustAll(true);
options.setHostnameVerificationAlgorithm("http");

mqttClient.connect(BROKER_PORT, BROKER_HOST).onComplete(ch -> {
if (ch.succeeded()) {
System.out.println("Connected to a server");
mqttClient = MqttClient.create(vertx, options);

mqttClient.publish(
MQTT_TOPIC,
Buffer.buffer(MQTT_MESSAGE),
MqttQoS.AT_MOST_ONCE,
false,
false)
.compose(s -> mqttClient.disconnect())
.onComplete(d -> System.out.println("Disconnected from server"));
} else {
System.out.println("Failed to connect to a server");
System.out.println(ch.cause());
}
});
return
mqttClient.connect(BROKER_PORT, BROKER_HOST)
.compose(ack -> {
System.out.println("Connected to a server");
return mqttClient.publish(
MQTT_TOPIC,
Buffer.buffer(MQTT_MESSAGE),
MqttQoS.AT_MOST_ONCE,
false,
false)
.onSuccess(ignore -> System.out.println("Message published"));
})
.eventually(() -> mqttClient
.disconnect()
.onComplete(ar -> System.out.println("Disconnected from server")));
}

}
18 changes: 6 additions & 12 deletions mqtt-examples/src/main/java/io/vertx/example/mqtt/ssl/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

package io.vertx.example.mqtt.ssl;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.net.PemKeyCertOptions;
import io.vertx.launcher.application.VertxApplication;
import io.vertx.mqtt.MqttServer;
Expand All @@ -25,14 +26,14 @@
/**
* An example of using the MQTT server with TLS support
*/
public class Server extends AbstractVerticle {
public class Server extends VerticleBase {

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

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

MqttServerOptions options = new MqttServerOptions()
.setPort(8883)
Expand All @@ -43,7 +44,7 @@ public void start() throws Exception {

MqttServer mqttServer = MqttServer.create(vertx, options);

mqttServer
return mqttServer
.endpointHandler(endpoint -> {

// shows main connect info
Expand All @@ -54,13 +55,6 @@ public void start() throws Exception {
endpoint.accept(false);

})
.listen().onComplete(ar -> {

if (ar.succeeded()) {
System.out.println("MQTT server is listening on port " + mqttServer.actualPort());
} else {
System.err.println("Error on starting the server" + ar.cause().getMessage());
}
});
.listen().onSuccess(ar -> System.out.println("MQTT server is listening on port " + mqttServer.actualPort()));
}
}
Loading

0 comments on commit 375763f

Please sign in to comment.