sczyh30 / vertx-blueprint-microservice

Vert.x Blueprint Project - Micro-Shop microservice application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use the Original ServiceImpl inside the RestVerticle can result in a vertx context issue

a-marcel opened this issue · comments

Hi,

inside your verticles you deploy the rest api with the same instance, created in the verticle.

// create the service instance
ProductService productService = new ProductServiceImpl(vertx, config());
// register the service proxy on event bus
ProxyHelper.registerService(ProductService.class, vertx, productService, SERVICE_ADDRESS);
...
.compose(servicePublished -> deployRestService(productService))
...

As an example:

  • you could use a http client inside your ProductServiceImpl (and create this one time with the constructor)

  • inside ProductServiceImpl there is a method "test" wich one use this http client

  • and following changes in the ProductVerticle:

private Future<Void> bindEventBusListener(ProductService productService) {
  Future<Void> future = Future.future();
  vertx.eventBus().consumer("cqrs.channel", m -> {
    productService.test(); // <--- this is important
  }).completionHandler(future.completer());
  return future.map(r -> null);
}

you will run in this:

[vert.x-eventloop-thread-1] WARN io.vertx.core.http.impl.ConnectionManager - Reusing a connection with a different context: an HttpClient is probably shared between different Verticles

as a solution, you should give a proxy instance inside the Rest API and inside the eventbus consumer.

ProductServiceVertxEBProxy productServiceProxy = new ProductServiceVertxEBProxy(vertx, SERVICE_ADDRESS);

...
.compose(servicePublished -> deployRestService(productServiceProxy))
.compose(servicePublished -> bindEventBusListener(productServiceProxy))
...

Maybe you ask why a eventBus consumer inside you verticles is needed. In my project i use CQRS and communicate with events.

As an example:

i've a customer and a email messaging verticle in your style.

a customer calls register and we write the data into a database and return the customer maybe a access_token.
at the same time, we're using the eventbus to send a CUSTOMER_REGISTER event.

this CUSTOMER_REGISTER is subscribed inside the messaging verticle and will send a email with a confirmation code to this customer
after that, the messaging verticle send a CONFIRMATION_MAIL_SEND event (including the generated confirmation code)

this CONFIRMATION_MAIL_SEND is subscribed by the customer verticle and save this confirmation code inside the customer database (via this testMethod from above)

i hope, i could describe my topic

Thanks
Marcel

P.s: i forget one thing to mention. both verticles DON'T share any Java Classes or generated code (to keep them decoupled). They just send plain JsonObject's to a channel.