36
36
import java .util .Collection ;
37
37
import java .util .List ;
38
38
import java .util .concurrent .TimeUnit ;
39
- import java .util .concurrent .atomic .AtomicInteger ;
40
39
41
40
@ Repository
42
41
public class CuratorRepositoryImpl implements CuratorRepository {
@@ -109,9 +108,9 @@ public CuratorFrameworkOpImpl(CuratorFramework curatorFramework) {
109
108
}
110
109
111
110
@ Override
112
- public boolean checkExists (final String znode ) {
111
+ public boolean checkExists (final String node ) {
113
112
try {
114
- return null != curatorFramework .checkExists ().forPath (znode );
113
+ return null != curatorFramework .checkExists ().forPath (node );
115
114
// CHECKSTYLE:OFF
116
115
} catch (final Exception ex ) {
117
116
// CHECKSTYLE:ON
@@ -120,15 +119,15 @@ public boolean checkExists(final String znode) {
120
119
}
121
120
122
121
@ Override
123
- public String getData (final String znode ) {
122
+ public String getData (final String node ) {
124
123
try {
125
- if (checkExists (znode )) {
126
- byte [] getZnodeData = curatorFramework .getData ().forPath (znode );
127
- if (getZnodeData == null ) {// executor的分片可能存在全部飘走的情况,sharding节点有可能获取到的是null,需要对null做判断,否则new
124
+ if (checkExists (node )) {
125
+ byte [] nodeData = curatorFramework .getData ().forPath (node );
126
+ if (nodeData == null ) {// executor的分片可能存在全部飘走的情况,sharding节点有可能获取到的是null,需要对null做判断,否则new
128
127
// String时会报空指针异常
129
128
return null ;
130
129
}
131
- return new String (getZnodeData , Charset .forName ("UTF-8" ));
130
+ return new String (nodeData , Charset .forName ("UTF-8" ));
132
131
} else {
133
132
return null ;
134
133
}
@@ -142,9 +141,9 @@ public String getData(final String znode) {
142
141
}
143
142
144
143
@ Override
145
- public List <String > getChildren (final String znode ) {
144
+ public List <String > getChildren (final String node ) {
146
145
try {
147
- return curatorFramework .getChildren ().forPath (znode );
146
+ return curatorFramework .getChildren ().forPath (node );
148
147
// CHECKSTYLE:OFF
149
148
} catch (final NoNodeException ignore ) {
150
149
return null ;
@@ -156,15 +155,19 @@ public List<String> getChildren(final String znode) {
156
155
}
157
156
158
157
@ Override
159
- public void create (final String znode ) {
160
- create (znode , "" );
158
+ public void create (final String node ) {
159
+ create (node , "" );
161
160
}
162
161
163
162
@ Override
164
- public void create (final String znode , Object data ) {
163
+ public void create (final String node , Object value ) {
164
+ if (value == null ) {
165
+ log .info ("node value is null, won't create, node: {}" , node );
166
+ return ;
167
+ }
165
168
try {
166
169
curatorFramework .create ().creatingParentsIfNeeded ()
167
- .forPath (znode , data .toString ().getBytes (Charset .forName ("UTF-8" )));
170
+ .forPath (node , value .toString ().getBytes (Charset .forName ("UTF-8" )));
168
171
} catch (final NodeExistsException ignore ) {
169
172
// CHECKSTYLE:OFF
170
173
} catch (final Exception ex ) {
@@ -173,13 +176,18 @@ public void create(final String znode, Object data) {
173
176
}
174
177
}
175
178
176
- public void update (final String znode , final Object value ) {
179
+ @ Override
180
+ public void update (final String node , final Object value ) {
181
+ if (value == null ) {
182
+ log .info ("node value is null, won't update, node: {}" , node );
183
+ return ;
184
+ }
177
185
try {
178
- if (this .checkExists (znode )) {
179
- curatorFramework .inTransaction ().check ().forPath (znode ).and ().setData ()
180
- .forPath (znode , value .toString ().getBytes (Charset .forName ("UTF-8" ))).and ().commit ();
186
+ if (this .checkExists (node )) {
187
+ curatorFramework .inTransaction ().check ().forPath (node ).and ().setData ()
188
+ .forPath (node , value .toString ().getBytes (Charset .forName ("UTF-8" ))).and ().commit ();
181
189
} else {
182
- this .create (znode , value );
190
+ this .create (node , value );
183
191
}
184
192
} catch (final NoNodeException ignore ) {
185
193
// CHECKSTYLE:OFF
@@ -190,10 +198,10 @@ public void update(final String znode, final Object value) {
190
198
}
191
199
192
200
@ Override
193
- public void delete (final String znode ) {
201
+ public void delete (final String node ) {
194
202
try {
195
- if (null != curatorFramework .checkExists ().forPath (znode )) {
196
- curatorFramework .delete ().forPath (znode );
203
+ if (null != curatorFramework .checkExists ().forPath (node )) {
204
+ curatorFramework .delete ().forPath (node );
197
205
}
198
206
} catch (final NoNodeException ignore ) {
199
207
// CHECKSTYLE:OFF
@@ -204,10 +212,10 @@ public void delete(final String znode) {
204
212
}
205
213
206
214
@ Override
207
- public void deleteRecursive (final String znode ) {
215
+ public void deleteRecursive (final String node ) {
208
216
try {
209
- if (null != curatorFramework .checkExists ().forPath (znode )) {
210
- CuratorUtils .deletingChildrenIfNeeded (curatorFramework , znode );
217
+ if (null != curatorFramework .checkExists ().forPath (node )) {
218
+ CuratorUtils .deletingChildrenIfNeeded (curatorFramework , node );
211
219
}
212
220
} catch (final NoNodeException ignore ) {
213
221
// CHECKSTYLE:OFF
@@ -225,8 +233,8 @@ public void deleteRecursive(final String znode) {
225
233
*/
226
234
@ Override
227
235
public void fillJobNodeIfNotExist (final String node , final Object value ) {
228
- if (null == value ) {
229
- log .info ("job node value is null, node:{}" , node );
236
+ if (value == null ) {
237
+ log .info ("node value is null, won't fillJobNodeIfNotExist, node: {}" , node );
230
238
return ;
231
239
}
232
240
if (!checkExists (node )) {
@@ -311,22 +319,18 @@ public CuratorTransactionOpImpl(CuratorFramework curatorClient) {
311
319
}
312
320
}
313
321
314
- private boolean checkExists (String znode ) throws Exception {
315
- return curatorClient .checkExists ().forPath (znode ) != null ;
322
+ private boolean checkExists (String node ) throws Exception {
323
+ return curatorClient .checkExists ().forPath (node ) != null ;
316
324
}
317
325
318
- private CuratorTransactionOpImpl create (String znode , byte [] data ) throws Exception {
326
+ private CuratorTransactionOpImpl create (String node , byte [] data ) throws Exception {
319
327
curatorTransactionFinal = curatorTransactionFinal .create ().withMode (CreateMode .PERSISTENT )
320
- .forPath (znode , data ).and ();
328
+ .forPath (node , data ).and ();
321
329
return this ;
322
330
}
323
331
324
- private byte [] getData (String znode ) throws Exception {
325
- return curatorClient .getData ().forPath (znode );
326
- }
327
-
328
- private byte [] toData (Object value ) {
329
- return (value == null ? "" : value .toString ()).getBytes (Charset .forName ("UTF-8" ));
332
+ private byte [] getData (String node ) throws Exception {
333
+ return curatorClient .getData ().forPath (node );
330
334
}
331
335
332
336
private boolean bytesEquals (byte [] a , byte [] b ) {
@@ -345,44 +349,50 @@ private boolean bytesEquals(byte[] a, byte[] b) {
345
349
}
346
350
347
351
@ Override
348
- public CuratorTransactionOpImpl replace (String znode , Object value ) throws Exception {
349
- byte [] data = toData (value );
350
- curatorTransactionFinal = curatorTransactionFinal .setData ().forPath (znode , data ).and ();
352
+ public CuratorTransactionOpImpl replace (String node , Object value ) throws Exception {
353
+ if (value == null ) {
354
+ log .info ("node value is null, won't replace, node: {}" , node );
355
+ return this ;
356
+ }
357
+ byte [] data = value .toString ().getBytes (Charset .forName ("UTF-8" ));
358
+ curatorTransactionFinal = curatorTransactionFinal .setData ().forPath (node , data ).and ();
351
359
return this ;
352
360
}
353
361
354
- public CuratorTransactionOpImpl replaceIfChanged ( String znode , Object value ) throws Exception {
355
- return replaceIfChanged (znode , value , new AtomicInteger ( 0 ));
356
- }
357
-
358
- public CuratorTransactionOpImpl replaceIfChanged ( String znode , Object value , AtomicInteger changedCount )
359
- throws Exception {
360
- byte [] newData = toData ( value );
361
- if (this .checkExists (znode )) {
362
- byte [] oldData = this .getData (znode );
362
+ @ Override
363
+ public CuratorTransactionOpImpl replaceIfChanged (String node , Object value ) throws Exception {
364
+ if ( value == null ) {
365
+ log . info ( "node value is null, won't replaceIfChanged, node: {}" , node );
366
+ return this ;
367
+ }
368
+ byte [] newData = value . toString (). getBytes ( Charset . forName ( "UTF-8" ) );
369
+ if (this .checkExists (node )) {
370
+ byte [] oldData = this .getData (node );
363
371
if (!bytesEquals (newData , oldData )) {
364
- curatorTransactionFinal = curatorTransactionFinal .check ().forPath (znode ).and ().setData ()
365
- .forPath (znode , newData ).and ();
366
- changedCount .incrementAndGet ();
372
+ curatorTransactionFinal = curatorTransactionFinal .check ().forPath (node ).and ().setData ()
373
+ .forPath (node , newData ).and ();
367
374
}
368
375
} else {
369
- this .create (znode , newData );
370
- changedCount .incrementAndGet ();
376
+ this .create (node , newData );
371
377
}
372
378
return this ;
373
379
}
374
380
375
381
@ Override
376
- public CuratorTransactionOp create (String znode , Object value ) throws Exception {
377
- byte [] data = toData (value );
382
+ public CuratorTransactionOp create (String node , Object value ) throws Exception {
383
+ if (value == null ) {
384
+ log .info ("node value is null, won't create, node: {}" , node );
385
+ return this ;
386
+ }
387
+ byte [] data = value .toString ().getBytes (Charset .forName ("UTF-8" ));
378
388
curatorTransactionFinal = curatorTransactionFinal .create ().withMode (CreateMode .PERSISTENT )
379
- .forPath (znode , data ).and ();
389
+ .forPath (node , data ).and ();
380
390
return this ;
381
391
}
382
392
383
393
@ Override
384
- public CuratorTransactionOp delete (String znode ) throws Exception {
385
- curatorTransactionFinal = curatorTransactionFinal .delete ().forPath (znode ).and ();
394
+ public CuratorTransactionOp delete (String node ) throws Exception {
395
+ curatorTransactionFinal = curatorTransactionFinal .delete ().forPath (node ).and ();
386
396
return this ;
387
397
}
388
398
0 commit comments