|
18 | 18 | */ |
19 | 19 | public class DaemonTopicRunner { |
20 | 20 |
|
21 | | - private static final Logger log = LoggerFactory.getLogger(DaemonTopicRunner.class); |
22 | | - |
23 | | - private static final long reconnectWaitMillis = 1000; |
24 | | - |
25 | | - private final JedisPool jedisPool; |
26 | | - |
27 | | - private final DaemonTopic daemonTopic; |
28 | | - |
29 | | - public DaemonTopicRunner(JedisPool jedisPool, DaemonTopic daemonTopic) { |
30 | | - this.jedisPool = jedisPool; |
31 | | - this.daemonTopic = daemonTopic; |
32 | | - } |
33 | | - |
34 | | - public void run() { |
35 | | - Thread t = new Thread(() -> attemptConnections(), "redis-sub"); |
36 | | - t.start(); |
37 | | - } |
38 | | - |
39 | | - private void attemptConnections() { |
40 | | - |
41 | | - Timer reloadTimer = new Timer("redis-sub-notify"); |
42 | | - ReloadNotifyTask notifyTask = null; |
43 | | - int attempts = 1; |
44 | | - |
45 | | - while (true) { |
46 | | - if (notifyTask != null) { |
47 | | - // we didn't successfully re-connect to redis |
48 | | - notifyTask.cancel(); |
49 | | - } |
50 | | - notifyTask = new ReloadNotifyTask(); |
51 | | - reloadTimer.schedule(notifyTask, reconnectWaitMillis + 500); |
52 | | - attempts++; |
53 | | - try { |
54 | | - subscribe(); |
55 | | - } catch (JedisException e) { |
56 | | - log.debug("... redis subscribe connection attempt:{} failed:{}", attempts, e.getMessage()); |
57 | | - try { |
58 | | - // wait a little before retrying |
59 | | - Thread.sleep(reconnectWaitMillis); |
60 | | - } catch (InterruptedException e1) { |
61 | | - log.warn("Interrupted redis re-connection wait", e1); |
62 | | - } |
63 | | - } |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - /** |
68 | | - * Subscribe and block when successful. |
69 | | - */ |
70 | | - private void subscribe() { |
71 | | - Jedis jedis = jedisPool.getResource(); |
72 | | - jedis.echo("hi"); |
73 | | - try { |
74 | | - daemonTopic.subscribe(jedis); |
75 | | - |
76 | | - } catch (Exception e) { |
77 | | - log.error("Lost connection to topic, starting re-connection loop", e); |
78 | | - attemptConnections(); |
79 | | - |
80 | | - } finally { |
81 | | - try { |
82 | | - jedis.close(); |
83 | | - } catch (Exception e) { |
84 | | - log.warn("Error closing probably broken Redis connection", e); |
85 | | - } |
86 | | - } |
87 | | - } |
88 | | - |
89 | | - private class ReloadNotifyTask extends TimerTask { |
90 | | - @Override |
91 | | - public void run() { |
92 | | - daemonTopic.notifyConnected(); |
93 | | - } |
94 | | - } |
| 21 | + private static final Logger log = LoggerFactory.getLogger(DaemonTopicRunner.class); |
| 22 | + |
| 23 | + private static final long reconnectWaitMillis = 1000; |
| 24 | + |
| 25 | + private final JedisPool jedisPool; |
| 26 | + |
| 27 | + private final DaemonTopic daemonTopic; |
| 28 | + |
| 29 | + public DaemonTopicRunner(JedisPool jedisPool, DaemonTopic daemonTopic) { |
| 30 | + this.jedisPool = jedisPool; |
| 31 | + this.daemonTopic = daemonTopic; |
| 32 | + } |
| 33 | + |
| 34 | + public void run() { |
| 35 | + Thread t = new Thread(() -> attemptConnections(), "redis-sub"); |
| 36 | + t.start(); |
| 37 | + } |
| 38 | + |
| 39 | + private void attemptConnections() { |
| 40 | + |
| 41 | + Timer reloadTimer = new Timer("redis-sub-notify"); |
| 42 | + ReloadNotifyTask notifyTask = null; |
| 43 | + int attempts = 1; |
| 44 | + |
| 45 | + while (true) { |
| 46 | + if (notifyTask != null) { |
| 47 | + // we didn't successfully re-connect to redis |
| 48 | + notifyTask.cancel(); |
| 49 | + } |
| 50 | + notifyTask = new ReloadNotifyTask(); |
| 51 | + reloadTimer.schedule(notifyTask, reconnectWaitMillis + 500); |
| 52 | + attempts++; |
| 53 | + try { |
| 54 | + subscribe(); |
| 55 | + } catch (JedisException e) { |
| 56 | + log.debug("... redis subscribe connection attempt:{} failed:{}", attempts, e.getMessage()); |
| 57 | + try { |
| 58 | + // wait a little before retrying |
| 59 | + Thread.sleep(reconnectWaitMillis); |
| 60 | + } catch (InterruptedException e1) { |
| 61 | + log.warn("Interrupted redis re-connection wait", e1); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Subscribe and block when successful. |
| 69 | + */ |
| 70 | + private void subscribe() { |
| 71 | + Jedis jedis = jedisPool.getResource(); |
| 72 | + jedis.echo("hi"); |
| 73 | + try { |
| 74 | + daemonTopic.subscribe(jedis); |
| 75 | + |
| 76 | + } catch (Exception e) { |
| 77 | + log.error("Lost connection to topic, starting re-connection loop", e); |
| 78 | + attemptConnections(); |
| 79 | + |
| 80 | + } finally { |
| 81 | + try { |
| 82 | + jedis.close(); |
| 83 | + } catch (Exception e) { |
| 84 | + log.warn("Error closing probably broken Redis connection", e); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private class ReloadNotifyTask extends TimerTask { |
| 90 | + @Override |
| 91 | + public void run() { |
| 92 | + daemonTopic.notifyConnected(); |
| 93 | + } |
| 94 | + } |
95 | 95 | } |
0 commit comments