From bc97367780d71d85e798ae11d3051825de8a957d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=BE=BD?= Date: Wed, 11 Apr 2018 21:13:30 +0800 Subject: [PATCH] support attachments --- .../weibo/api/motan/proxy/CommonHandler.java | 20 ++++++ .../api/motan/proxy/RefererCommonHandler.java | 35 ++++++---- .../weibo/api/motan/rpc/DefaultRequest.java | 66 +++++++++++++++++-- .../java/com/weibo/api/motan/rpc/Request.java | 2 + .../motan/demo/client/DemoRpcClient.java | 15 +++++ 5 files changed, 120 insertions(+), 18 deletions(-) diff --git a/motan-core/src/main/java/com/weibo/api/motan/proxy/CommonHandler.java b/motan-core/src/main/java/com/weibo/api/motan/proxy/CommonHandler.java index 9e4899598..27c8c7fcc 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/proxy/CommonHandler.java +++ b/motan-core/src/main/java/com/weibo/api/motan/proxy/CommonHandler.java @@ -1,5 +1,7 @@ package com.weibo.api.motan.proxy; +import com.weibo.api.motan.rpc.Request; + /** * @author sunnights */ @@ -25,4 +27,22 @@ public interface CommonHandler { * @throws Throwable */ Object asyncCall(String methodName, Object[] params, Class returnType) throws Throwable; + + /** + * call a service method with request + * + * @param request + * @param returnType + * @return + */ + Object call(Request request, Class returnType) throws Throwable; + + /** + * async call a service with request + * + * @param request + * @param returnType + * @return + */ + Object asyncCall(Request request, Class returnType) throws Throwable; } diff --git a/motan-core/src/main/java/com/weibo/api/motan/proxy/RefererCommonHandler.java b/motan-core/src/main/java/com/weibo/api/motan/proxy/RefererCommonHandler.java index 4f3758b10..33c909797 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/proxy/RefererCommonHandler.java +++ b/motan-core/src/main/java/com/weibo/api/motan/proxy/RefererCommonHandler.java @@ -2,10 +2,10 @@ import com.weibo.api.motan.cluster.Cluster; import com.weibo.api.motan.rpc.DefaultRequest; -import com.weibo.api.motan.util.ReflectUtil; -import com.weibo.api.motan.util.RequestIdGenerator; +import com.weibo.api.motan.rpc.Request; import java.util.List; +import java.util.Map; /** * @author sunnights @@ -18,24 +18,35 @@ public RefererCommonHandler(String interfaceName, List> clusters) { init(); } - public Object call(String methodName, Object[] params, Class returnType, boolean async) throws Throwable { - DefaultRequest request = new DefaultRequest(); - request.setRequestId(RequestIdGenerator.getRequestId()); - request.setArguments(params); - request.setMethodName(methodName); - request.setParamtersDesc(ReflectUtil.getParamsDesc(params)); - request.setInterfaceName(interfaceName); - + public Object call(String methodName, Object[] params, Class returnType, Map attachments, boolean async) throws Throwable { + DefaultRequest request = new DefaultRequest.Builder() + .interfaceName(interfaceName) + .methodName(methodName) + .arguments(params) + .attachments(attachments) + .build(); return invokeRequest(request, returnType, async); } @Override public Object call(String methodName, Object[] params, Class returnType) throws Throwable { - return call(methodName, params, returnType, false); + return call(methodName, params, returnType, null, false); } @Override public Object asyncCall(String methodName, Object[] params, Class returnType) throws Throwable { - return call(methodName, params, returnType, true); + return call(methodName, params, returnType, null, true); + } + + @Override + public Object call(Request request, Class returnType) throws Throwable { + request.setInterfaceName(interfaceName); + return invokeRequest(request, returnType, false); + } + + @Override + public Object asyncCall(Request request, Class returnType) throws Throwable { + request.setInterfaceName(interfaceName); + return invokeRequest(request, returnType, true); } } diff --git a/motan-core/src/main/java/com/weibo/api/motan/rpc/DefaultRequest.java b/motan-core/src/main/java/com/weibo/api/motan/rpc/DefaultRequest.java index f3bb5e808..9eb8640db 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/rpc/DefaultRequest.java +++ b/motan-core/src/main/java/com/weibo/api/motan/rpc/DefaultRequest.java @@ -17,6 +17,8 @@ package com.weibo.api.motan.rpc; import com.weibo.api.motan.protocol.rpc.RpcProtocolVersion; +import com.weibo.api.motan.util.ReflectUtil; +import com.weibo.api.motan.util.RequestIdGenerator; import java.io.Serializable; import java.util.Collections; @@ -24,7 +26,6 @@ import java.util.Map; /** - * * Rpc Request * * @author fishermen @@ -50,6 +51,7 @@ public String getInterfaceName() { return interfaceName; } + @Override public void setInterfaceName(String interfaceName) { this.interfaceName = interfaceName; } @@ -87,19 +89,19 @@ public Map getAttachments() { return attachments != null ? attachments : Collections.EMPTY_MAP; } + public void setAttachments(Map attachments) { + this.attachments = attachments; + } + @Override public void setAttachment(String key, String value) { if (this.attachments == null) { - this.attachments = new HashMap(); + this.attachments = new HashMap<>(); } this.attachments.put(key, value); } - public void setAttachments(Map attachments) { - this.attachments = attachments; - } - @Override public long getRequestId() { return requestId; @@ -134,5 +136,57 @@ public void setRpcProtocolVersion(byte rpcProtocolVersion) { this.rpcProtocolVersion = rpcProtocolVersion; } + public static class Builder { + private String interfaceName; + private String methodName; + private Object[] arguments; + private Map attachments; + + public Builder() { + attachments = new HashMap<>(); + } + + public Builder interfaceName(String interfaceName) { + this.interfaceName = interfaceName; + return this; + } + + public Builder methodName(String methodName) { + this.methodName = methodName; + return this; + } + + public Builder arguments(Object[] arguments) { + this.arguments = arguments; + return this; + } + + public Builder attachment(String key, String value) { + this.attachments.put(key, value); + return this; + } + + public Builder attachments(Map attachments) { + if (attachments != null) { + this.attachments.putAll(attachments); + } + return this; + } + + public DefaultRequest build() { + DefaultRequest request = new DefaultRequest(); + request.setRequestId(RequestIdGenerator.getRequestId()); + request.setInterfaceName(interfaceName); + request.setMethodName(methodName); + request.setParamtersDesc(ReflectUtil.getParamsDesc(arguments)); + request.setArguments(arguments); + if (attachments != null) { + for (Map.Entry entry : attachments.entrySet()) { + request.setAttachment(entry.getKey(), entry.getValue()); + } + } + return request; + } + } } diff --git a/motan-core/src/main/java/com/weibo/api/motan/rpc/Request.java b/motan-core/src/main/java/com/weibo/api/motan/rpc/Request.java index 20597f383..c78661c8c 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/rpc/Request.java +++ b/motan-core/src/main/java/com/weibo/api/motan/rpc/Request.java @@ -35,6 +35,8 @@ public interface Request { */ String getInterfaceName(); + void setInterfaceName(String interfaceName); + /** * service method name * diff --git a/motan-demo/motan-demo-client/src/main/java/com/weibo/motan/demo/client/DemoRpcClient.java b/motan-demo/motan-demo-client/src/main/java/com/weibo/motan/demo/client/DemoRpcClient.java index 04d5f3cba..ab07118bf 100644 --- a/motan-demo/motan-demo-client/src/main/java/com/weibo/motan/demo/client/DemoRpcClient.java +++ b/motan-demo/motan-demo-client/src/main/java/com/weibo/motan/demo/client/DemoRpcClient.java @@ -17,6 +17,7 @@ package com.weibo.motan.demo.client; import com.weibo.api.motan.proxy.RefererCommonHandler; +import com.weibo.api.motan.rpc.DefaultRequest; import com.weibo.api.motan.rpc.Future; import com.weibo.api.motan.rpc.FutureListener; import com.weibo.api.motan.rpc.ResponseFuture; @@ -25,6 +26,8 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; +import java.util.HashMap; + public class DemoRpcClient { public static void main(String[] args) throws Throwable { @@ -53,6 +56,18 @@ public void operationComplete(Future future) { } }); + DefaultRequest request = new DefaultRequest.Builder() + .methodName("rename") + .arguments(new Object[]{user, "EEE"}) + .attachment("a", "a") + .attachment("b", "b") + .attachments(new HashMap()) + .build(); + + ResponseFuture future3 = (ResponseFuture) client.asyncCall(request, User.class); + user = (User) future3.getValue(); + System.out.println(user); + System.out.println("motan demo is finish."); System.exit(0); }