diff --git a/pom.xml b/pom.xml index 9c24200a0b..312e2aef9f 100644 --- a/pom.xml +++ b/pom.xml @@ -203,9 +203,8 @@ apache-solrj rabbitmq - - - + vertx + @@ -216,4 +215,4 @@ --> - + \ No newline at end of file diff --git a/vertx/pom.xml b/vertx/pom.xml new file mode 100644 index 0000000000..971a61d336 --- /dev/null +++ b/vertx/pom.xml @@ -0,0 +1,111 @@ + + + 4.0.0 + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + com.baeldung + vertx + 1.0-SNAPSHOT + vertx + http://maven.apache.org + + + junit + junit + 4.12 + test + + + + io.vertx + vertx-core + 3.0.0 + + + + io.vertx + vertx-web + 3.0.0 + + + + io.vertx + vertx-unit + 3.0.0 + test + + + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + + + + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-shade-plugin + 2.3 + + + package + + shade + + + + + + io.vertx.core.Starter + com.baeldung.SimpleServerVerticle + + + + + ${project.build.directory}/${project.artifactId}-${project.version}-app.jar + + + + + + + + + + + 1.7.21 + 1.1.7 + + + 6.10 + + + 3.6.0 + 2.19.1 + + + + diff --git a/vertx/src/main/conf/conf.json b/vertx/src/main/conf/conf.json new file mode 100644 index 0000000000..4fa43ee648 --- /dev/null +++ b/vertx/src/main/conf/conf.json @@ -0,0 +1,3 @@ +{ + "http.port":8080 +} \ No newline at end of file diff --git a/vertx/src/main/java/com/baeldung/HelloVerticle.java b/vertx/src/main/java/com/baeldung/HelloVerticle.java new file mode 100644 index 0000000000..98d1b336a3 --- /dev/null +++ b/vertx/src/main/java/com/baeldung/HelloVerticle.java @@ -0,0 +1,28 @@ +package com.baeldung; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.vertx.core.AbstractVerticle; +import io.vertx.core.Future; +import io.vertx.core.Vertx; + +public class HelloVerticle extends AbstractVerticle { + private static final Logger LOGGER = LoggerFactory.getLogger(HelloVerticle.class); + + @Override + public void start(Future future) { + LOGGER.info("Welcome to Vertx"); + } + + @Override + public void stop() { + LOGGER.info("Shutting down application"); + } + + public static void main(String[] args) { + Vertx vertx = Vertx.vertx(); + vertx.deployVerticle(new HelloVerticle()); + } +} + diff --git a/vertx/src/main/java/com/baeldung/SimpleServerVerticle.java b/vertx/src/main/java/com/baeldung/SimpleServerVerticle.java new file mode 100644 index 0000000000..2cee37903b --- /dev/null +++ b/vertx/src/main/java/com/baeldung/SimpleServerVerticle.java @@ -0,0 +1,26 @@ +package com.baeldung; + +import io.vertx.core.AbstractVerticle; +import io.vertx.core.Future; + +public class SimpleServerVerticle extends AbstractVerticle { + + @Override + public void start(Future future) { + vertx.createHttpServer() + .requestHandler(request -> { + request.response() + .end("Welcome to Vert.x Intro"); + }) + .listen(config().getInteger("http.port", 8080), result -> { + if (result.succeeded()) { + future.complete(); + } else { + future.fail(result.cause()); + } + }); + } + +} + + diff --git a/vertx/src/main/java/com/baeldung/model/Article.java b/vertx/src/main/java/com/baeldung/model/Article.java new file mode 100644 index 0000000000..9f1fdb8203 --- /dev/null +++ b/vertx/src/main/java/com/baeldung/model/Article.java @@ -0,0 +1,59 @@ +package com.baeldung.model; + +public class Article { + private String id; + private String content; + private String author; + private String datePublished; + private int wordCount; + + public Article(String id, String content, String author, String datePublished, int wordCount) { + super(); + this.id = id; + this.content = content; + this.author = author; + this.datePublished = datePublished; + this.wordCount = wordCount; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getDatePublished() { + return datePublished; + } + + public void setDatePublished(String datePublished) { + this.datePublished = datePublished; + } + + public int getWordCount() { + return wordCount; + } + + public void setWordCount(int wordCount) { + this.wordCount = wordCount; + } + +} diff --git a/vertx/src/main/java/com/baledung/rest/RestServiceVerticle.java b/vertx/src/main/java/com/baledung/rest/RestServiceVerticle.java new file mode 100644 index 0000000000..181b3007d5 --- /dev/null +++ b/vertx/src/main/java/com/baledung/rest/RestServiceVerticle.java @@ -0,0 +1,41 @@ +package com.baledung.rest; + +import com.baeldung.model.Article; + +import io.vertx.core.AbstractVerticle; +import io.vertx.core.Future; +import io.vertx.core.json.Json; +import io.vertx.ext.web.Router; +import io.vertx.ext.web.RoutingContext; + +public class RestServiceVerticle extends AbstractVerticle { + @Override + public void start(Future future) { + + Router router = Router.router(vertx); + router.get("/api/baeldung/articles/article/:id") + .handler(this::getArticles); + + vertx.createHttpServer() + .requestHandler(router::accept) + .listen(config().getInteger("http.port", 8080), result -> { + if (result.succeeded()) { + future.complete(); + } else { + future.fail(result.cause()); + } + }); + } + + private void getArticles(RoutingContext routingContext) { + String articleId = routingContext.request() + .getParam("id"); + Article article = new Article(articleId, "This is an intro to vertx", "baeldung", "01-02-2017", 1578); + + routingContext.response() + .putHeader("content-type", "application/json") + .setStatusCode(200) + .end(Json.encodePrettily(article)); + } + +} diff --git a/vertx/src/resources/logback.xml b/vertx/src/resources/logback.xml new file mode 100644 index 0000000000..e9ae1894a6 --- /dev/null +++ b/vertx/src/resources/logback.xml @@ -0,0 +1,14 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + \ No newline at end of file diff --git a/vertx/src/test/java/com/baeldung/RestServiceVerticleTest.java b/vertx/src/test/java/com/baeldung/RestServiceVerticleTest.java new file mode 100644 index 0000000000..8eebe1396d --- /dev/null +++ b/vertx/src/test/java/com/baeldung/RestServiceVerticleTest.java @@ -0,0 +1,46 @@ +package com.baeldung; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.baledung.rest.RestServiceVerticle; + +import io.vertx.core.Vertx; +import io.vertx.ext.unit.Async; +import io.vertx.ext.unit.TestContext; +import io.vertx.ext.unit.junit.VertxUnitRunner; + +@RunWith(VertxUnitRunner.class) +public class RestServiceVerticleTest { + + private Vertx vertx; + + @Before + public void setup(TestContext testContext) { + vertx = Vertx.vertx(); + + vertx.deployVerticle(RestServiceVerticle.class.getName(), testContext.asyncAssertSuccess()); + } + + @After + public void tearDown(TestContext testContext) { + vertx.close(testContext.asyncAssertSuccess()); + } + + @Test + public void givenId_whenReceivedArticle_thenSuccess(TestContext testContext) { + final Async async = testContext.async(); + + vertx.createHttpClient() + .getNow(8080, "localhost", "/api/baeldung/articles/article/12345", response -> { + response.handler(responseBody -> { + testContext.assertTrue(responseBody.toString() + .contains("\"id\" : \"12345\"")); + async.complete(); + }); + }); + } + +} diff --git a/vertx/src/test/java/com/baeldung/SimpleServerVerticleTest.java b/vertx/src/test/java/com/baeldung/SimpleServerVerticleTest.java new file mode 100644 index 0000000000..177f8d8435 --- /dev/null +++ b/vertx/src/test/java/com/baeldung/SimpleServerVerticleTest.java @@ -0,0 +1,47 @@ +package com.baeldung; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import io.vertx.core.DeploymentOptions; +import io.vertx.core.Vertx; +import io.vertx.core.json.JsonObject; +import io.vertx.ext.unit.Async; +import io.vertx.ext.unit.TestContext; +import io.vertx.ext.unit.junit.VertxUnitRunner; + +@RunWith(VertxUnitRunner.class) +public class SimpleServerVerticleTest { + private Vertx vertx; + + @Before + public void setup(TestContext testContext) { + vertx = Vertx.vertx(); + + vertx.deployVerticle(SimpleServerVerticle.class.getName(), + testContext.asyncAssertSuccess()); + } + + @After + public void tearDown(TestContext testContext) { + vertx.close(testContext.asyncAssertSuccess()); + } + + @Test + public void whenReceivedResponse_thenSuccess(TestContext testContext) { + final Async async = testContext.async(); + + vertx.createHttpClient() + .getNow(8080, "localhost", "/", response -> { + response.handler(responseBody -> { + testContext.assertTrue(responseBody.toString() + .contains("Welcome")); + async.complete(); + }); + }); + } + +} +