`
cywhoyi
  • 浏览: 412676 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Vertx与Spring配合完成DML操作

阅读更多

前言

vertx相较于Tomcat不同之处

引用oschina上关于vertx的文章,感觉他说得非常明白,不在这里过多讨论,这里我简单说明下如何在vertx和spring配合完成基础任务。

 

服务启动:

  public static void main( String[] args ) {
    ApplicationContext context = new AnnotationConfigApplicationContext(SimpleSpringConfiguration.class);
    final Vertx vertx = Vertx.vertx();
    vertx.deployVerticle(new SpringSimpleVerticle(context));
    vertx.deployVerticle(new ServerVerticle());
  }

 

 EventBus接受给Service:

public class SpringSimpleVerticle extends AbstractVerticle {

    public static final String ALL_PRODUCTS_ADDRESS = "example.all.products";


    private final ObjectMapper mapper = new ObjectMapper();
    private final ProductService service;

    public SpringSimpleVerticle(final ApplicationContext context) {

        service = (ProductService) context.getBean("productService");

    }

    private Handler<Message<String>> allProductsHandler(ProductService service, String name) {
        return ms2g -> vertx.<String>executeBlocking(future -> {
                    try {
                        future.complete(mapper.writeValueAsString(service.getAllProducts(ms2g.body())));
                    } catch (JsonProcessingException e) {
                        System.out.println("Failed to serialize result");
                        future.fail(e);
                    }
                },
                result -> {
                    if (result.succeeded()) {
                        ms2g.reply(result.result());
                    } else {
                        ms2g.reply(result.cause().toString());
                    }
                });
    }

    @Override
    public void start() throws Exception {
        super.start();

        System.out.println("<<<<<<<<<<<<<<<<<<<<<<< CONSUMER >>>>>>>>>>>>>>>>>>>>>>>>>");
        vertx.eventBus().<String>consumer(ALL_PRODUCTS_ADDRESS).handler(allProductsHandler(service, "message"));

    }
}

 传统Service+Dao

@Service
public class ProductService {

    @Autowired
    private ProductRepository repo;

    public List<Product> getAllProducts(String productId) {
        System.out.println("productid : " + productId);
        return repo.findAll();
    }

    public void getProduct(String productId) {
        System.out.println("productid : " + productId);
    }

}

 

端口监听:

public class ServerVerticle extends AbstractVerticle {

  @Override
  public void start() throws Exception {
    super.start();
    HttpServer server = vertx.createHttpServer();
    server.requestHandler(req -> {
      if (req.method() == HttpMethod.GET) {
        req.response().setChunked(true);

        if (req.path().equals("/products")) {
          vertx.eventBus().<String>send(SpringSimpleVerticle.ALL_PRODUCTS_ADDRESS, "123456", result -> {
            if (result.succeeded()) {
              req.response().setStatusCode(200).write(result.result().body()).end();
            } else {
              req.response().setStatusCode(500).write(result.cause().toString()).end();
            }
          });
        } else {
          req.response().setStatusCode(200).write("Hello from vert.x").end();
        }

      } else {
        // We only support GET for now
        req.response().setStatusCode(405).end();
      }
    });

    server.listen(8080);
  }
}

 

结论:

DML操作基本都是类似的做法,但是观念的改变其实很大,传统烟囱的方式=》基于消息、事件的方式转移

5
4
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics