[Java 11503] (#12626)
* [JAVA-11503] Created new vertx-modules * [JAVA-11503] Moved vertx(sub-module) to vertx-modules(parent) * [JAVA-11503] Moved spring-vertx(sub-module) to vertx-modules(parent) * [JAVA-11503] Moved vertx-and-java(sub-module) to vertx-modules(parent) * [JAVA-11503] deleted modules that were moved Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
## VERTX
|
||||
|
||||
This module contains modules about VERTX.
|
||||
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>vertx-modules</artifactId>
|
||||
<name>vertx-modules</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>vertx</module>
|
||||
<module>spring-vertx</module>
|
||||
<module>vertx-and-rxjava</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,6 @@
|
||||
## Spring Vert.x
|
||||
|
||||
This module contains articles about Spring with Vert.x
|
||||
|
||||
### Relevant Articles:
|
||||
- [Vert.x Spring Integration](https://www.baeldung.com/spring-vertx)
|
||||
@@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-vertx</artifactId>
|
||||
<name>spring-vertx</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>A demo project with vertx spring integration</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-web</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<vertx.version>3.4.1</vertx.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.vertxspring;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
import com.baeldung.vertxspring.verticles.ServerVerticle;
|
||||
import com.baeldung.vertxspring.verticles.ArticleRecipientVerticle;
|
||||
|
||||
import io.vertx.core.Vertx;
|
||||
|
||||
@SpringBootApplication
|
||||
@Configuration
|
||||
@EnableJpaRepositories("com.baeldung.vertxspring.repository")
|
||||
@EntityScan("com.baeldung.vertxspring.entity")
|
||||
@ComponentScan(basePackages = { "com.baeldung" })
|
||||
public class VertxSpringApplication {
|
||||
|
||||
@Autowired
|
||||
private ServerVerticle serverVerticle;
|
||||
|
||||
@Autowired
|
||||
private ArticleRecipientVerticle serviceVerticle;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(VertxSpringApplication.class, args);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void deployVerticle() {
|
||||
final Vertx vertx = Vertx.vertx();
|
||||
vertx.deployVerticle(serverVerticle);
|
||||
vertx.deployVerticle(serviceVerticle);
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.vertxspring.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
|
||||
@Configuration
|
||||
public class PortConfiguration {
|
||||
|
||||
private static final int DEFAULT_PORT = 8069;
|
||||
|
||||
@Profile("default")
|
||||
@Bean
|
||||
public Integer defaultPort() {
|
||||
return DEFAULT_PORT;
|
||||
}
|
||||
|
||||
@Profile("test")
|
||||
@Bean
|
||||
public Integer randomPort() {
|
||||
try (ServerSocket socket = new ServerSocket(0)) {
|
||||
return socket.getLocalPort();
|
||||
|
||||
} catch (IOException e) {
|
||||
return DEFAULT_PORT;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.vertxspring.entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.springframework.data.annotation.PersistenceConstructor;
|
||||
|
||||
@Entity
|
||||
public class Article {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
private String article;
|
||||
|
||||
private Article() {
|
||||
}
|
||||
|
||||
@PersistenceConstructor
|
||||
public Article(Long id, String article) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.article = article;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Article [id=" + id + ", article=" + article + "]";
|
||||
}
|
||||
|
||||
public Long getArticleId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setArticleId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getArticle() {
|
||||
return article;
|
||||
}
|
||||
|
||||
public void setArticle(String article) {
|
||||
this.article = article;
|
||||
}
|
||||
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.vertxspring.repository;
|
||||
|
||||
import com.baeldung.vertxspring.entity.Article;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface ArticleRepository extends JpaRepository<Article, Long> {
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.vertxspring.service;
|
||||
|
||||
import com.baeldung.vertxspring.entity.Article;
|
||||
import com.baeldung.vertxspring.repository.ArticleRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class ArticleService {
|
||||
|
||||
@Autowired
|
||||
private ArticleRepository articleRepository;
|
||||
|
||||
public List<Article> getAllArticle() {
|
||||
return articleRepository.findAll();
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.vertxspring.util;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.baeldung.vertxspring.entity.Article;
|
||||
import com.baeldung.vertxspring.repository.ArticleRepository;
|
||||
|
||||
@Component
|
||||
public class DbBootstrap implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
private ArticleRepository articleRepository;
|
||||
|
||||
@Override
|
||||
public void run(String... arg0) throws Exception {
|
||||
|
||||
IntStream.range(0, 10)
|
||||
.forEach(count -> this.articleRepository.save(new Article(new Random().nextLong(), UUID.randomUUID()
|
||||
.toString())));
|
||||
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.vertxspring.verticles;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.baeldung.vertxspring.service.ArticleService;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import io.vertx.core.AbstractVerticle;
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.core.eventbus.Message;
|
||||
import io.vertx.core.json.Json;
|
||||
|
||||
@Component
|
||||
public class ArticleRecipientVerticle extends AbstractVerticle {
|
||||
|
||||
public static final String GET_ALL_ARTICLES = "get.articles.all";
|
||||
|
||||
private final ObjectMapper mapper = Json.mapper;
|
||||
|
||||
@Autowired
|
||||
private ArticleService articleService;
|
||||
|
||||
@Override
|
||||
public void start() throws Exception {
|
||||
super.start();
|
||||
vertx.eventBus()
|
||||
.<String>consumer(GET_ALL_ARTICLES)
|
||||
.handler(getAllArticleService(articleService));
|
||||
}
|
||||
|
||||
private Handler<Message<String>> getAllArticleService(ArticleService service) {
|
||||
return msg -> vertx.<String>executeBlocking(future -> {
|
||||
try {
|
||||
future.complete(mapper.writeValueAsString(service.getAllArticle()));
|
||||
} catch (JsonProcessingException e) {
|
||||
System.out.println("Failed to serialize result");
|
||||
future.fail(e);
|
||||
}
|
||||
}, result -> {
|
||||
if (result.succeeded()) {
|
||||
msg.reply(result.result());
|
||||
} else {
|
||||
msg.reply(result.cause()
|
||||
.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.vertxspring.verticles;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import io.vertx.core.AbstractVerticle;
|
||||
import io.vertx.ext.web.Router;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
|
||||
@Component
|
||||
public class ServerVerticle extends AbstractVerticle {
|
||||
|
||||
@Autowired
|
||||
private Integer defaultPort;
|
||||
|
||||
private void getAllArticlesHandler(RoutingContext routingContext) {
|
||||
vertx.eventBus()
|
||||
.<String>send(ArticleRecipientVerticle.GET_ALL_ARTICLES, "", result -> {
|
||||
if (result.succeeded()) {
|
||||
routingContext.response()
|
||||
.putHeader("content-type", "application/json")
|
||||
.setStatusCode(200)
|
||||
.end(result.result()
|
||||
.body());
|
||||
} else {
|
||||
routingContext.response()
|
||||
.setStatusCode(500)
|
||||
.end();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() throws Exception {
|
||||
super.start();
|
||||
|
||||
Router router = Router.router(vertx);
|
||||
router.get("/api/baeldung/articles")
|
||||
.handler(this::getAllArticlesHandler);
|
||||
|
||||
vertx.createHttpServer()
|
||||
.requestHandler(router::accept)
|
||||
.listen(config().getInteger("http.port", defaultPort));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"http.port":8080
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.vertxspring.VertxSpringApplication;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = VertxSpringApplication.class)
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.vertxspring;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
public class VertxSpringApplicationIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private Integer port;
|
||||
|
||||
private TestRestTemplate restTemplate = new TestRestTemplate();
|
||||
|
||||
@Test
|
||||
public void givenUrl_whenReceivedArticles_thenSuccess() throws InterruptedException {
|
||||
ResponseEntity<String> responseEntity = restTemplate
|
||||
.getForEntity("http://localhost:" + port + "/api/baeldung/articles", String.class);
|
||||
|
||||
assertEquals(200, responseEntity.getStatusCodeValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
/.vertx
|
||||
@@ -0,0 +1,6 @@
|
||||
## Vert.x and RxJava
|
||||
|
||||
This module contains articles about RxJava with Vert.x
|
||||
|
||||
### Relevant articles
|
||||
- [Example of Vertx and RxJava Integration](https://www.baeldung.com/vertx-rx-java)
|
||||
@@ -0,0 +1,52 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>vertx-and-rxjava</artifactId>
|
||||
<name>vertx-and-rxjava</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>vertx-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-rx-java2</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-core</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-unit</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<vertx.version>3.5.0.Beta1</vertx.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,71 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>vertx-and-rxjava</artifactId>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-rx-java2</artifactId>
|
||||
<version>3.5.0.Beta1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-core</artifactId>
|
||||
<version>3.5.0.Beta1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-unit</artifactId>
|
||||
<version>3.5.0.Beta1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.25</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.2.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.weather;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
|
||||
class CityAndDayLength {
|
||||
|
||||
private final String city;
|
||||
private final double dayLengthInHours;
|
||||
|
||||
CityAndDayLength(String city, long dayLengthInSeconds) {
|
||||
this.city = city;
|
||||
this.dayLengthInHours = dayLengthInSeconds / (60.0 * 60.0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return MessageFormat.format("In {0} there are {1,number,#0.0} hours of light.", city, dayLengthInHours);
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.weather;
|
||||
|
||||
import io.reactivex.Flowable;
|
||||
import io.vertx.core.http.RequestOptions;
|
||||
import io.vertx.reactivex.core.http.HttpClient;
|
||||
import io.vertx.reactivex.core.http.HttpClientRequest;
|
||||
import io.vertx.reactivex.core.http.HttpClientResponse;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
class MetaWeatherClient {
|
||||
|
||||
private static RequestOptions metawether = new RequestOptions()
|
||||
.setHost("www.metaweather.com")
|
||||
.setPort(443)
|
||||
.setSsl(true);
|
||||
|
||||
/**
|
||||
* @return A flowable backed by vertx that automatically sends an HTTP request at soon as the first subscription is received.
|
||||
*/
|
||||
private static Flowable<HttpClientResponse> autoPerformingReq(HttpClient httpClient, String uri) {
|
||||
HttpClientRequest req = httpClient.get(new RequestOptions(metawether).setURI(uri));
|
||||
return req.toFlowable()
|
||||
.doOnSubscribe(subscription -> req.end());
|
||||
}
|
||||
|
||||
static Flowable<HttpClientResponse> searchByCityName(HttpClient httpClient, String cityName) {
|
||||
HttpClientRequest req = httpClient.get(
|
||||
new RequestOptions()
|
||||
.setHost("www.metaweather.com")
|
||||
.setPort(443)
|
||||
.setSsl(true)
|
||||
.setURI(format("/api/location/search/?query=%s", cityName)));
|
||||
return req
|
||||
.toFlowable()
|
||||
.doOnSubscribe(subscription -> req.end());
|
||||
}
|
||||
|
||||
static Flowable<HttpClientResponse> getDataByPlaceId(HttpClient httpClient, long placeId) {
|
||||
return autoPerformingReq(
|
||||
httpClient,
|
||||
format("/api/location/%s/", placeId));
|
||||
}
|
||||
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package com.baeldung.weather;
|
||||
|
||||
import io.reactivex.Flowable;
|
||||
import io.reactivex.functions.Function;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.reactivex.core.Vertx;
|
||||
import io.vertx.reactivex.core.buffer.Buffer;
|
||||
import io.vertx.reactivex.core.file.FileSystem;
|
||||
import io.vertx.reactivex.core.http.HttpClient;
|
||||
import io.vertx.reactivex.core.http.HttpClientResponse;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import static com.baeldung.weather.MetaWeatherClient.getDataByPlaceId;
|
||||
import static com.baeldung.weather.MetaWeatherClient.searchByCityName;
|
||||
|
||||
public class VertxWithRxJavaIntegrationTest {
|
||||
|
||||
private Vertx vertx;
|
||||
private HttpClient httpClient;
|
||||
private FileSystem fileSystem;
|
||||
private static Logger log = LoggerFactory.getLogger(VertxWithRxJavaIntegrationTest.class);
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
vertx = Vertx.vertx();
|
||||
httpClient = vertx.createHttpClient();
|
||||
fileSystem = vertx.fileSystem();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
vertx.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDisplayLightLenghts() throws InterruptedException {
|
||||
|
||||
// read the file that contains one city name per line
|
||||
fileSystem
|
||||
.rxReadFile("cities.txt").toFlowable()
|
||||
.doOnNext(buffer -> log.info("File buffer ---\n{}\n---", buffer))
|
||||
.flatMap(buffer -> Flowable.fromArray(buffer.toString().split("\\r?\\n")))
|
||||
.doOnNext(city -> log.info("City from file: '{}'", city))
|
||||
.filter(city -> !city.startsWith("#"))
|
||||
.doOnNext(city -> log.info("City that survived filtering: '{}'", city))
|
||||
.flatMap(city -> searchByCityName(httpClient, city))
|
||||
.flatMap(HttpClientResponse::toFlowable)
|
||||
.doOnNext(buffer -> log.info("JSON of city detail: '{}'", buffer))
|
||||
.map(extractingWoeid())
|
||||
.flatMap(cityId -> getDataByPlaceId(httpClient, cityId))
|
||||
.flatMap(toBufferFlowable())
|
||||
.doOnNext(buffer -> log.info("JSON of place detail: '{}'", buffer))
|
||||
.map(Buffer::toJsonObject)
|
||||
.map(toCityAndDayLength())
|
||||
.subscribe(System.out::println, Throwable::printStackTrace);
|
||||
|
||||
Thread.sleep(20000); // enough to give time to complete the execution
|
||||
}
|
||||
|
||||
private static Function<HttpClientResponse, Publisher<? extends Buffer>> toBufferFlowable() {
|
||||
return response -> response
|
||||
.toObservable()
|
||||
.reduce(
|
||||
Buffer.buffer(),
|
||||
Buffer::appendBuffer).toFlowable();
|
||||
}
|
||||
|
||||
private static Function<Buffer, Long> extractingWoeid() {
|
||||
return cityBuffer -> cityBuffer
|
||||
.toJsonArray()
|
||||
.getJsonObject(0)
|
||||
.getLong("woeid");
|
||||
}
|
||||
|
||||
private static Function<JsonObject, CityAndDayLength> toCityAndDayLength() {
|
||||
return json -> {
|
||||
ZonedDateTime sunRise = ZonedDateTime.parse(json.getString("sun_rise"));
|
||||
ZonedDateTime sunSet = ZonedDateTime.parse(json.getString("sun_set"));
|
||||
String cityName = json.getString("title");
|
||||
return new CityAndDayLength(
|
||||
cityName, sunSet.toEpochSecond() - sunRise.toEpochSecond());
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
Milan
|
||||
Chicago
|
||||
Cairo
|
||||
Santiago
|
||||
Moscow
|
||||
Auckland
|
||||
@@ -0,0 +1,12 @@
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="warn">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
@@ -0,0 +1,7 @@
|
||||
## Vert.x
|
||||
|
||||
This module contains articles about Vert.x
|
||||
|
||||
### Relevant articles
|
||||
|
||||
- [Introduction to Vert.x](https://www.baeldung.com/vertx)
|
||||
@@ -0,0 +1,72 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>vertx</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>vertx</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>vertx-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-core</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-web</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-unit</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>${maven-shade-plugin.version}</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>
|
||||
<vertx.version>3.8.1</vertx.version>
|
||||
<maven-shade-plugin.version>3.2.1</maven-shade-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -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);
|
||||
|
||||
public static void main(String[] args) {
|
||||
Vertx vertx = Vertx.vertx();
|
||||
vertx.deployVerticle(new HelloVerticle());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Future<Void> future) {
|
||||
LOGGER.info("Welcome to Vertx");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
LOGGER.info("Shutting down application");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
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(
|
||||
r -> r.response().end("Welcome to Vert.x Intro"))
|
||||
.listen(config().getInteger("http.port", 8080), result -> {
|
||||
if (result.succeeded()) {
|
||||
future.complete();
|
||||
} else {
|
||||
future.fail(result.cause());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"http.port":8080
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.baeldung;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.baeldung.rest.RestServiceVerticle;
|
||||
|
||||
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 RestServiceVerticleUnitTest {
|
||||
|
||||
private Vertx vertx;
|
||||
|
||||
private int port = 8081;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() {
|
||||
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup(TestContext testContext) throws IOException {
|
||||
vertx = Vertx.vertx();
|
||||
|
||||
// Pick an available and random
|
||||
ServerSocket socket = new ServerSocket(0);
|
||||
port = socket.getLocalPort();
|
||||
socket.close();
|
||||
|
||||
DeploymentOptions options = new DeploymentOptions().setConfig(new JsonObject().put("http.port", port));
|
||||
|
||||
vertx.deployVerticle(RestServiceVerticle.class.getName(), options, 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(port, "localhost", "/api/baeldung/articles/article/12345", response -> {
|
||||
response.handler(responseBody -> {
|
||||
testContext.assertTrue(responseBody.toString()
|
||||
.contains("\"id\" : \"12345\""));
|
||||
async.complete();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.baeldung;
|
||||
|
||||
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;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(VertxUnitRunner.class)
|
||||
public class SimpleServerVerticleUnitTest {
|
||||
private Vertx vertx;
|
||||
|
||||
private int port = 8081;
|
||||
|
||||
@Before
|
||||
public void setup(TestContext testContext) throws IOException {
|
||||
vertx = Vertx.vertx();
|
||||
|
||||
// Pick an available and random
|
||||
ServerSocket socket = new ServerSocket(0);
|
||||
port = socket.getLocalPort();
|
||||
socket.close();
|
||||
|
||||
DeploymentOptions options = new DeploymentOptions().setConfig(new JsonObject().put("http.port", port));
|
||||
|
||||
vertx.deployVerticle(SimpleServerVerticle.class.getName(), options, 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(port, "localhost", "/", response -> response.handler(responseBody -> {
|
||||
testContext.assertTrue(responseBody.toString()
|
||||
.contains("Welcome"));
|
||||
async.complete();
|
||||
}));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user