Skip to content

Commit 32147b4

Browse files
committed
BAEL-702 Intro to Vert.x Initial commit
1 parent 9063961 commit 32147b4

10 files changed

Lines changed: 378 additions & 4 deletions

File tree

‎pom.xml‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,8 @@
203203
<module>apache-solrj</module>
204204

205205
<module>rabbitmq</module>
206-
207-
208-
</modules>
206+
<module>vertx</module>
207+
</modules>
209208

210209
<build>
211210
<extensions>
@@ -216,4 +215,4 @@
216215
</extension>-->
217216
</extensions>
218217
</build>
219-
</project>
218+
</project>

‎vertx/pom.xml‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?xml version="1.0"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>parent-modules</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
</parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>vertx</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<name>vertx</name>
15+
<url>http://maven.apache.org</url>
16+
<dependencies>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.12</version>
21+
<scope>test</scope>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>io.vertx</groupId>
26+
<artifactId>vertx-core</artifactId>
27+
<version>3.0.0</version>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>io.vertx</groupId>
32+
<artifactId>vertx-web</artifactId>
33+
<version>3.0.0</version>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>io.vertx</groupId>
38+
<artifactId>vertx-unit</artifactId>
39+
<version>3.0.0</version>
40+
<scope>test</scope>
41+
</dependency>
42+
43+
<!-- logging -->
44+
45+
<dependency>
46+
<groupId>org.slf4j</groupId>
47+
<artifactId>slf4j-api</artifactId>
48+
<version>${org.slf4j.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>ch.qos.logback</groupId>
52+
<artifactId>logback-classic</artifactId>
53+
<version>${logback.version}</version>
54+
</dependency>
55+
</dependencies>
56+
57+
<build>
58+
<plugins>
59+
<plugin>
60+
<artifactId>maven-compiler-plugin</artifactId>
61+
<version>3.3</version>
62+
<configuration>
63+
<source>1.8</source>
64+
<target>1.8</target>
65+
</configuration>
66+
</plugin>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-shade-plugin</artifactId>
70+
<version>2.3</version>
71+
<executions>
72+
<execution>
73+
<phase>package</phase>
74+
<goals>
75+
<goal>shade</goal>
76+
</goals>
77+
<configuration>
78+
<transformers>
79+
<transformer
80+
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
81+
<manifestEntries>
82+
<Main-Class>io.vertx.core.Starter</Main-Class>
83+
<Main-Verticle>com.baeldung.SimpleServerVerticle</Main-Verticle>
84+
</manifestEntries>
85+
</transformer>
86+
</transformers>
87+
<artifactSet />
88+
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-app.jar</outputFile>
89+
</configuration>
90+
</execution>
91+
</executions>
92+
</plugin>
93+
</plugins>
94+
</build>
95+
96+
<properties>
97+
98+
<!-- logging -->
99+
<org.slf4j.version>1.7.21</org.slf4j.version>
100+
<logback.version>1.1.7</logback.version>
101+
102+
<!-- testing -->
103+
<testng.version>6.10</testng.version>
104+
105+
<!-- maven plugins -->
106+
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
107+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
108+
109+
</properties>
110+
111+
</project>

‎vertx/src/main/conf/conf.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"http.port":8080
3+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import io.vertx.core.AbstractVerticle;
7+
import io.vertx.core.Future;
8+
import io.vertx.core.Vertx;
9+
10+
public class HelloVerticle extends AbstractVerticle {
11+
private static final Logger LOGGER = LoggerFactory.getLogger(HelloVerticle.class);
12+
13+
@Override
14+
public void start(Future<Void> future) {
15+
LOGGER.info("Welcome to Vertx");
16+
}
17+
18+
@Override
19+
public void stop() {
20+
LOGGER.info("Shutting down application");
21+
}
22+
23+
public static void main(String[] args) {
24+
Vertx vertx = Vertx.vertx();
25+
vertx.deployVerticle(new HelloVerticle());
26+
}
27+
}
28+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung;
2+
3+
import io.vertx.core.AbstractVerticle;
4+
import io.vertx.core.Future;
5+
6+
public class SimpleServerVerticle extends AbstractVerticle {
7+
8+
@Override
9+
public void start(Future<Void> future) {
10+
vertx.createHttpServer()
11+
.requestHandler(request -> {
12+
request.response()
13+
.end("Welcome to Vert.x Intro");
14+
})
15+
.listen(config().getInteger("http.port", 8080), result -> {
16+
if (result.succeeded()) {
17+
future.complete();
18+
} else {
19+
future.fail(result.cause());
20+
}
21+
});
22+
}
23+
24+
}
25+
26+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.model;
2+
3+
public class Article {
4+
private String id;
5+
private String content;
6+
private String author;
7+
private String datePublished;
8+
private int wordCount;
9+
10+
public Article(String id, String content, String author, String datePublished, int wordCount) {
11+
super();
12+
this.id = id;
13+
this.content = content;
14+
this.author = author;
15+
this.datePublished = datePublished;
16+
this.wordCount = wordCount;
17+
}
18+
19+
public String getId() {
20+
return id;
21+
}
22+
23+
public void setId(String id) {
24+
this.id = id;
25+
}
26+
27+
public String getContent() {
28+
return content;
29+
}
30+
31+
public void setContent(String content) {
32+
this.content = content;
33+
}
34+
35+
public String getAuthor() {
36+
return author;
37+
}
38+
39+
public void setAuthor(String author) {
40+
this.author = author;
41+
}
42+
43+
public String getDatePublished() {
44+
return datePublished;
45+
}
46+
47+
public void setDatePublished(String datePublished) {
48+
this.datePublished = datePublished;
49+
}
50+
51+
public int getWordCount() {
52+
return wordCount;
53+
}
54+
55+
public void setWordCount(int wordCount) {
56+
this.wordCount = wordCount;
57+
}
58+
59+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baledung.rest;
2+
3+
import com.baeldung.model.Article;
4+
5+
import io.vertx.core.AbstractVerticle;
6+
import io.vertx.core.Future;
7+
import io.vertx.core.json.Json;
8+
import io.vertx.ext.web.Router;
9+
import io.vertx.ext.web.RoutingContext;
10+
11+
public class RestServiceVerticle extends AbstractVerticle {
12+
@Override
13+
public void start(Future<Void> future) {
14+
15+
Router router = Router.router(vertx);
16+
router.get("/api/baeldung/articles/article/:id")
17+
.handler(this::getArticles);
18+
19+
vertx.createHttpServer()
20+
.requestHandler(router::accept)
21+
.listen(config().getInteger("http.port", 8080), result -> {
22+
if (result.succeeded()) {
23+
future.complete();
24+
} else {
25+
future.fail(result.cause());
26+
}
27+
});
28+
}
29+
30+
private void getArticles(RoutingContext routingContext) {
31+
String articleId = routingContext.request()
32+
.getParam("id");
33+
Article article = new Article(articleId, "This is an intro to vertx", "baeldung", "01-02-2017", 1578);
34+
35+
routingContext.response()
36+
.putHeader("content-type", "application/json")
37+
.setStatusCode(200)
38+
.end(Json.encodePrettily(article));
39+
}
40+
41+
}

‎vertx/src/resources/logback.xml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<configuration>
2+
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
6+
</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<root level="INFO">
11+
<appender-ref ref="STDOUT" />
12+
</root>
13+
14+
</configuration>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
8+
import com.baledung.rest.RestServiceVerticle;
9+
10+
import io.vertx.core.Vertx;
11+
import io.vertx.ext.unit.Async;
12+
import io.vertx.ext.unit.TestContext;
13+
import io.vertx.ext.unit.junit.VertxUnitRunner;
14+
15+
@RunWith(VertxUnitRunner.class)
16+
public class RestServiceVerticleTest {
17+
18+
private Vertx vertx;
19+
20+
@Before
21+
public void setup(TestContext testContext) {
22+
vertx = Vertx.vertx();
23+
24+
vertx.deployVerticle(RestServiceVerticle.class.getName(), testContext.asyncAssertSuccess());
25+
}
26+
27+
@After
28+
public void tearDown(TestContext testContext) {
29+
vertx.close(testContext.asyncAssertSuccess());
30+
}
31+
32+
@Test
33+
public void givenId_whenReceivedArticle_thenSuccess(TestContext testContext) {
34+
final Async async = testContext.async();
35+
36+
vertx.createHttpClient()
37+
.getNow(8080, "localhost", "/api/baeldung/articles/article/12345", response -> {
38+
response.handler(responseBody -> {
39+
testContext.assertTrue(responseBody.toString()
40+
.contains("\"id\" : \"12345\""));
41+
async.complete();
42+
});
43+
});
44+
}
45+
46+
}

0 commit comments

Comments
 (0)