RedissonRemoteService is now able to support method overload

tests and solutions added
pull/766/head
Rui Gu 8 years ago
parent 18f37017b5
commit b402bad025

@ -20,7 +20,9 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.redisson.api.RBlockingQueue; import org.redisson.api.RBlockingQueue;
@ -184,7 +186,7 @@ public abstract class BaseRemoteService {
final RBlockingQueue<RemoteServiceRequest> requestQueue = redisson.getBlockingQueue(requestQueueName, final RBlockingQueue<RemoteServiceRequest> requestQueue = redisson.getBlockingQueue(requestQueueName,
getCodec()); getCodec());
final RemoteServiceRequest request = new RemoteServiceRequest(requestId, method.getName(), args, final RemoteServiceRequest request = new RemoteServiceRequest(requestId, method.getName(), getMethodSignatures(method), args,
optionsCopy, System.currentTimeMillis()); optionsCopy, System.currentTimeMillis());
final RemotePromise<Object> result = new RemotePromise<Object>(commandExecutor.getConnectionManager().newPromise()) { final RemotePromise<Object> result = new RemotePromise<Object>(commandExecutor.getConnectionManager().newPromise()) {
@ -399,7 +401,7 @@ public abstract class BaseRemoteService {
String requestQueueName = getRequestQueueName(remoteInterface); String requestQueueName = getRequestQueueName(remoteInterface);
RBlockingQueue<RemoteServiceRequest> requestQueue = redisson.getBlockingQueue(requestQueueName, RBlockingQueue<RemoteServiceRequest> requestQueue = redisson.getBlockingQueue(requestQueueName,
getCodec()); getCodec());
RemoteServiceRequest request = new RemoteServiceRequest(requestId, method.getName(), args, optionsCopy, RemoteServiceRequest request = new RemoteServiceRequest(requestId, method.getName(), getMethodSignatures(method), args, optionsCopy,
System.currentTimeMillis()); System.currentTimeMillis());
requestQueue.add(request); requestQueue.add(request);
@ -537,4 +539,11 @@ public abstract class BaseRemoteService {
} }
} }
protected List<String> getMethodSignatures(Method method) {
List<String> list = new ArrayList();
for (Class<?> t : method.getParameterTypes()) {
list.add(t.getName());
}
return list;
}
} }

@ -93,7 +93,7 @@ public class RedissonRemoteService extends BaseRemoteService implements RRemoteS
} }
for (Method method : remoteInterface.getMethods()) { for (Method method : remoteInterface.getMethods()) {
RemoteServiceMethod value = new RemoteServiceMethod(method, object); RemoteServiceMethod value = new RemoteServiceMethod(method, object);
RemoteServiceKey key = new RemoteServiceKey(remoteInterface, method.getName()); RemoteServiceKey key = new RemoteServiceKey(remoteInterface, method.getName(), getMethodSignatures(method));
if (beans.put(key, value) != null) { if (beans.put(key, value) != null) {
return; return;
} }
@ -183,7 +183,7 @@ public class RedissonRemoteService extends BaseRemoteService implements RRemoteS
private <T> void executeMethod(final Class<T> remoteInterface, final RBlockingQueue<RemoteServiceRequest> requestQueue, private <T> void executeMethod(final Class<T> remoteInterface, final RBlockingQueue<RemoteServiceRequest> requestQueue,
final ExecutorService executor, final RemoteServiceRequest request) { final ExecutorService executor, final RemoteServiceRequest request) {
final RemoteServiceMethod method = beans.get(new RemoteServiceKey(remoteInterface, request.getMethodName())); final RemoteServiceMethod method = beans.get(new RemoteServiceKey(remoteInterface, request.getMethodName(), request.getSignatures()));
final String responseName = getResponseQueueName(remoteInterface, request.getRequestId()); final String responseName = getResponseQueueName(remoteInterface, request.getRequestId());
RBlockingQueue<RemoteServiceCancelRequest> cancelRequestQueue = RBlockingQueue<RemoteServiceCancelRequest> cancelRequestQueue =

@ -15,6 +15,11 @@
*/ */
package org.redisson.remote; package org.redisson.remote;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/** /**
* *
* @author Nikita Koksharov * @author Nikita Koksharov
@ -24,17 +29,23 @@ public class RemoteServiceKey {
private final Class<?> serviceInterface; private final Class<?> serviceInterface;
private final String methodName; private final String methodName;
private final List<String> signatures;
public RemoteServiceKey(Class<?> serviceInterface, String methodName) { public RemoteServiceKey(Class<?> serviceInterface, String method, List<String> signatures) {
super(); super();
this.serviceInterface = serviceInterface; this.serviceInterface = serviceInterface;
this.methodName = methodName; this.methodName = method;
this.signatures = Collections.unmodifiableList(signatures);
} }
public String getMethodName() { public String getMethodName() {
return methodName; return methodName;
} }
public List<String> getSignatures() {
return signatures;
}
public Class<?> getServiceInterface() { public Class<?> getServiceInterface() {
return serviceInterface; return serviceInterface;
} }
@ -44,6 +55,7 @@ public class RemoteServiceKey {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + ((methodName == null) ? 0 : methodName.hashCode()); result = prime * result + ((methodName == null) ? 0 : methodName.hashCode());
result = prime * result + ((signatures == null) ? 0 : signatures.hashCode());
result = prime * result + ((serviceInterface == null) ? 0 : serviceInterface.getName().hashCode()); result = prime * result + ((serviceInterface == null) ? 0 : serviceInterface.getName().hashCode());
return result; return result;
} }
@ -60,9 +72,11 @@ public class RemoteServiceKey {
if (methodName == null) { if (methodName == null) {
if (other.methodName != null) if (other.methodName != null)
return false; return false;
} else if (!methodName.equals(other.methodName)) } else if (!methodName.equals(other.methodName)) {
return false;
} else if (!signatures.equals(other.signatures)) {
return false; return false;
if (serviceInterface == null) { } if (serviceInterface == null) {
if (other.serviceInterface != null) if (other.serviceInterface != null)
return false; return false;
} else if (!serviceInterface.equals(other.serviceInterface)) } else if (!serviceInterface.equals(other.serviceInterface))

@ -17,6 +17,7 @@ package org.redisson.remote;
import java.io.Serializable; import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import org.redisson.api.RemoteInvocationOptions; import org.redisson.api.RemoteInvocationOptions;
@ -31,6 +32,7 @@ public class RemoteServiceRequest implements Serializable {
private String requestId; private String requestId;
private String methodName; private String methodName;
private List<String> signatures;
private Object[] args; private Object[] args;
private RemoteInvocationOptions options; private RemoteInvocationOptions options;
private long date; private long date;
@ -43,10 +45,11 @@ public class RemoteServiceRequest implements Serializable {
this.requestId = requestId; this.requestId = requestId;
} }
public RemoteServiceRequest(String requestId, String methodName, Object[] args, RemoteInvocationOptions options, long date) { public RemoteServiceRequest(String requestId, String methodName, List<String> signatures, Object[] args, RemoteInvocationOptions options, long date) {
super(); super();
this.requestId = requestId; this.requestId = requestId;
this.methodName = methodName; this.methodName = methodName;
this.signatures = signatures;
this.args = args; this.args = args;
this.options = options; this.options = options;
this.date = date; this.date = date;
@ -64,6 +67,10 @@ public class RemoteServiceRequest implements Serializable {
return args; return args;
} }
public List<String> getSignatures() {
return signatures;
}
public RemoteInvocationOptions getOptions() { public RemoteInvocationOptions getOptions() {
return options; return options;
} }
@ -74,7 +81,8 @@ public class RemoteServiceRequest implements Serializable {
@Override @Override
public String toString() { public String toString() {
return "RemoteServiceRequest [requestId=" + requestId + ", methodName=" + methodName + ", args=" return "RemoteServiceRequest [requestId=" + requestId + ", methodName=" + methodName + ", signatures=["
+ Arrays.toString(signatures.toArray()) + "], args="
+ Arrays.toString(args) + ", options=" + options + ", date=" + date + "]"; + Arrays.toString(args) + ", options=" + options + ", date=" + date + "]";
} }

@ -12,9 +12,12 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static org.redisson.BaseTest.createConfig;
import static org.redisson.BaseTest.createInstance;
import org.redisson.api.RFuture; import org.redisson.api.RFuture;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
import org.redisson.api.RemoteInvocationOptions; import org.redisson.api.RemoteInvocationOptions;
@ -120,6 +123,14 @@ public class RedissonRemoteServiceTest extends BaseTest {
SerializablePojo doSomethingWithSerializablePojo(SerializablePojo pojo); SerializablePojo doSomethingWithSerializablePojo(SerializablePojo pojo);
String methodOverload();
String methodOverload(String str);
String methodOverload(Long lng);
String methodOverload(String str, Long lng);
} }
public class RemoteImpl implements RemoteInterface { public class RemoteImpl implements RemoteInterface {
@ -183,6 +194,27 @@ public class RedissonRemoteServiceTest extends BaseTest {
public SerializablePojo doSomethingWithSerializablePojo(SerializablePojo pojo) { public SerializablePojo doSomethingWithSerializablePojo(SerializablePojo pojo) {
return pojo; return pojo;
} }
@Override
public String methodOverload() {
return "methodOverload()";
}
@Override
public String methodOverload(Long lng) {
return "methodOverload(Long lng)";
}
@Override
public String methodOverload(String str) {
return "methodOverload(String str)";
}
@Override
public String methodOverload(String str, Long lng) {
return "methodOverload(String str, Long lng)";
}
} }
@Test @Test
@ -694,4 +726,21 @@ public class RedissonRemoteServiceTest extends BaseTest {
server.shutdown(); server.shutdown();
} }
} }
@Test
public void testMethodOverload() {
RedissonClient r1 = createInstance();
r1.getRemoteService().register(RemoteInterface.class, new RemoteImpl());
RedissonClient r2 = createInstance();
RemoteInterface ri = r2.getRemoteService().get(RemoteInterface.class);
assertThat(ri.methodOverload()).isEqualTo("methodOverload()");
assertThat(ri.methodOverload(1l)).isEqualTo("methodOverload(Long lng)");
assertThat(ri.methodOverload("")).isEqualTo("methodOverload(String str)");
assertThat(ri.methodOverload("", 1l)).isEqualTo("methodOverload(String str, Long lng)");
r1.shutdown();
r2.shutdown();
}
} }

Loading…
Cancel
Save