Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2204

How to reconnect to redis using java with vertx?

$
0
0

The application I'm working on doesn't reconnect with the redis server if the server goes down.

When my application goes up and the redis server is still not running, my application tries to reconnect until the server runs and they connect. But when the two are connected and the redis server goes down, my application does not try to reconnect and starts giving an error[ httpStatus: 500message: "Redis connection is broken." ]

I created an endpoint that makes requests to redis as a test and I'm running the server on my machine and this is the test environment I'm using.

I tried to configure the connection using RedisOptions from the io.vertx.redis.client package but without success. Can you help me please!

code:

private final RedesOptions options = new RedesOptions()private Redis redisClient;private void createRedisClient(Handler<AsyncResult<Redis>> handler) {                Redis.createClient(vertx, options)                        .connect(onConnect -> {                            if (onConnect.succeeded()) {                                this.redisClient = onConnect.result();                                //make sure the client is reconnected o error                                this.redisClient.exceptionHandler(e -> {                                    // attempt to reconnect                                    attemptReconnect(0);                                });                            }                            // allow further processing                            handler.handle(onConnect);                        });            }  private void attemptReconnect(int retry) {    if (retry > MAX_RECONNECT_RETRIES) {        // we should stop now, as there's nothing we can do.    } else {        System.out.println("erro de conexao >>>> " );        // retry with backoff up to 10240 ms        long backoff = (long) (Math.pow(2, Math.min(retry, 10)) * 10);        vertx.setTimer(backoff, timer -> createRedisClient(onReconnect -> {            if (onReconnect.failed()) {                attemptReconnect(retry + 1);            }        }));    }}

and call this method createRedisClient in the constructor of the class.

code:

 createRedisClient(onCreate -> {        if (onCreate.succeeded()) {            System.out.println("conectado com sucesso >>> " + onCreate.result());            log.info("Redis Connected: "+onCreate.result());        } else if (onCreate.failed()) {            System.out.println("erro de conexao >>>> " + onCreate.result());            attemptReconnect(0);        }    });

Viewing all articles
Browse latest Browse all 2204

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>