Skip to content

Commit 4ff0d94

Browse files
committed
BAEL-778 initial commit
1 parent e59ecbc commit 4ff0d94

12 files changed

Lines changed: 348 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
</properties>
2525

2626
<modules>
27+
<module>vertx-spring</module>
2728
<module>aws</module>
2829
<module>akka-streams</module>
2930
<module>algorithms</module>

‎vertx-spring/pom.xml‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>vertx-spring</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>vertx-spring</name>
12+
<description>A demo project with vertx spring integration</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.3.RELEASE</version>
18+
<relativePath /> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
<vertx.version>3.4.1</vertx.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-jpa</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-web</artifactId>
36+
<exclusions>
37+
<exclusion>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-tomcat</artifactId>
40+
</exclusion>
41+
</exclusions>
42+
</dependency>
43+
<dependency>
44+
<groupId>io.vertx</groupId>
45+
<artifactId>vertx-core</artifactId>
46+
<version>${vertx.version}</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>io.vertx</groupId>
50+
<artifactId>vertx-web</artifactId>
51+
<version>${vertx.version}</version>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>com.h2database</groupId>
56+
<artifactId>h2</artifactId>
57+
<scope>runtime</scope>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-starter-test</artifactId>
62+
<scope>test</scope>
63+
</dependency>
64+
</dependencies>
65+
66+
<build>
67+
<plugins>
68+
<plugin>
69+
<groupId>org.springframework.boot</groupId>
70+
<artifactId>spring-boot-maven-plugin</artifactId>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
75+
76+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.vertxspring;
2+
3+
import javax.annotation.PostConstruct;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.SpringApplication;
7+
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
import org.springframework.boot.autoconfigure.domain.EntityScan;
9+
import org.springframework.context.annotation.ComponentScan;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
12+
13+
import com.baeldung.vertxspring.verticles.ServerVerticle;
14+
import com.baeldung.vertxspring.verticles.ServiceVerticle;
15+
16+
import io.vertx.core.Vertx;
17+
18+
@SpringBootApplication
19+
@Configuration
20+
@EnableJpaRepositories("com.baeldung.vertxspring.repository")
21+
@EntityScan("com.baeldung.vertxspring.entity")
22+
@ComponentScan(basePackages = { "com.baeldung" })
23+
public class VertxSpringApplication {
24+
25+
@Autowired
26+
ServerVerticle serverVerticle;
27+
28+
@Autowired
29+
ServiceVerticle serviceVerticle;
30+
31+
public static void main(String[] args) {
32+
SpringApplication.run(VertxSpringApplication.class, args);
33+
}
34+
35+
@PostConstruct
36+
public void deployVerticle() {
37+
final Vertx vertx = Vertx.vertx();
38+
vertx.deployVerticle(serverVerticle);
39+
vertx.deployVerticle(serviceVerticle);
40+
}
41+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.vertxspring.entity;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
6+
import org.springframework.data.annotation.PersistenceConstructor;
7+
8+
@Entity
9+
public class Article {
10+
11+
@Id
12+
private Long id;
13+
private String article;
14+
15+
private Article() {
16+
}
17+
18+
@PersistenceConstructor
19+
public Article(Long id, String article) {
20+
super();
21+
this.id = id;
22+
this.article = article;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
return "Article [id=" + id + ", article=" + article + "]";
28+
}
29+
30+
public Long getArticleId() {
31+
return id;
32+
}
33+
34+
public void setArticleId(Long id) {
35+
this.id = id;
36+
}
37+
38+
public String getArticle() {
39+
return article;
40+
}
41+
42+
public void setArticle(String article) {
43+
this.article = article;
44+
}
45+
46+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.vertxspring.repository;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
import com.baeldung.vertxspring.entity.Article;
7+
8+
@Repository
9+
public interface ArticleRepository extends CrudRepository<Article, Long> {
10+
11+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.vertxspring.service;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.stereotype.Service;
8+
9+
import com.baeldung.vertxspring.entity.Article;
10+
import com.baeldung.vertxspring.repository.ArticleRepository;
11+
12+
@Service
13+
public class ArticleService {
14+
15+
@Autowired
16+
ArticleRepository articleRepository;
17+
18+
public List<Article> getAllArticle() {
19+
List<Article> articles = new ArrayList<>();
20+
articleRepository.findAll()
21+
.forEach(articles::add);
22+
return articles;
23+
}
24+
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.vertxspring.util;
2+
3+
import java.util.Random;
4+
import java.util.UUID;
5+
import java.util.stream.IntStream;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.CommandLineRunner;
9+
import org.springframework.stereotype.Component;
10+
11+
import com.baeldung.vertxspring.entity.Article;
12+
import com.baeldung.vertxspring.repository.ArticleRepository;
13+
14+
@Component
15+
public class DbBootstrap implements CommandLineRunner {
16+
17+
@Autowired
18+
private ArticleRepository articleRepository;
19+
20+
@Override
21+
public void run(String... arg0) throws Exception {
22+
23+
IntStream.range(0, 10)
24+
.forEach(count -> this.articleRepository.save(new Article(new Random().nextLong(), UUID.randomUUID()
25+
.toString())));
26+
27+
}
28+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.vertxspring.verticles;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
import io.vertx.core.AbstractVerticle;
6+
import io.vertx.ext.web.Router;
7+
import io.vertx.ext.web.RoutingContext;
8+
9+
@Component
10+
public class ServerVerticle extends AbstractVerticle {
11+
12+
private void getAllArticlesHandler(RoutingContext routingContext) {
13+
vertx.eventBus()
14+
.<String> send(ServiceVerticle.GET_ALL_ARTICLES, "", result -> {
15+
if (result.succeeded()) {
16+
routingContext.response()
17+
.putHeader("content-type", "application/json")
18+
.setStatusCode(200)
19+
.end(result.result()
20+
.body());
21+
} else {
22+
routingContext.response()
23+
.setStatusCode(500)
24+
.end();
25+
}
26+
});
27+
}
28+
29+
@Override
30+
public void start() throws Exception {
31+
super.start();
32+
33+
Router router = Router.router(vertx);
34+
router.get("/api/baeldung/articles")
35+
.handler(this::getAllArticlesHandler);
36+
37+
vertx.createHttpServer()
38+
.requestHandler(router::accept)
39+
.listen(config().getInteger("http.port", 8080));
40+
}
41+
42+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.vertxspring.verticles;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
import com.baeldung.vertxspring.service.ArticleService;
7+
import com.fasterxml.jackson.core.JsonProcessingException;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
10+
import io.vertx.core.AbstractVerticle;
11+
import io.vertx.core.Handler;
12+
import io.vertx.core.eventbus.Message;
13+
import io.vertx.core.json.Json;
14+
15+
@Component
16+
public class ServiceVerticle extends AbstractVerticle {
17+
18+
public static final String GET_ALL_ARTICLES = "get.artilces.all";
19+
20+
@Autowired
21+
private ArticleService articleService;
22+
private final ObjectMapper mapper = Json.mapper;
23+
24+
@Override
25+
public void start() throws Exception {
26+
super.start();
27+
vertx.eventBus()
28+
.<String> consumer(GET_ALL_ARTICLES)
29+
.handler(getAllArticleService(articleService));
30+
}
31+
32+
private Handler<Message<String>> getAllArticleService(ArticleService service) {
33+
return msg -> vertx.<String> executeBlocking(future -> {
34+
try {
35+
future.complete(mapper.writeValueAsString(service.getAllArticle()));
36+
} catch (JsonProcessingException e) {
37+
System.out.println("Failed to serialize result");
38+
future.fail(e);
39+
}
40+
} , result -> {
41+
if (result.succeeded()) {
42+
msg.reply(result.result());
43+
} else {
44+
msg.reply(result.cause()
45+
.toString());
46+
}
47+
});
48+
}
49+
}

‎vertx-spring/src/main/resources/application.properties‎

Whitespace-only changes.

0 commit comments

Comments
 (0)