-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Redisson.java
executable file
·313 lines (282 loc) · 9.15 KB
/
Redisson.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* Copyright 2014 Nikita Koksharov, Nickolay Borbit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.redisson;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.connection.ClusterConnectionManager;
import org.redisson.connection.ConnectionManager;
import org.redisson.connection.MasterSlaveConnectionManager;
import org.redisson.connection.SentinelConnectionManager;
import org.redisson.connection.SingleConnectionManager;
import org.redisson.core.RAtomicLong;
import org.redisson.core.RBatch;
import org.redisson.core.RBlockingQueue;
import org.redisson.core.RBucket;
import org.redisson.core.RCountDownLatch;
import org.redisson.core.RDeque;
import org.redisson.core.RHyperLogLog;
import org.redisson.core.RList;
import org.redisson.core.RLock;
import org.redisson.core.RMap;
import org.redisson.core.RQueue;
import org.redisson.core.RScript;
import org.redisson.core.RSet;
import org.redisson.core.RSortedSet;
import org.redisson.core.RTopic;
/**
* Main infrastructure class allows to get access
* to all Redisson objects on top of Redis server.
*
* @author Nikita Koksharov
*
*/
public class Redisson implements RedissonClient {
private final CommandExecutor commandExecutor;
private final ConnectionManager connectionManager;
private final Config config;
private final UUID id = UUID.randomUUID();
Redisson(Config config) {
this.config = config;
Config configCopy = new Config(config);
if (configCopy.getMasterSlaveServersConfig() != null) {
connectionManager = new MasterSlaveConnectionManager(configCopy.getMasterSlaveServersConfig(), configCopy);
} else if (configCopy.getSingleServerConfig() != null) {
connectionManager = new SingleConnectionManager(configCopy.getSingleServerConfig(), configCopy);
} else if (configCopy.getSentinelServersConfig() != null) {
connectionManager = new SentinelConnectionManager(configCopy.getSentinelServersConfig(), configCopy);
} else if (configCopy.getClusterServersConfig() != null) {
connectionManager = new ClusterConnectionManager(configCopy.getClusterServersConfig(), configCopy);
} else {
throw new IllegalArgumentException("server(s) address(es) not defined!");
}
commandExecutor = new CommandExecutorService(connectionManager);
}
/**
* Creates an Redisson instance
*
* @return Redisson instance
*/
public static Redisson create() {
Config config = new Config();
config.useSingleServer().setAddress("127.0.0.1:6379");
// config.useMasterSlaveConnection().setMasterAddress("127.0.0.1:6379").addSlaveAddress("127.0.0.1:6389").addSlaveAddress("127.0.0.1:6399");
// config.useSentinelConnection().setMasterName("mymaster").addSentinelAddress("127.0.0.1:26389", "127.0.0.1:26379");
// config.useClusterServers().addNodeAddress("127.0.0.1:7000");
return create(config);
}
/**
* Creates an Redisson instance with configuration
*
* @param config
* @return Redisson instance
*/
public static Redisson create(Config config) {
return new Redisson(config);
}
/**
* Returns object holder by name
*
* @param name of object
* @return
*/
@Override
public <V> RBucket<V> getBucket(String name) {
return new RedissonBucket<V>(commandExecutor, name);
}
/**
* Returns a list of object holder by a key pattern
*/
@Override
public <V> List<RBucket<V>> getBuckets(final String pattern) {
Collection<Object> keys = commandExecutor.get(commandExecutor.readAllAsync(RedisCommands.KEYS, pattern));
List<RBucket<V>> buckets = new ArrayList<RBucket<V>>(keys.size());
for (Object key : keys) {
if(key != null) {
buckets.add(this.<V>getBucket(key.toString()));
}
}
return buckets;
}
/**
* Returns HyperLogLog object
*
* @param name of object
* @return
*/
@Override
public <V> RHyperLogLog<V> getHyperLogLog(String name) {
return new RedissonHyperLogLog<V>(commandExecutor, name);
}
/**
* Returns distributed list instance by name.
*
* @param name of the distributed list
* @return distributed list
*/
@Override
public <V> RList<V> getList(String name) {
return new RedissonList<V>(commandExecutor, name);
}
/**
* Returns distributed map instance by name.
*
* @param name of the distributed map
* @return distributed map
*/
@Override
public <K, V> RMap<K, V> getMap(String name) {
return new RedissonMap<K, V>(commandExecutor, name);
}
/**
* Returns distributed lock instance by name.
*
* @param name of the distributed lock
* @return distributed lock
*/
@Override
public RLock getLock(String name) {
return new RedissonLock(commandExecutor, name, id);
}
/**
* Returns distributed set instance by name.
*
* @param name of the distributed set
* @return distributed set
*/
@Override
public <V> RSet<V> getSet(String name) {
return new RedissonSet<V>(commandExecutor, name);
}
/**
* Returns script with eval-operations support
*
* @return
*/
public RScript getScript() {
return new RedissonScript(commandExecutor);
}
/**
* Returns distributed sorted set instance by name.
*
* @param name of the distributed set
* @return distributed set
*/
@Override
public <V> RSortedSet<V> getSortedSet(String name) {
return new RedissonSortedSet<V>(commandExecutor, name);
}
/**
* Returns topic instance by name.
*
* @param name of the distributed topic
* @return distributed topic
*/
@Override
public <M> RTopic<M> getTopic(String name) {
return new RedissonTopic<M>(commandExecutor, name);
}
/**
* Returns topic instance satisfies by pattern name.
*
* Supported glob-style patterns:
* h?llo subscribes to hello, hallo and hxllo
* h*llo subscribes to hllo and heeeello
* h[ae]llo subscribes to hello and hallo, but not hillo
*
* @param pattern of the topic
* @return
*/
@Override
public <M> RTopic<M> getTopicPattern(String pattern) {
return new RedissonTopicPattern<M>(commandExecutor, pattern);
}
/**
* Returns distributed queue instance by name.
*
* @param name of the distributed queue
* @return distributed queue
*/
@Override
public <V> RQueue<V> getQueue(String name) {
return new RedissonQueue<V>(commandExecutor, name);
}
/**
* Returns distributed blocking queue instance by name.
*
* @param name of the distributed blocking queue
* @return distributed queue
*/
@Override
public <V> RBlockingQueue<V> getBlockingQueue(String name) {
return new RedissonBlockingQueue<V>(commandExecutor, name);
}
/**
* Returns distributed deque instance by name.
*
* @param name of the distributed queue
* @return distributed queue
*/
@Override
public <V> RDeque<V> getDeque(String name) {
return new RedissonDeque<V>(commandExecutor, name);
}
/**
* Returns distributed "atomic long" instance by name.
*
* @param name of the distributed "atomic long"
* @return distributed "atomic long"
*/
@Override
public RAtomicLong getAtomicLong(String name) {
return new RedissonAtomicLong(commandExecutor, name);
}
/**
* Returns distributed "count down latch" instance by name.
*
* @param name of the distributed "count down latch"
* @return distributed "count down latch"
*/
@Override
public RCountDownLatch getCountDownLatch(String name) {
return new RedissonCountDownLatch(commandExecutor, name, id);
}
/**
* Shuts down Redisson instance <b>NOT</b> Redis server
*/
public void shutdown() {
connectionManager.shutdown();
}
/**
* Allows to get configuration provided
* during Redisson instance creation. Further changes on
* this object not affect Redisson instance.
*
* @return Config object
*/
public Config getConfig() {
return config;
}
public void flushdb() {
commandExecutor.writeAllAsync(RedisCommands.FLUSHDB).awaitUninterruptibly();
}
@Override
public RBatch createBatch() {
return new RedissonBatch(connectionManager);
}
}