[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:
+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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user