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.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.redisson.api.RBlockingQueue;
@ -184,7 +186,7 @@ public abstract class BaseRemoteService {
final RBlockingQueue<RemoteServiceRequest> requestQueue = redisson.getBlockingQueue(requestQueueName,
getCodec());
final RemoteServiceRequest request = new RemoteServiceRequest(requestId, method.getName(), args,
final RemoteServiceRequest request = new RemoteServiceRequest(requestId, method.getName(), getMethodSignatures(method), args,
optionsCopy, System.currentTimeMillis());
final RemotePromise<Object> result = new RemotePromise<Object>(commandExecutor.getConnectionManager().newPromise()) {
@ -399,7 +401,7 @@ public abstract class BaseRemoteService {
String requestQueueName = getRequestQueueName(remoteInterface);
RBlockingQueue<RemoteServiceRequest> requestQueue = redisson.getBlockingQueue(requestQueueName,
getCodec());
RemoteServiceRequest request = new RemoteServiceRequest(requestId, method.getName(), args, optionsCopy,
RemoteServiceRequest request = new RemoteServiceRequest(requestId, method.getName(), getMethodSignatures(method), args, optionsCopy,
System.currentTimeMillis());
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()) {
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) {
return;
}
@ -183,7 +183,7 @@ public class RedissonRemoteService extends BaseRemoteService implements RRemoteS
private <T> void executeMethod(final Class<T> remoteInterface, final RBlockingQueue<RemoteServiceRequest> requestQueue,
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());
RBlockingQueue<RemoteServiceCancelRequest> cancelRequestQueue =

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

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

@ -12,9 +12,12 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Assert;
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.RedissonClient;
import org.redisson.api.RemoteInvocationOptions;
@ -119,6 +122,14 @@ public class RedissonRemoteServiceTest extends BaseTest {
Pojo doSomethingWithPojo(Pojo pojo);
SerializablePojo doSomethingWithSerializablePojo(SerializablePojo pojo);
String methodOverload();
String methodOverload(String str);
String methodOverload(Long lng);
String methodOverload(String str, Long lng);
}
@ -183,6 +194,27 @@ public class RedissonRemoteServiceTest extends BaseTest {
public SerializablePojo doSomethingWithSerializablePojo(SerializablePojo 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
@ -694,4 +726,21 @@ public class RedissonRemoteServiceTest extends BaseTest {
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