Merge pull request #2035 from eugenp/BAEL-778

BAEL-778
This commit is contained in:
slavisa-baeldung 2017-06-11 12:19:00 +02:00 committed by GitHub
commit ed082d3660
12 changed files with 366 additions and 17 deletions

36
pom.xml
View File

@ -1,5 +1,6 @@
<?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">
<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>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
@ -11,19 +12,20 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<gib.referenceBranch>refs/heads/master</gib.referenceBranch>
<!-- <gib.enabled>false</gib.enabled>-->
<!-- <gib.enabled>false</gib.enabled>-->
<junit.version>4.12</junit.version>
<org.hamcrest.version>1.3</org.hamcrest.version>
<org.hamcrest.version>1.3</org.hamcrest.version>
<mockito.version>1.10.19</mockito.version>
<!-- logging -->
<org.slf4j.version>1.7.21</org.slf4j.version>
<logback.version>1.1.7</logback.version>
<!-- plugins -->
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
</properties>
<modules>
<module>spring-vertx</module>
<module>aws</module>
<module>akka-streams</module>
<module>algorithms</module>
@ -70,7 +72,7 @@
<module>immutables</module>
<module>jackson</module>
<!-- <module>java-cassandra</module> -->
<!-- <module>java-cassandra</module> -->
<module>vavr</module>
<module>javax-servlets</module>
<module>javaxval</module>
@ -122,8 +124,8 @@
<module>selenium-junit-testng</module>
<module>solr</module>
<module>spark-java</module>
<module>spring-5</module>
<module>spring-5-mvc</module>
<module>spring-5</module>
<module>spring-5-mvc</module>
<module>spring-akka</module>
<module>spring-amqp</module>
<module>spring-all</module>
@ -175,7 +177,7 @@
<module>spring-rest-angular</module>
<module>spring-rest-docs</module>
<module>spring-rest</module>
<module>spring-rest-simple</module>
<module>spring-rest-simple</module>
<module>spring-security-cache-control</module>
<module>spring-security-client/spring-security-jsp-authentication</module>
<module>spring-security-client/spring-security-jsp-authorize</module>
@ -242,12 +244,12 @@
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
</dependency>
<!-- test -->
<dependency>
@ -267,8 +269,8 @@
<artifactId>hamcrest-library</artifactId>
<version>${org.hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>${org.hamcrest.version}</version>
@ -280,7 +282,7 @@
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencies>
<build>
<plugins>
@ -295,7 +297,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<forkCount>3</forkCount>
<reuseForks>true</reuseForks>
@ -312,12 +314,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugin>
</plugins>
<extensions>
<!--<extension>

77
spring-vertx/pom.xml Normal file
View File

@ -0,0 +1,77 @@
<?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>
<groupId>com.baeldung</groupId>
<artifactId>vertx-spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>vertx-spring</name>
<description>A demo project with vertx spring integration</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<vertx.version>3.4.1</vertx.version>
</properties>
<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-core</artifactId>
<version>${vertx.version}</version>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -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.ServiceVerticle;
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
ServerVerticle serverVerticle;
@Autowired
ServiceVerticle 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);
}
}

View File

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

View File

@ -0,0 +1,11 @@
package com.baeldung.vertxspring.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.baeldung.vertxspring.entity.Article;
@Repository
public interface ArticleRepository extends CrudRepository<Article, Long> {
}

View File

@ -0,0 +1,25 @@
package com.baeldung.vertxspring.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baeldung.vertxspring.entity.Article;
import com.baeldung.vertxspring.repository.ArticleRepository;
@Service
public class ArticleService {
@Autowired
ArticleRepository articleRepository;
public List<Article> getAllArticle() {
List<Article> articles = new ArrayList<>();
articleRepository.findAll()
.forEach(articles::add);
return articles;
}
}

View File

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

View File

@ -0,0 +1,42 @@
package com.baeldung.vertxspring.verticles;
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 {
private void getAllArticlesHandler(RoutingContext routingContext) {
vertx.eventBus()
.<String>send(ServiceVerticle.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", 8080));
}
}

View File

@ -0,0 +1,48 @@
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 ServiceVerticle extends AbstractVerticle {
public static final String GET_ALL_ARTICLES = "get.artilces.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());
}
});
}
}

View File

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

View File

@ -0,0 +1,26 @@
package com.baeldung.vertxspring;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
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.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class VertxSpringApplicationTests {
private TestRestTemplate restTemplate = new TestRestTemplate();
@Test
public void givenUrl_whenReceivedArticles_thenSuccess() {
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://localhost:8080/api/baeldung/articles", String.class);
assertEquals(200, responseEntity.getStatusCodeValue());
}
}