Permalink
Browse files

fix: 应对redis重启的可能性,这时候jedis会重连,但订阅topic也需要重连

  • Loading branch information...
1 parent 86a0d0c commit 56dc15aea618da3777a386053ec095cee1e8727b @wendal wendal committed Jan 10, 2017
@@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.List;
+import org.nutz.lang.Lang;
import org.nutz.lang.Streams;
import org.nutz.log.Log;
import org.nutz.log.Logs;
@@ -24,6 +25,21 @@ public void reg(final String pattern, PubSub pb) {
new Thread("jedis.pubsub." + pattern) {
public void run() {
jedisPool.getResource().psubscribe(proxy, pattern);
+ int count = 1;
+ while (!jedisPool.isClosed()) {
+ try {
+ log.debug("psubscribe " + pattern);
+ jedisPool.getResource().psubscribe(proxy, pattern);
+ }
+ catch (Exception e) {
+ if (jedisPool.isClosed())
+ break;
+ log.debug("psubscribe fail, retry after "+count+"seconds", e);
+ Lang.quiteSleep(count * 1000);
+ if (count < 15)
+ count ++;
+ }
+ }
}
}.start();
}
@@ -41,6 +57,11 @@ public void fire(String channel, String message) {
public void depose() {
for (PubSubProxy proxy : list)
- proxy.punsubscribe(proxy.pattern);
+ try {
+ proxy.punsubscribe(proxy.pattern);
+ }
+ catch (Exception e) {
+ log.debug("punsubscribe " + proxy.pattern, e);
+ }
}
}
@@ -9,12 +9,17 @@
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.util.Destroyable;
import org.apache.shiro.util.Initializable;
+import org.nutz.lang.Lang;
import org.nutz.lang.random.R;
+import org.nutz.log.Log;
+import org.nutz.log.Logs;
import redis.clients.jedis.JedisPool;
@SuppressWarnings({"unchecked", "rawtypes"})
public class LCacheManager implements CacheManager, Initializable, Destroyable, Runnable {
+
+ private static final Log log = Logs.get();
public static String PREFIX = "LCache:";
@@ -44,7 +49,22 @@ public void setupJedisPool(JedisPool pool) {
@Override
public void run() {
- pool.getResource().psubscribe(pubSub, PREFIX + "*");
+ int count = 1;
+ while (!pool.isClosed()) {
+ try {
+ log.debug("psubscribe " + PREFIX + "*");
+ pool.getResource().psubscribe(pubSub, PREFIX + "*");
+ }
+ catch (Exception e) {
+ if (pool.isClosed())
+ break;
+ log.debug("psubscribe fail, retry after "+count+"seconds", e);
+ Lang.quiteSleep(count * 1000);
+ if (count < 15)
+ count ++;
+ }
+ }
+
}
public void depose() {

0 comments on commit 56dc15a

Please sign in to comment.