|
1 | 1 | package redis.clients.jedis;
|
2 | 2 |
|
3 |
| -import static redis.clients.jedis.Protocol.ResponseKeyword.*; |
4 |
| - |
5 |
| -import java.util.Arrays; |
6 |
| -import java.util.List; |
7 |
| - |
8 |
| -import redis.clients.jedis.Protocol.Command; |
9 |
| -import redis.clients.jedis.exceptions.JedisConnectionException; |
10 |
| -import redis.clients.jedis.exceptions.JedisException; |
11 | 3 | import redis.clients.jedis.util.SafeEncoder;
|
12 | 4 |
|
13 |
| -public abstract class JedisPubSub { |
14 |
| - |
15 |
| - private static final String JEDIS_SUBSCRIPTION_MESSAGE = "JedisPubSub is not subscribed to a Jedis instance."; |
16 |
| - private int subscribedChannels = 0; |
17 |
| - private volatile Connection client; |
18 |
| - |
19 |
| - public void onMessage(String channel, String message) { |
20 |
| - } |
21 |
| - |
22 |
| - public void onPMessage(String pattern, String channel, String message) { |
23 |
| - } |
24 |
| - |
25 |
| - public void onSubscribe(String channel, int subscribedChannels) { |
26 |
| - } |
27 |
| - |
28 |
| - public void onUnsubscribe(String channel, int subscribedChannels) { |
29 |
| - } |
30 |
| - |
31 |
| - public void onPUnsubscribe(String pattern, int subscribedChannels) { |
32 |
| - } |
33 |
| - |
34 |
| - public void onPSubscribe(String pattern, int subscribedChannels) { |
35 |
| - } |
36 |
| - |
37 |
| - public void onPong(String pattern) { |
38 |
| - } |
39 |
| - |
40 |
| - public void unsubscribe() { |
41 |
| - if (client == null) { |
42 |
| - throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
43 |
| - } |
44 |
| - client.sendCommand(Command.UNSUBSCRIBE); |
45 |
| - client.flush(); |
46 |
| - } |
47 |
| - |
48 |
| - public void unsubscribe(String... channels) { |
49 |
| - if (client == null) { |
50 |
| - throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
51 |
| - } |
52 |
| - client.sendCommand(Command.UNSUBSCRIBE, channels); |
53 |
| - client.flush(); |
54 |
| - } |
55 |
| - |
56 |
| - public void subscribe(String... channels) { |
57 |
| - if (client == null) { |
58 |
| - throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
59 |
| - } |
60 |
| - client.sendCommand(Command.SUBSCRIBE, channels); |
61 |
| - client.flush(); |
62 |
| - } |
63 |
| - |
64 |
| - public void psubscribe(String... patterns) { |
65 |
| - if (client == null) { |
66 |
| - throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
67 |
| - } |
68 |
| - client.sendCommand(Command.PSUBSCRIBE, patterns); |
69 |
| - client.flush(); |
70 |
| - } |
71 |
| - |
72 |
| - public void punsubscribe() { |
73 |
| - if (client == null) { |
74 |
| - throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
75 |
| - } |
76 |
| - client.sendCommand(Command.PUNSUBSCRIBE); |
77 |
| - client.flush(); |
78 |
| - } |
79 |
| - |
80 |
| - public void punsubscribe(String... patterns) { |
81 |
| - if (client == null) { |
82 |
| - throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
83 |
| - } |
84 |
| - client.sendCommand(Command.PUNSUBSCRIBE, patterns); |
85 |
| - client.flush(); |
86 |
| - } |
87 |
| - |
88 |
| - public void ping() { |
89 |
| - if (client == null) { |
90 |
| - throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
91 |
| - } |
92 |
| - client.sendCommand(Command.PING); |
93 |
| - client.flush(); |
94 |
| - } |
95 |
| - |
96 |
| - public void ping(String argument) { |
97 |
| - if (client == null) { |
98 |
| - throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
99 |
| - } |
100 |
| - client.sendCommand(Command.PING, argument); |
101 |
| - client.flush(); |
102 |
| - } |
103 |
| - |
104 |
| - public boolean isSubscribed() { |
105 |
| - return subscribedChannels > 0; |
106 |
| - } |
107 |
| - |
108 |
| - public void proceedWithPatterns(Connection client, String... patterns) { |
109 |
| - this.client = client; |
110 |
| - this.client.setTimeoutInfinite(); |
111 |
| - try { |
112 |
| - psubscribe(patterns); |
113 |
| - process(); |
114 |
| - } finally { |
115 |
| - this.client.rollbackTimeout(); |
116 |
| - } |
117 |
| - } |
118 |
| - |
119 |
| - public void proceed(Connection client, String... channels) { |
120 |
| - this.client = client; |
121 |
| - this.client.setTimeoutInfinite(); |
122 |
| - try { |
123 |
| - subscribe(channels); |
124 |
| - process(); |
125 |
| - } finally { |
126 |
| - this.client.rollbackTimeout(); |
127 |
| - } |
128 |
| - } |
129 |
| - |
130 |
| -// private void process(Client client) { |
131 |
| - private void process() { |
132 |
| - |
133 |
| - do { |
134 |
| - Object reply = client.getUnflushedObject(); |
135 |
| - |
136 |
| - if (reply instanceof List) { |
137 |
| - List<Object> listReply = (List<Object>) reply; |
138 |
| - final Object firstObj = listReply.get(0); |
139 |
| - if (!(firstObj instanceof byte[])) { |
140 |
| - throw new JedisException("Unknown message type: " + firstObj); |
141 |
| - } |
142 |
| - final byte[] resp = (byte[]) firstObj; |
143 |
| - if (Arrays.equals(SUBSCRIBE.getRaw(), resp)) { |
144 |
| - subscribedChannels = ((Long) listReply.get(2)).intValue(); |
145 |
| - final byte[] bchannel = (byte[]) listReply.get(1); |
146 |
| - final String strchannel = (bchannel == null) ? null : SafeEncoder.encode(bchannel); |
147 |
| - onSubscribe(strchannel, subscribedChannels); |
148 |
| - } else if (Arrays.equals(UNSUBSCRIBE.getRaw(), resp)) { |
149 |
| - subscribedChannels = ((Long) listReply.get(2)).intValue(); |
150 |
| - final byte[] bchannel = (byte[]) listReply.get(1); |
151 |
| - final String strchannel = (bchannel == null) ? null : SafeEncoder.encode(bchannel); |
152 |
| - onUnsubscribe(strchannel, subscribedChannels); |
153 |
| - } else if (Arrays.equals(MESSAGE.getRaw(), resp)) { |
154 |
| - final byte[] bchannel = (byte[]) listReply.get(1); |
155 |
| - final byte[] bmesg = (byte[]) listReply.get(2); |
156 |
| - final String strchannel = (bchannel == null) ? null : SafeEncoder.encode(bchannel); |
157 |
| - final String strmesg = (bmesg == null) ? null : SafeEncoder.encode(bmesg); |
158 |
| - onMessage(strchannel, strmesg); |
159 |
| - } else if (Arrays.equals(PMESSAGE.getRaw(), resp)) { |
160 |
| - final byte[] bpattern = (byte[]) listReply.get(1); |
161 |
| - final byte[] bchannel = (byte[]) listReply.get(2); |
162 |
| - final byte[] bmesg = (byte[]) listReply.get(3); |
163 |
| - final String strpattern = (bpattern == null) ? null : SafeEncoder.encode(bpattern); |
164 |
| - final String strchannel = (bchannel == null) ? null : SafeEncoder.encode(bchannel); |
165 |
| - final String strmesg = (bmesg == null) ? null : SafeEncoder.encode(bmesg); |
166 |
| - onPMessage(strpattern, strchannel, strmesg); |
167 |
| - } else if (Arrays.equals(PSUBSCRIBE.getRaw(), resp)) { |
168 |
| - subscribedChannels = ((Long) listReply.get(2)).intValue(); |
169 |
| - final byte[] bpattern = (byte[]) listReply.get(1); |
170 |
| - final String strpattern = (bpattern == null) ? null : SafeEncoder.encode(bpattern); |
171 |
| - onPSubscribe(strpattern, subscribedChannels); |
172 |
| - } else if (Arrays.equals(PUNSUBSCRIBE.getRaw(), resp)) { |
173 |
| - subscribedChannels = ((Long) listReply.get(2)).intValue(); |
174 |
| - final byte[] bpattern = (byte[]) listReply.get(1); |
175 |
| - final String strpattern = (bpattern == null) ? null : SafeEncoder.encode(bpattern); |
176 |
| - onPUnsubscribe(strpattern, subscribedChannels); |
177 |
| - } else if (Arrays.equals(PONG.getRaw(), resp)) { |
178 |
| - final byte[] bpattern = (byte[]) listReply.get(1); |
179 |
| - final String strpattern = (bpattern == null) ? null : SafeEncoder.encode(bpattern); |
180 |
| - onPong(strpattern); |
181 |
| - } else { |
182 |
| - throw new JedisException("Unknown message type: " + firstObj); |
183 |
| - } |
184 |
| - } else if (reply instanceof byte[]) { |
185 |
| - byte[] resp = (byte[]) reply; |
186 |
| - String str = SafeEncoder.encode(resp); |
187 |
| - if ("PONG".equalsIgnoreCase(str)) { |
188 |
| - onPong(null); |
189 |
| - } else { |
190 |
| - onPong(str); |
191 |
| - } |
192 |
| - } else { |
193 |
| - throw new JedisException("Unknown message type: " + reply); |
194 |
| - } |
195 |
| - } while (isSubscribed()); |
196 |
| - |
197 |
| -// /* Invalidate instance since this thread is no longer listening */ |
198 |
| -// this.client = null; |
199 |
| - } |
| 5 | +public abstract class JedisPubSub extends JedisPubSubBase<String> { |
200 | 6 |
|
201 |
| - public int getSubscribedChannels() { |
202 |
| - return subscribedChannels; |
| 7 | + @Override |
| 8 | + protected final String encode(byte[] raw) { |
| 9 | + return SafeEncoder.encode(raw); |
203 | 10 | }
|
204 | 11 | }
|
0 commit comments