Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice design on vertx. Eventbus or Singleton for repositories

I am using vertx3

I need to use redis in order to set and get values.(Redis might be changed to something else in the future)

Iam looking for best practice design for my implementation.

I am working on vertx cluster and I need to retrieve a messages via the eventbus extract the message and insert into Redis.

In the other hand I can get requests via web and also extract the messages and insert them into redis

Two options:

  1. Should I have a "redis-verticle" that get the messages via the bus and write them.

  2. Should I create a "Listener verticle" that will hold DAO which will hold RedisRepo object which will write them.

    I will also be able to handle the web calls and hold this DAO object

If I was on spring-app I would create a DAO which holds RedisRepo and Inject it into my service layer but here we got the eventbus so I am not sure.

(btw the redis datasource me be changed to something else so I gotta think about generic wrappers)

1. 

public class RedisRepoVerticle extends AbstractVerticle {

...
 public void start() {
 client = new RedisClient("localhost", 6379);
                 connection = client.connect();
...

vertx.eventBus().consumer("redis-operation", (handler) -> {
            {
                JsonObject msg = new JsonObject(handler.body().toString());
               //write straight to Redis 

            }
        });

}






2. 

     public class RedisMessageListener extends AbstractVerticle {
        DatasourceDAO datasource
        ...
         public void start() {
         client = new RedisClient("localhost", 6379);
                         connection = client.connect();
    ...

    vertx.eventBus().consumer("redis-operation", (handler) -> {
                {
                    JsonObject msg = new JsonObject(handler.body().toString());
                   datasourceDAO.writeToRedis(..); 

                }
            });

    }

//datasourceDAO will hold RedisRepo object

If I take the second option should I start maintain singletons? I dont want to duplicate my daos which will duplicate my redisrepo's classes

like image 545
rayman Avatar asked Sep 08 '15 15:09

rayman


1 Answers

I think it would be better to implement the second case, create a seperate verticle that will hold a redis client and a seperate verticle that will accept, process and pass the requests to the redis. Each verticle should have separate connection pool, and you might get in problems with maintaining multiple connection pools in the future when creating separate connections from each verticle to the redis.

The DAO-s replication should be not a case here. I would suggest you taking a look at https://github.com/vert-x3/vertx-service-proxy. It's a vert.x sub-project which can help you understand what a verticle itself is. You should tread a verticle as a service (or in particular, DAO) that provides a set of methods. It behaves like a singleton, that is you don't create new instances of a verticle on-demand, but there can be more than one instance of it depending on the configuration (http://vertx.io/docs/vertx-core/groovy/#_specifying_number_of_verticle_instances).

like image 127
meshuga Avatar answered Sep 24 '22 19:09

meshuga