Skip to content

Commit

Permalink
support attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnights committed Apr 12, 2018
1 parent 0d7e0ff commit bc97367
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.weibo.api.motan.proxy;

import com.weibo.api.motan.rpc.Request;

/**
* @author sunnights
*/
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,24 +18,35 @@ public RefererCommonHandler(String interfaceName, List<Cluster<T>> 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<String, String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
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;
import java.util.HashMap;
import java.util.Map;

/**
*
* Rpc Request
*
* @author fishermen
Expand All @@ -50,6 +51,7 @@ public String getInterfaceName() {
return interfaceName;
}

@Override
public void setInterfaceName(String interfaceName) {
this.interfaceName = interfaceName;
}
Expand Down Expand Up @@ -87,19 +89,19 @@ public Map<String, String> getAttachments() {
return attachments != null ? attachments : Collections.EMPTY_MAP;
}

public void setAttachments(Map<String, String> attachments) {
this.attachments = attachments;
}

@Override
public void setAttachment(String key, String value) {
if (this.attachments == null) {
this.attachments = new HashMap<String, String>();
this.attachments = new HashMap<>();
}

this.attachments.put(key, value);
}

public void setAttachments(Map<String, String> attachments) {
this.attachments = attachments;
}

@Override
public long getRequestId() {
return requestId;
Expand Down Expand Up @@ -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<String, String> 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<String, String> 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<String, String> entry : attachments.entrySet()) {
request.setAttachment(entry.getKey(), entry.getValue());
}
}
return request;
}
}

}
2 changes: 2 additions & 0 deletions motan-core/src/main/java/com/weibo/api/motan/rpc/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public interface Request {
*/
String getInterfaceName();

void setInterfaceName(String interfaceName);

/**
* service method name
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<String, String>())
.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);
}
Expand Down

0 comments on commit bc97367

Please sign in to comment.