BAEL-778 - renaming the vertx-spring to spring-vertx

This commit is contained in:
slavisa-baeldung
2017-06-10 11:53:39 +01:00
parent 5cb1c71cf0
commit cc37377821
13 changed files with 106 additions and 105 deletions
@@ -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);
}
}
@@ -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;
}
}
@@ -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> {
}
@@ -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;
}
}
@@ -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())));
}
}
@@ -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));
}
}
@@ -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());
}
});
}
}