BAEL-702 Intro to Vert.x Initial commit

This commit is contained in:
dhruba619 2017-03-19 22:28:34 +05:30
parent 9063961bb4
commit 32147b4459
10 changed files with 378 additions and 4 deletions

View File

@ -203,8 +203,7 @@
<module>apache-solrj</module> <module>apache-solrj</module>
<module>rabbitmq</module> <module>rabbitmq</module>
<module>vertx</module>
</modules> </modules>
<build> <build>

111
vertx/pom.xml Normal file
View File

@ -0,0 +1,111 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.baeldung</groupId>
<artifactId>vertx</artifactId>
<version>1.0-SNAPSHOT</version>
<name>vertx</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-unit</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<!-- logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>io.vertx.core.Starter</Main-Class>
<Main-Verticle>com.baeldung.SimpleServerVerticle</Main-Verticle>
</manifestEntries>
</transformer>
</transformers>
<artifactSet />
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-app.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<!-- logging -->
<org.slf4j.version>1.7.21</org.slf4j.version>
<logback.version>1.1.7</logback.version>
<!-- testing -->
<testng.version>6.10</testng.version>
<!-- maven plugins -->
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
</properties>
</project>

View File

@ -0,0 +1,3 @@
{
"http.port":8080
}

View File

@ -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<Void> 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());
}
}

View File

@ -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<Void> 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());
}
});
}
}

View File

@ -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;
}
}

View File

@ -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<Void> 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));
}
}

View File

@ -0,0 +1,14 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -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();
});
});
}
}

View File

@ -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();
});
});
}
}