Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions hello-vertx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-stack-depchain</artifactId>
<version>4.5.18</version>
<version>5.0.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>2.24.3</version>
<version>2.25.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -45,14 +45,18 @@
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-launcher-application</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency>
<dependency>
<groupId>org.folio.okapi</groupId>
<artifactId>okapi-common</artifactId>
<version>6.2.1</version>
<version>7.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand All @@ -68,7 +72,7 @@
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.0</version>
<version>3.14.1</version>
<configuration>
<release>21</release>
<compilerArgument>-Xlint:unchecked</compilerArgument>
Expand Down Expand Up @@ -135,7 +139,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<version>3.6.1</version>
<executions>
<execution>
<phase>package</phase>
Expand Down
36 changes: 17 additions & 19 deletions hello-vertx/src/main/java/org/folio/sample/MainVerticle.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.folio.sample;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import java.lang.management.ManagementFactory;
Expand All @@ -14,45 +14,43 @@
* The main verticle. This is the HTTP server that accepts incoming requests and
* routes them to the relevant handlers.
*/
public class MainVerticle extends AbstractVerticle {
public class MainVerticle extends VerticleBase {

private static final Logger logger = LogManager.getLogger(MainVerticle.class);
private static final Logger LOGGER = LogManager.getLogger(MainVerticle.class);

@Override
public void start(Promise<Void> startPromise) {
public Future<?> start() {

final int port = Integer.parseInt(System.getProperty("port", "8080"));
logger.info("Starting hello "
+ ManagementFactory.getRuntimeMXBean().getName()
+ " on port " + port);
var port = Integer.parseInt(System.getProperty("port", "8080"));
LOGGER.info("Starting hello {} on port {}",
ManagementFactory.getRuntimeMXBean().getName(),
port);

// Define the routes for HTTP requests.
Router router = Router.router(vertx);
var router = Router.router(vertx);
router.get("/hello").handler(this::getHandle);
router.post("/hello").handler(this::postHandle);

// And start listening
vertx.createHttpServer()
.requestHandler(router)
.listen(port)
.onSuccess(x -> logger.debug("Hello: Succeeded in starting the listener"))
.onFailure(e -> logger.error("Hello failed to start the listener:", e))
.<Void>mapEmpty()
.onComplete(startPromise);
return vertx.createHttpServer()
.requestHandler(router)
.listen(port)
.onSuccess(x -> LOGGER.debug("Hello: Succeeded in starting the listener"))
.onFailure(e -> LOGGER.error("Hello failed to start the listener: {}", e.getMessage(), e));
}

// Handler for the GET requests.
// Just replies "Hello, world" in plain text
public void getHandle(RoutingContext ctx) {
logger.debug("Hello: handling a GET request");
LOGGER.debug("Hello: handling a GET request");
responseText(ctx, 200).end("Hello, world\n");
}

// Handler for the POST request
// Replies with a JSON structure that contains all posted data
// As long as the input data is valid JSON, the output should be too.
public void postHandle(RoutingContext ctx) {
logger.debug("Hello: handling a POST request");
LOGGER.debug("Hello: handling a POST request");
if (! "application/json".equals(ctx.request().getHeader("Content-Type"))) {
responseError(ctx, 400, "Content-Type must be application/json");
return;
Expand Down
14 changes: 9 additions & 5 deletions simple-vertx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-stack-depchain</artifactId>
<version>4.5.18</version>
<version>5.0.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>2.24.3</version>
<version>2.25.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -46,6 +46,10 @@
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-launcher-application</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
Expand All @@ -57,7 +61,7 @@
<dependency>
<groupId>org.folio.okapi</groupId>
<artifactId>okapi-common</artifactId>
<version>6.2.1</version>
<version>7.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand All @@ -73,7 +77,7 @@
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.0</version>
<version>3.14.1</version>
<configuration>
<release>21</release>
<compilerArgument>-Xlint:unchecked</compilerArgument>
Expand Down Expand Up @@ -136,7 +140,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<version>3.6.1</version>
<executions>
<execution>
<phase>package</phase>
Expand Down
36 changes: 18 additions & 18 deletions simple-vertx/src/main/java/org/folio/simple/MainVerticle.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.folio.simple;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.VerticleBase;
import io.vertx.core.http.HttpClient;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
Expand All @@ -13,38 +15,36 @@
* The main verticle. This is the HTTP server that accepts incoming requests and
* routes them to the relevant handlers.
*/
public class MainVerticle extends AbstractVerticle {
public class MainVerticle extends VerticleBase {

private static final Logger logger = LogManager.getLogger(MainVerticle.class);
private static final Logger LOGGER = LogManager.getLogger(MainVerticle.class);

@Override
public void start(Promise<Void> startPromise) {
public Future<?> start() {
/**
* All services of one Verticle instance should share a single HttpClient (or WebClient)
* to allow for HTTP pooling and HTTP pipe-lining and to avoid HttpClient socket leaks.
*/
final HttpClient httpClient = vertx.createHttpClient();
final SimpleWebService simple = new SimpleWebService(httpClient);

final int port = Integer.parseInt(System.getProperty("port", "8080"));
logger.info("Starting simple "
+ ManagementFactory.getRuntimeMXBean().getName()
+ " on port " + port);
var port = Integer.parseInt(System.getProperty("port", "8080"));
LOGGER.info("Starting simple {} on port {}",
ManagementFactory.getRuntimeMXBean().getName(),
port);

// Define the routes for HTTP requests. Both GET and POST go to the same
// one here...
Router router = Router.router(vertx);
router.get("/simple").handler(simple::get_handle);
router.post("/*").handler(BodyHandler.create()); // Tell vertx we want to the whole POST body in the handler
router.post("/simple").handler(simple::post_handle);
var router = Router.router(vertx);
router.get("/simple").handler(simple::getHandle);
router.post("/*").handler(BodyHandler.create()); // Tell vertx we want the whole POST body in the handler
router.post("/simple").handler(simple::postHandle);

// And start listening
vertx.createHttpServer()
.requestHandler(router)
.listen(port)
.onSuccess(x -> logger.debug("Succeeded in starting the listener for simple"))
.onFailure(e -> logger.error("simple failed: " + e.getMessage(), e))
.<Void>mapEmpty()
.onComplete(startPromise);
return vertx.createHttpServer()
.requestHandler(router)
.listen(port)
.onSuccess(x -> LOGGER.debug("Succeeded in starting the listener for simple"))
.onFailure(e -> LOGGER.error("simple failed: {}", e.getMessage(), e));
}
}
28 changes: 14 additions & 14 deletions simple-vertx/src/main/java/org/folio/simple/SimpleWebService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @author heikki
*/
public class SimpleWebService {
private static final Logger logger = LogManager.getLogger(SimpleWebService.class);
private static final Logger LOGGER = LogManager.getLogger(SimpleWebService.class);
private final HttpClient httpClient;

public SimpleWebService(HttpClient httpClient) {
Expand All @@ -26,10 +26,10 @@ public SimpleWebService(HttpClient httpClient) {

// Handler for the GET requests.
// Calls the hello module, and reports its success
public void get_handle(RoutingContext ctx) {
logger.debug("Simple: Handling a GET request. About to call the hello module");
OkapiClient okapiClient = new OkapiClient(httpClient, ctx);
logger.debug("Contacting Okapi via " + okapiClient.getOkapiUrl());
public void getHandle(RoutingContext ctx) {
LOGGER.debug("Simple: Handling a GET request. About to call the hello module");
var okapiClient = new OkapiClient(httpClient, ctx);
LOGGER.debug("Contacting Okapi via {}", okapiClient.getOkapiUrl());
okapiClient.get("/hello")
.onSuccess(body -> responseText(ctx, 200).end("Hello module says: '" + body + "'"))
.onFailure(e -> responseError(ctx, 500, "Hello failed with " + e.getMessage()));
Expand All @@ -38,24 +38,24 @@ public void get_handle(RoutingContext ctx) {
// Handler for the POST request
// POSTs the same request to the hello module, and returns its response in a
// new Json response
public void post_handle(RoutingContext ctx) {
public void postHandle(RoutingContext ctx) {
if (! "application/json".equals(ctx.request().getHeader("Content-Type"))) {
responseError(ctx, 400, "Only accepts Content-Type application/json");
return;
}
String reqData = ctx.getBodyAsString();
logger.debug("Simple: Received a POST of " + reqData);
OkapiClient okapiClient = new OkapiClient(httpClient, ctx);
var reqData = ctx.body().asString();
LOGGER.debug("Simple: Received a POST of {}", reqData);
var okapiClient = new OkapiClient(httpClient, ctx);
okapiClient.setHeaders(Map.of(
"Content-Type", "application/json",
"X-Okapi-Tenant", ctx.request().getHeader("X-Okapi-Tenant")));
okapiClient.post("/hello", reqData)
.onSuccess(helloRes -> {
JsonObject hjo = new JsonObject(helloRes);
String greeting = hjo.getString("greeting");
logger.info("Got a greeting from hello: " + greeting);
hjo.put("simple", "Simple module did indeed call the hello module!");
String simpleRes = hjo.encodePrettily();
var jsonObject = new JsonObject(helloRes);
var greeting = jsonObject.getString("greeting");
LOGGER.info("Got a greeting from hello: {}", greeting);
jsonObject.put("simple", "Simple module did indeed call the hello module!");
var simpleRes = jsonObject.encodePrettily();
responseJson(ctx, 200).end(simpleRes);
})
.onFailure(e -> responseError(ctx, 500, "Hello failed with " + e.getMessage()));
Expand Down