future) throws Exception {
+ if (!future.isSuccess()) {
+ result.tryFailure(future.cause());
+ return;
+ }
+
+ if (future.getNow() != null) {
+ result.trySuccess(future.getNow());
+ } else {
+ if (counter.decrementAndGet() == 0) {
+ result.trySuccess(null);
+ return;
+ }
+ poll(name, codec, result, ref, names, counter, command);
+ }
+ }
+ });
+ } else {
+ ref.set(names.iterator());
+ poll(name, codec, result, ref, names, counter, command);
+ }
+ }
+
}
diff --git a/redisson/src/main/java/org/redisson/config/Config.java b/redisson/src/main/java/org/redisson/config/Config.java
index 77c038f48..81627fbf4 100644
--- a/redisson/src/main/java/org/redisson/config/Config.java
+++ b/redisson/src/main/java/org/redisson/config/Config.java
@@ -66,11 +66,6 @@ public class Config {
*/
private Codec codec;
- /**
- * For codec registry and look up. DefaultCodecProvider used by default
- */
- private ReferenceCodecProvider referenceCodecProvider = new DefaultReferenceCodecProvider();
-
private ExecutorService executor;
/**
@@ -115,7 +110,6 @@ public class Config {
setNettyThreads(oldConf.getNettyThreads());
setThreads(oldConf.getThreads());
setCodec(oldConf.getCodec());
- setReferenceCodecProvider(oldConf.getReferenceCodecProvider());
setReferenceEnabled(oldConf.isReferenceEnabled());
setEventLoopGroup(oldConf.getEventLoopGroup());
setTransportMode(oldConf.getTransportMode());
@@ -159,28 +153,6 @@ public class Config {
return codec;
}
- /**
- * Reference objects codec provider used for codec registry and look up.
- * org.redisson.codec.DefaultReferenceCodecProvider
used by default.
- *
- * @param codecProvider object
- * @return config
- * @see org.redisson.codec.ReferenceCodecProvider
- */
- public Config setReferenceCodecProvider(ReferenceCodecProvider codecProvider) {
- this.referenceCodecProvider = codecProvider;
- return this;
- }
-
- /**
- * Returns the CodecProvider instance
- *
- * @return CodecProvider
- */
- public ReferenceCodecProvider getReferenceCodecProvider() {
- return referenceCodecProvider;
- }
-
/**
* Config option indicate whether Redisson Reference feature is enabled.
*
diff --git a/redisson/src/main/java/org/redisson/liveobject/core/AccessorInterceptor.java b/redisson/src/main/java/org/redisson/liveobject/core/AccessorInterceptor.java
index c83f29f81..1cbf585bb 100644
--- a/redisson/src/main/java/org/redisson/liveobject/core/AccessorInterceptor.java
+++ b/redisson/src/main/java/org/redisson/liveobject/core/AccessorInterceptor.java
@@ -32,7 +32,6 @@ import org.redisson.client.codec.Codec;
import org.redisson.liveobject.misc.ClassUtils;
import org.redisson.liveobject.misc.Introspectior;
import org.redisson.liveobject.resolver.NamingScheme;
-import org.redisson.misc.RedissonObjectFactory;
import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.FieldValue;
@@ -76,7 +75,7 @@ public class AccessorInterceptor {
if (isGetter(method, fieldName)) {
Object result = liveMap.get(fieldName);
if (result == null) {
- RObject ar = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), fieldType, fieldName);
+ RObject ar = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), fieldType, fieldName, redisson);
if (ar != null) {
objectBuilder.store(ar, fieldName, liveMap);
return ar;
@@ -90,7 +89,7 @@ public class AccessorInterceptor {
return result;
}
if (result instanceof RedissonReference) {
- return RedissonObjectFactory.fromReference(redisson, (RedissonReference) result);
+ return objectBuilder.fromReference(redisson, (RedissonReference) result);
}
return result;
}
@@ -105,9 +104,10 @@ public class AccessorInterceptor {
Class extends Object> rEntity = liveObject.getClass().getSuperclass();
REntity anno = ClassUtils.getAnnotation(rEntity, REntity.class);
+ Codec codec = objectBuilder.getReferenceCodecProvider().getCodec(anno, rEntity, redisson.getConfig());
NamingScheme ns = anno.namingScheme()
.getDeclaredConstructor(Codec.class)
- .newInstance(redisson.getConfig().getReferenceCodecProvider().getCodec(anno, (Class) rEntity, redisson.getConfig()));
+ .newInstance(codec);
liveMap.fastPut(fieldName, new RedissonReference(rEntity,
ns.getName(rEntity, fieldType, getREntityIdFieldName(liveObject),
liveObject.getLiveObjectId())));
@@ -119,7 +119,7 @@ public class AccessorInterceptor {
&& TransformationMode.ANNOTATION_BASED
.equals(ClassUtils.getAnnotation(me.getClass().getSuperclass(),
REntity.class).fieldTransformation())) {
- RObject rObject = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), arg.getClass(), fieldName);
+ RObject rObject = objectBuilder.createObject(((RLiveObject) me).getLiveObjectId(), me.getClass().getSuperclass(), arg.getClass(), fieldName, redisson);
if (arg != null) {
if (rObject instanceof Collection) {
Collection> c = (Collection>) rObject;
diff --git a/redisson/src/main/java/org/redisson/liveobject/core/LiveObjectInterceptor.java b/redisson/src/main/java/org/redisson/liveobject/core/LiveObjectInterceptor.java
index a03fa57dd..9d6c19db0 100644
--- a/redisson/src/main/java/org/redisson/liveobject/core/LiveObjectInterceptor.java
+++ b/redisson/src/main/java/org/redisson/liveobject/core/LiveObjectInterceptor.java
@@ -57,9 +57,9 @@ public class LiveObjectInterceptor {
private final NamingScheme namingScheme;
private Codec codec;
- public LiveObjectInterceptor(RedissonClient redisson, Class> entityClass, String idFieldName) {
+ public LiveObjectInterceptor(RedissonClient redisson, Class> entityClass, String idFieldName, RedissonObjectBuilder objectBuilder) {
this.redisson = redisson;
- this.codecProvider = redisson.getConfig().getReferenceCodecProvider();
+ this.codecProvider = objectBuilder.getReferenceCodecProvider();
this.originalClass = entityClass;
this.idFieldName = idFieldName;
diff --git a/redisson/src/main/java/org/redisson/liveobject/core/RedissonObjectBuilder.java b/redisson/src/main/java/org/redisson/liveobject/core/RedissonObjectBuilder.java
index 412f1c683..530be0776 100644
--- a/redisson/src/main/java/org/redisson/liveobject/core/RedissonObjectBuilder.java
+++ b/redisson/src/main/java/org/redisson/liveobject/core/RedissonObjectBuilder.java
@@ -16,7 +16,11 @@
package org.redisson.liveobject.core;
import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Arrays;
import java.util.Deque;
+import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -32,21 +36,30 @@ import org.redisson.RedissonBlockingDeque;
import org.redisson.RedissonBlockingQueue;
import org.redisson.RedissonDeque;
import org.redisson.RedissonList;
+import org.redisson.RedissonLiveObjectService;
import org.redisson.RedissonMap;
import org.redisson.RedissonQueue;
import org.redisson.RedissonReference;
import org.redisson.RedissonSet;
import org.redisson.RedissonSortedSet;
+import org.redisson.api.RLiveObject;
import org.redisson.api.RMap;
import org.redisson.api.RObject;
+import org.redisson.api.RObjectReactive;
+import org.redisson.api.RObjectRx;
import org.redisson.api.RedissonClient;
+import org.redisson.api.RedissonReactiveClient;
+import org.redisson.api.RedissonRxClient;
import org.redisson.api.annotation.REntity;
+import org.redisson.api.annotation.RId;
import org.redisson.api.annotation.RObjectField;
import org.redisson.client.codec.Codec;
+import org.redisson.codec.DefaultReferenceCodecProvider;
import org.redisson.codec.ReferenceCodecProvider;
+import org.redisson.config.Config;
import org.redisson.liveobject.misc.ClassUtils;
+import org.redisson.liveobject.misc.Introspectior;
import org.redisson.liveobject.resolver.NamingScheme;
-import org.redisson.misc.RedissonObjectFactory;
import io.netty.util.internal.PlatformDependent;
@@ -73,15 +86,37 @@ public class RedissonObjectBuilder {
supportedClassMapping.put(List.class, RedissonList.class);
}
- private final RedissonClient redisson;
- private final ReferenceCodecProvider codecProvider;
+ private final Config config;
- public RedissonObjectBuilder(RedissonClient redisson) {
+ public static class CodecMethodRef {
+
+ Method defaultCodecMethod;
+ Method customCodecMethod;
+
+ Method get(boolean value) {
+ if (value) {
+ return defaultCodecMethod;
+ }
+ return customCodecMethod;
+ }
+ }
+
+ private static final Map, CodecMethodRef> references = new HashMap, CodecMethodRef>();
+
+ private final ReferenceCodecProvider codecProvider = new DefaultReferenceCodecProvider();
+
+ public RedissonObjectBuilder(Config config) {
super();
- this.redisson = redisson;
- this.codecProvider = redisson.getConfig().getReferenceCodecProvider();
+ this.config = config;
+ fillCodecMethods(references, RedissonClient.class, RObject.class);
+ fillCodecMethods(references, RedissonReactiveClient.class, RObjectReactive.class);
+ fillCodecMethods(references, RedissonRxClient.class, RObjectRx.class);
}
+ public ReferenceCodecProvider getReferenceCodecProvider() {
+ return codecProvider;
+ }
+
public void store(RObject ar, String fieldName, RMap liveMap) {
Codec codec = ar.getCodec();
if (codec != null) {
@@ -91,23 +126,15 @@ public class RedissonObjectBuilder {
new RedissonReference(ar.getClass(), ar.getName(), codec));
}
- public RObject createObject(Object id, Class> clazz, Class> fieldType, String fieldName) {
+ public RObject createObject(Object id, Class> clazz, Class> fieldType, String fieldName, RedissonClient redisson) {
Class extends RObject> mappedClass = getMappedClass(fieldType);
try {
if (mappedClass != null) {
Codec fieldCodec = getFieldCodec(clazz, mappedClass, fieldName);
NamingScheme fieldNamingScheme = getFieldNamingScheme(clazz, fieldName, fieldCodec);
+ String referenceName = fieldNamingScheme.getFieldReferenceName(clazz, id, mappedClass, fieldName, null);
- RObject obj = RedissonObjectFactory
- .createRObject(redisson,
- mappedClass,
- fieldNamingScheme.getFieldReferenceName(clazz,
- id,
- mappedClass,
- fieldName,
- null),
- fieldCodec);
- return obj;
+ return createRObject(redisson, mappedClass, referenceName, fieldCodec);
}
} catch (Exception e) {
throw new IllegalArgumentException(e);
@@ -122,10 +149,10 @@ public class RedissonObjectBuilder {
Field field = ClassUtils.getDeclaredField(rEntity, fieldName);
if (field.isAnnotationPresent(RObjectField.class)) {
RObjectField anno = field.getAnnotation(RObjectField.class);
- return codecProvider.getCodec(anno, rEntity, rObjectClass, fieldName, redisson.getConfig());
+ return codecProvider.getCodec(anno, rEntity, rObjectClass, fieldName, config);
} else {
REntity anno = ClassUtils.getAnnotation(rEntity, REntity.class);
- return codecProvider.getCodec(anno, (Class>) rEntity, redisson.getConfig());
+ return codecProvider.getCodec(anno, (Class>) rEntity, config);
}
}
@@ -151,4 +178,138 @@ public class RedissonObjectBuilder {
return null;
}
+ private void fillCodecMethods(Map, CodecMethodRef> b, Class> clientClazz, Class> objectClazz) {
+ for (Method method : clientClazz.getDeclaredMethods()) {
+ if (!method.getReturnType().equals(Void.TYPE)
+ && objectClazz.isAssignableFrom(method.getReturnType())
+ && method.getName().startsWith("get")) {
+ Class> cls = method.getReturnType();
+ if (!b.containsKey(cls)) {
+ b.put(cls, new CodecMethodRef());
+ }
+ CodecMethodRef builder = b.get(cls);
+ if (method.getParameterTypes().length == 2 //first param is name, second param is codec.
+ && Codec.class.isAssignableFrom(method.getParameterTypes()[1])) {
+ builder.customCodecMethod = method;
+ } else if (method.getParameterTypes().length == 1) {
+ builder.defaultCodecMethod = method;
+ }
+ }
+ }
+ }
+
+ public Object fromReference(RedissonClient redisson, RedissonReference rr) throws Exception {
+ Class extends Object> type = rr.getType();
+ if (type != null) {
+ if (ClassUtils.isAnnotationPresent(type, REntity.class)) {
+ RedissonLiveObjectService liveObjectService = (RedissonLiveObjectService) redisson.getLiveObjectService();
+ REntity anno = ClassUtils.getAnnotation(type, REntity.class);
+ NamingScheme ns = anno.namingScheme()
+ .getDeclaredConstructor(Codec.class)
+ .newInstance(codecProvider.getCodec(anno, type, redisson.getConfig()));
+ Object id = ns.resolveId(rr.getKeyName());
+ return liveObjectService.createLiveObject(type, id);
+ }
+ }
+
+ return getObject(redisson, rr, type, codecProvider);
+ }
+
+ private Object getObject(Object redisson, RedissonReference rr, Class extends Object> type,
+ ReferenceCodecProvider codecProvider)
+ throws IllegalAccessException, InvocationTargetException, Exception, ClassNotFoundException {
+ if (type != null) {
+ CodecMethodRef b = references.get(type);
+ if (b == null && type.getInterfaces().length > 0) {
+ type = type.getInterfaces()[0];
+ }
+ b = references.get(type);
+ if (b != null) {
+ Method builder = b.get(isDefaultCodec(rr));
+ return (isDefaultCodec(rr)
+ ? builder.invoke(redisson, rr.getKeyName())
+ : builder.invoke(redisson, rr.getKeyName(), codecProvider.getCodec(rr.getCodecType())));
+ }
+ }
+ throw new ClassNotFoundException("No RObject is found to match class type of " + rr.getTypeName() + " with codec type of " + rr.getCodecName());
+ }
+
+ private boolean isDefaultCodec(RedissonReference rr) {
+ return rr.getCodec() == null;
+ }
+
+ public Object fromReference(RedissonRxClient redisson, RedissonReference rr) throws Exception {
+ Class extends Object> type = rr.getReactiveType();
+ /**
+ * Live Object from reference in reactive client is not supported yet.
+ */
+ return getObject(redisson, rr, type, codecProvider);
+ }
+
+ public Object fromReference(RedissonReactiveClient redisson, RedissonReference rr) throws Exception {
+ Class extends Object> type = rr.getReactiveType();
+ /**
+ * Live Object from reference in reactive client is not supported yet.
+ */
+ return getObject(redisson, rr, type, codecProvider);
+ }
+
+ public RedissonReference toReference(Object object) {
+ if (object != null && ClassUtils.isAnnotationPresent(object.getClass(), REntity.class)) {
+ throw new IllegalArgumentException("REntity should be attached to Redisson before save");
+ }
+
+ if (object instanceof RObject && !(object instanceof RLiveObject)) {
+ Class> clazz = object.getClass().getInterfaces()[0];
+
+ RObject rObject = ((RObject) object);
+ if (rObject.getCodec() != null) {
+ codecProvider.registerCodec((Class) rObject.getCodec().getClass(), rObject.getCodec());
+ }
+ return new RedissonReference(clazz, rObject.getName(), rObject.getCodec());
+ }
+ if (object instanceof RObjectReactive && !(object instanceof RLiveObject)) {
+ Class> clazz = object.getClass().getInterfaces()[0];
+
+ RObjectReactive rObject = ((RObjectReactive) object);
+ if (rObject.getCodec() != null) {
+ codecProvider.registerCodec((Class) rObject.getCodec().getClass(), rObject.getCodec());
+ }
+ return new RedissonReference(clazz, rObject.getName(), rObject.getCodec());
+ }
+
+ try {
+ if (object instanceof RLiveObject) {
+ Class extends Object> rEntity = object.getClass().getSuperclass();
+ REntity anno = ClassUtils.getAnnotation(rEntity, REntity.class);
+ NamingScheme ns = anno.namingScheme()
+ .getDeclaredConstructor(Codec.class)
+ .newInstance(codecProvider.getCodec(anno, (Class) rEntity, config));
+ String name = Introspectior
+ .getFieldsWithAnnotation(rEntity, RId.class)
+ .getOnly().getName();
+ Class> type = ClassUtils.getDeclaredField(rEntity, name).getType();
+
+ return new RedissonReference(rEntity,
+ ns.getName(rEntity, type, name, ((RLiveObject) object).getLiveObjectId()));
+ }
+ } catch (Exception e) {
+ throw new IllegalArgumentException(e);
+ }
+ return null;
+ }
+
+ public T createRObject(RedissonClient redisson, Class expectedType, String name, K codec) throws Exception {
+ List> interfaces = Arrays.asList(expectedType.getInterfaces());
+ for (Class> iType : interfaces) {
+ if (references.containsKey(iType)) {// user cache to speed up things a little.
+ Method builder = references.get(iType).get(codec != null);
+ return (T) (codec != null
+ ? builder.invoke(redisson, name)
+ : builder.invoke(redisson, name, codec));
+ }
+ }
+ throw new ClassNotFoundException("No RObject is found to match class type of " + (expectedType != null ? expectedType.getName() : "null") + " with codec type of " + (codec != null ? codec.getClass().getName() : "null"));
+ }
+
}
diff --git a/redisson/src/main/java/org/redisson/misc/RedissonObjectFactory.java b/redisson/src/main/java/org/redisson/misc/RedissonObjectFactory.java
deleted file mode 100644
index 15406edad..000000000
--- a/redisson/src/main/java/org/redisson/misc/RedissonObjectFactory.java
+++ /dev/null
@@ -1,224 +0,0 @@
-/**
- * Copyright 2018 Nikita Koksharov
- *
- * 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.misc;
-
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.redisson.RedissonLiveObjectService;
-import org.redisson.RedissonReference;
-import org.redisson.api.RLiveObject;
-import org.redisson.api.RObject;
-import org.redisson.api.RObjectReactive;
-import org.redisson.api.RedissonClient;
-import org.redisson.api.RedissonReactiveClient;
-import org.redisson.api.RedissonRxClient;
-import org.redisson.api.annotation.REntity;
-import org.redisson.api.annotation.RId;
-import org.redisson.client.codec.Codec;
-import org.redisson.codec.ReferenceCodecProvider;
-import org.redisson.config.Config;
-import org.redisson.liveobject.misc.ClassUtils;
-import org.redisson.liveobject.misc.Introspectior;
-import org.redisson.liveobject.resolver.NamingScheme;
-
-/**
- *
- * @author Rui Gu (https://github.com/jackygurui)
- */
-public class RedissonObjectFactory {
-
- public static class RedissonObjectBuilder {
-
- Method defaultCodecMethod;
- Method customCodecMethod;
-
- Method get(boolean value) {
- if (value) {
- return defaultCodecMethod;
- }
- return customCodecMethod;
- }
- }
-
- private static final Map, RedissonObjectBuilder> builders;
-
- static {
- HashMap, RedissonObjectBuilder> b = new HashMap, RedissonObjectBuilder>();
- for (Method method : RedissonClient.class.getDeclaredMethods()) {
- if (!method.getReturnType().equals(Void.TYPE)
- && RObject.class.isAssignableFrom(method.getReturnType())
- && method.getName().startsWith("get")) {
- Class> cls = method.getReturnType();
- if (!b.containsKey(cls)) {
- b.put(cls, new RedissonObjectBuilder());
- }
- RedissonObjectBuilder builder = b.get(cls);
- if (method.getParameterTypes().length == 2 //first param is name, second param is codec.
- && Codec.class.isAssignableFrom(method.getParameterTypes()[1])) {
- builder.customCodecMethod = method;
- } else if (method.getParameterTypes().length == 1) {
- builder.defaultCodecMethod = method;
- }
- }
- }
-
- for (Method method : RedissonReactiveClient.class.getDeclaredMethods()) {
- if (!method.getReturnType().equals(Void.TYPE)
- && RObjectReactive.class.isAssignableFrom(method.getReturnType())
- && method.getName().startsWith("get")) {
- Class> cls = method.getReturnType();
- if (!b.containsKey(cls)) {
- b.put(cls, new RedissonObjectBuilder());
- }
- RedissonObjectBuilder builder = b.get(cls);
- if (method.getParameterTypes().length == 2 //first param is name, second param is codec.
- && Codec.class.isAssignableFrom(method.getParameterTypes()[1])) {
- builder.customCodecMethod = method;
- } else if (method.getParameterTypes().length == 1) {
- builder.defaultCodecMethod = method;
- }
- }
- }
-
- builders = Collections.unmodifiableMap(b);
-
- }
-
- public static Object fromReference(RedissonClient redisson, RedissonReference rr) throws Exception {
- Class extends Object> type = rr.getType();
- ReferenceCodecProvider codecProvider = redisson.getConfig().getReferenceCodecProvider();
- if (type != null) {
- if (ClassUtils.isAnnotationPresent(type, REntity.class)) {
- RedissonLiveObjectService liveObjectService = (RedissonLiveObjectService) redisson.getLiveObjectService();
- REntity anno = ClassUtils.getAnnotation(type, REntity.class);
- NamingScheme ns = anno.namingScheme()
- .getDeclaredConstructor(Codec.class)
- .newInstance(codecProvider.getCodec(anno, type, redisson.getConfig()));
- Object id = ns.resolveId(rr.getKeyName());
- return liveObjectService.createLiveObject(type, id);
- }
- }
-
- return getObject(redisson, rr, type, codecProvider);
- }
-
- protected static Object getObject(Object redisson, RedissonReference rr, Class extends Object> type,
- ReferenceCodecProvider codecProvider)
- throws IllegalAccessException, InvocationTargetException, Exception, ClassNotFoundException {
- if (type != null) {
- RedissonObjectBuilder b = builders.get(type);
- if (b == null && type.getInterfaces().length > 0) {
- type = type.getInterfaces()[0];
- }
- b = builders.get(type);
- if (b != null) {
- Method builder = b.get(isDefaultCodec(rr));
- return (isDefaultCodec(rr)
- ? builder.invoke(redisson, rr.getKeyName())
- : builder.invoke(redisson, rr.getKeyName(), codecProvider.getCodec(rr.getCodecType())));
- }
- }
- throw new ClassNotFoundException("No RObject is found to match class type of " + rr.getTypeName() + " with codec type of " + rr.getCodecName());
- }
-
- private static boolean isDefaultCodec(RedissonReference rr) {
- return rr.getCodec() == null;
- }
-
- public static Object fromReference(RedissonRxClient redisson, RedissonReference rr) throws Exception {
- Class extends Object> type = rr.getReactiveType();
- ReferenceCodecProvider codecProvider = redisson.getConfig().getReferenceCodecProvider();
- /**
- * Live Object from reference in reactive client is not supported yet.
- */
- return getObject(redisson, rr, type, codecProvider);
- }
-
- public static Object fromReference(RedissonReactiveClient redisson, RedissonReference rr) throws Exception {
- Class extends Object> type = rr.getReactiveType();
- ReferenceCodecProvider codecProvider = redisson.getConfig().getReferenceCodecProvider();
- /**
- * Live Object from reference in reactive client is not supported yet.
- */
- return getObject(redisson, rr, type, codecProvider);
- }
-
- public static RedissonReference toReference(Config config, Object object) {
- if (object != null && ClassUtils.isAnnotationPresent(object.getClass(), REntity.class)) {
- throw new IllegalArgumentException("REntity should be attached to Redisson before save");
- }
-
- if (object instanceof RObject && !(object instanceof RLiveObject)) {
- Class> clazz = object.getClass().getInterfaces()[0];
-
- RObject rObject = ((RObject) object);
- if (rObject.getCodec() != null) {
- config.getReferenceCodecProvider().registerCodec((Class) rObject.getCodec().getClass(), rObject.getCodec());
- }
- return new RedissonReference(clazz, rObject.getName(), rObject.getCodec());
- }
- if (object instanceof RObjectReactive && !(object instanceof RLiveObject)) {
- Class> clazz = object.getClass().getInterfaces()[0];
-
- RObjectReactive rObject = ((RObjectReactive) object);
- if (rObject.getCodec() != null) {
- config.getReferenceCodecProvider().registerCodec((Class) rObject.getCodec().getClass(), rObject.getCodec());
- }
- return new RedissonReference(clazz, rObject.getName(), rObject.getCodec());
- }
-
- try {
- if (object instanceof RLiveObject) {
- Class extends Object> rEntity = object.getClass().getSuperclass();
- REntity anno = ClassUtils.getAnnotation(rEntity, REntity.class);
- NamingScheme ns = anno.namingScheme()
- .getDeclaredConstructor(Codec.class)
- .newInstance(config.getReferenceCodecProvider().getCodec(anno, (Class) rEntity, config));
- String name = Introspectior
- .getFieldsWithAnnotation(rEntity, RId.class)
- .getOnly().getName();
- Class> type = ClassUtils.getDeclaredField(rEntity, name).getType();
-
- return new RedissonReference(rEntity,
- ns.getName(rEntity, type, name, ((RLiveObject) object).getLiveObjectId()));
- }
- } catch (Exception e) {
- throw new IllegalArgumentException(e);
- }
- return null;
- }
-
- public static T createRObject(RedissonClient redisson, Class expectedType, String name, K codec) throws Exception {
- List> interfaces = Arrays.asList(expectedType.getInterfaces());
- for (Class> iType : interfaces) {
- if (builders.containsKey(iType)) {// user cache to speed up things a little.
- Method builder = builders.get(iType).get(codec != null);
- return (T) (codec != null
- ? builder.invoke(redisson, name)
- : builder.invoke(redisson, name, codec));
- }
- }
- throw new ClassNotFoundException("No RObject is found to match class type of " + (expectedType != null ? expectedType.getName() : "null") + " with codec type of " + (codec != null ? codec.getClass().getName() : "null"));
- }
-
- public static void warmUp() {}
-}
diff --git a/redisson/src/main/java/org/redisson/misc/URIBuilder.java b/redisson/src/main/java/org/redisson/misc/URIBuilder.java
index d08c43b4c..6165dcd1b 100644
--- a/redisson/src/main/java/org/redisson/misc/URIBuilder.java
+++ b/redisson/src/main/java/org/redisson/misc/URIBuilder.java
@@ -70,10 +70,17 @@ public class URIBuilder {
throw new IOException(e);
}
}
-
+
+ private static String trimIpv6Brackets(String host) {
+ if (host.startsWith("[") && host.endsWith("]")) {
+ return host.substring(1, host.length() - 1);
+ }
+ return host;
+ }
+
public static boolean compare(InetSocketAddress entryAddr, URI addr) {
- if (((entryAddr.getHostName() != null && entryAddr.getHostName().equals(addr.getHost()))
- || entryAddr.getAddress().getHostAddress().equals(addr.getHost()))
+ if (((entryAddr.getHostName() != null && entryAddr.getHostName().equals(trimIpv6Brackets(addr.getHost())))
+ || entryAddr.getAddress().getHostAddress().equals(trimIpv6Brackets(addr.getHost())))
&& entryAddr.getPort() == addr.getPort()) {
return true;
}
diff --git a/redisson/src/test/java/org/redisson/RedissonBlockingQueueTest.java b/redisson/src/test/java/org/redisson/RedissonBlockingQueueTest.java
index 9247be434..7178c6750 100644
--- a/redisson/src/test/java/org/redisson/RedissonBlockingQueueTest.java
+++ b/redisson/src/test/java/org/redisson/RedissonBlockingQueueTest.java
@@ -17,6 +17,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Assert;
import org.junit.Test;
+import org.redisson.ClusterRunner.ClusterProcesses;
import org.redisson.RedisRunner.RedisProcess;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RFuture;
@@ -314,6 +315,52 @@ public class RedissonBlockingQueueTest extends RedissonQueueTest {
redisson.shutdown();
}
+ @Test
+ public void testPollFromAnyInCluster() throws Exception {
+ RedisRunner master1 = new RedisRunner().port(6890).randomDir().nosave();
+ RedisRunner master2 = new RedisRunner().port(6891).randomDir().nosave();
+ RedisRunner master3 = new RedisRunner().port(6892).randomDir().nosave();
+ RedisRunner slave1 = new RedisRunner().port(6900).randomDir().nosave();
+ RedisRunner slave2 = new RedisRunner().port(6901).randomDir().nosave();
+ RedisRunner slave3 = new RedisRunner().port(6902).randomDir().nosave();
+
+ ClusterRunner clusterRunner = new ClusterRunner()
+ .addNode(master1, slave1)
+ .addNode(master2, slave2)
+ .addNode(master3, slave3);
+ ClusterProcesses process = clusterRunner.run();
+
+ Thread.sleep(5000);
+
+ Config config = new Config();
+ config.useClusterServers()
+ .setLoadBalancer(new RandomLoadBalancer())
+ .addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
+ RedissonClient redisson = Redisson.create(config);
+
+ final RBlockingQueue queue1 = redisson.getBlockingQueue("queue:pollany");
+ Executors.newSingleThreadScheduledExecutor().schedule(() -> {
+ RBlockingQueue queue2 = redisson.getBlockingQueue("queue:pollany1");
+ RBlockingQueue queue3 = redisson.getBlockingQueue("queue:pollany2");
+ try {
+ queue3.put(2);
+ queue1.put(1);
+ queue2.put(3);
+ } catch (InterruptedException e) {
+ Assert.fail();
+ }
+ }, 3, TimeUnit.SECONDS);
+
+ long s = System.currentTimeMillis();
+ int l = queue1.pollFromAny(4, TimeUnit.SECONDS, "queue:pollany1", "queue:pollany2");
+
+ Assert.assertEquals(2, l);
+ Assert.assertTrue(System.currentTimeMillis() - s > 2000);
+
+ redisson.shutdown();
+ process.shutdown();
+ }
+
@Test
public void testPollFromAny() throws InterruptedException {
final RBlockingQueue queue1 = redisson.getBlockingQueue("queue:pollany");
diff --git a/redisson/src/test/java/org/redisson/RedissonGeoTest.java b/redisson/src/test/java/org/redisson/RedissonGeoTest.java
index 69ab4fedf..fa2b2d5e2 100644
--- a/redisson/src/test/java/org/redisson/RedissonGeoTest.java
+++ b/redisson/src/test/java/org/redisson/RedissonGeoTest.java
@@ -74,8 +74,46 @@ public class RedissonGeoTest extends BaseTest {
assertThat(geo.hash("Palermo", "Catania")).isEmpty();
}
+
+ @Test
+ public void testPos4() {
+ RGeo geo = redisson.getGeo("test");
+ geo.add(new GeoEntry(13.361389, 38.115556, "Palermo"), new GeoEntry(15.087269, 37.502669, "Catania"));
+
+ Map expected = new LinkedHashMap();
+ expected.put("Palermo", new GeoPosition(13.361389338970184, 38.115556395496299));
+ expected.put("Catania", new GeoPosition(15.087267458438873, 37.50266842333162));
+ assertThat(geo.pos("Palermo", "Catania")).isEqualTo(expected);
+ }
+ @Test
+ public void testPos1() {
+ RGeo geo = redisson.getGeo("test");
+ geo.add(0.123,0.893,"hi");
+ Map res = geo.pos("hi");
+ assertThat(res.get("hi").getLatitude()).isNotNull();
+ assertThat(res.get("hi").getLongitude()).isNotNull();
+ }
+ @Test
+ public void testPos3() {
+ RGeo geo = redisson.getGeo("test");
+ geo.add(0.123,0.893,"hi");
+ Map res = geo.pos("hi", "123f", "sdfdsf");
+ assertThat(res.get("hi").getLatitude()).isNotNull();
+ assertThat(res.get("hi").getLongitude()).isNotNull();
+ }
+
+ @Test
+ public void testPos2() {
+ RGeo geo = redisson.getGeo("test");
+ geo.add(new GeoEntry(13.361389, 38.115556, "Palermo"));
+
+ Map expected = new LinkedHashMap();
+ expected.put("Palermo", new GeoPosition(13.361389338970184, 38.115556395496299));
+ assertThat(geo.pos("test2", "Palermo", "test3", "Catania", "test1")).isEqualTo(expected);
+ }
+
@Test
public void testPos() {
RGeo geo = redisson.getGeo("test");
diff --git a/redisson/src/test/java/org/redisson/spring/support/SpringNamespaceWikiTest.java b/redisson/src/test/java/org/redisson/spring/support/SpringNamespaceWikiTest.java
index 2734ebd7b..48faf6563 100644
--- a/redisson/src/test/java/org/redisson/spring/support/SpringNamespaceWikiTest.java
+++ b/redisson/src/test/java/org/redisson/spring/support/SpringNamespaceWikiTest.java
@@ -19,7 +19,6 @@ import org.redisson.client.RedisClient;
import org.redisson.client.RedisConnection;
import org.redisson.client.codec.Codec;
import org.redisson.client.protocol.RedisCommands;
-import org.redisson.codec.ReferenceCodecProvider;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.context.ConfigurableApplicationContext;
@@ -122,7 +121,6 @@ public class SpringNamespaceWikiTest {
assertEquals(2, config.getNettyThreads());
assertSame(context.getBean("myCodec", Codec.class), config.getCodec());
assertEquals(false, config.isReferenceEnabled());
- assertSame(context.getBean("myCodecProvider", ReferenceCodecProvider.class), config.getReferenceCodecProvider());
assertSame(context.getBean("myExecutor", Executor.class), config.getExecutor());
assertSame(context.getBean("myEventLoopGroup", EventLoopGroup.class), config.getEventLoopGroup());
Method method = Config.class.getDeclaredMethod("getSingleServerConfig", (Class>[]) null);