mirror of
https://github.com/proxiodev/RedisBungee.git
synced 2026-06-24 03:46:43 +00:00
Migrate from the consumer model to ExecutorService. The consumer was not flexible and was less reliable.
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Copyright © 2013 tuxed <write@imaginarycode.com>
|
||||
* This work is free. You can redistribute it and/or modify it under the
|
||||
* terms of the Do What The Fuck You Want To Public License, Version 2,
|
||||
* as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
|
||||
*/
|
||||
package com.imaginarycode.minecraft.redisbungee.util;
|
||||
|
||||
import com.imaginarycode.minecraft.redisbungee.RedisBungee;
|
||||
import lombok.AllArgsConstructor;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@AllArgsConstructor
|
||||
public abstract class RedisCallable<T> implements Callable<T>, Runnable {
|
||||
private final RedisBungee plugin;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
run(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T call() {
|
||||
return run(false);
|
||||
}
|
||||
|
||||
private T run(boolean retry) {
|
||||
Jedis jedis = null;
|
||||
|
||||
try {
|
||||
jedis = plugin.getPool().getResource();
|
||||
return call(jedis);
|
||||
} catch (JedisConnectionException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Unable to get connection", e);
|
||||
|
||||
if (jedis != null)
|
||||
plugin.getPool().returnBrokenResource(jedis);
|
||||
|
||||
if (!retry) {
|
||||
// Wait one second before retrying the task
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e1) {
|
||||
throw new RuntimeException("task failed to run", e1);
|
||||
}
|
||||
run(true);
|
||||
}
|
||||
} finally {
|
||||
if (jedis != null) {
|
||||
plugin.getPool().returnResource(jedis);
|
||||
}
|
||||
}
|
||||
|
||||
throw new RuntimeException("task failed to run");
|
||||
}
|
||||
|
||||
protected abstract T call(Jedis jedis);
|
||||
}
|
||||
Reference in New Issue
Block a user