Skip to content

Commit 7484dd8

Browse files
authored
Merge pull request #1081 from weibocom/feat/support_set_attachments_on_server_side
support setting attachments on server side
2 parents 1c78355 + 0f4b335 commit 7484dd8

File tree

7 files changed

+23
-5
lines changed

7 files changed

+23
-5
lines changed

motan-core/src/main/java/com/weibo/api/motan/rpc/DefaultResponseFuture.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public void onSuccess(Response response) {
8484
public void onFailure(Response response) {
8585
this.exception = response.getException();
8686
this.processTime = response.getProcessTime();
87+
this.attachments = response.getAttachments();
8788

8889
done();
8990
}

motan-transport-netty/src/main/java/com/weibo/api/motan/transport/netty/NettyChannelHandler.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@
2424
import com.weibo.api.motan.rpc.*;
2525
import com.weibo.api.motan.transport.Channel;
2626
import com.weibo.api.motan.transport.MessageHandler;
27-
import com.weibo.api.motan.util.LoggerUtil;
28-
import com.weibo.api.motan.util.MotanFrameworkUtil;
29-
import com.weibo.api.motan.util.NetUtils;
30-
import com.weibo.api.motan.util.StatisticCallback;
27+
import com.weibo.api.motan.util.*;
3128
import org.jboss.netty.channel.*;
3229

30+
import java.util.Map;
3331
import java.util.concurrent.RejectedExecutionException;
3432
import java.util.concurrent.ThreadPoolExecutor;
3533
import java.util.concurrent.atomic.AtomicInteger;
@@ -148,6 +146,13 @@ private void processResult(final ChannelHandlerContext ctx, final DefaultRespons
148146
response.setSerializeNumber(request.getSerializeNumber());
149147
response.setRpcProtocolVersion(request.getRpcProtocolVersion());
150148
response.setRequestId(request.getRequestId());
149+
// set rpc context attachments to response
150+
Map<String, String> attachments = RpcContext.getContext().getRpcAttachments();
151+
if (!CollectionUtil.isEmpty(attachments)) {
152+
for (Map.Entry<String, String> entry : attachments.entrySet()) {
153+
response.setAttachment(entry.getKey(), entry.getValue());
154+
}
155+
}
151156
response.setProcessTime(System.currentTimeMillis() - processStartTime);
152157
ChannelFuture channelFuture = null;
153158
if (ctx.getChannel().isConnected()) {

motan-transport-netty/src/main/java/com/weibo/api/motan/transport/netty/NettyClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ private Response asyncResponse(Response response, boolean async) {
195195
if (async || !(response instanceof ResponseFuture)) {
196196
return response;
197197
}
198-
198+
RpcContext.getContext().setResponse(response);
199199
return new DefaultResponse(response);
200200
}
201201

motan-transport-netty/src/test/java/com/weibo/api/motan/transport/netty/NettyClientTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public void testNormal() throws TransportException {
8686

8787
Assert.assertNotNull(result);
8888
Assert.assertEquals("method: " + request.getMethodName() + " requestId: " + request.getRequestId(), result);
89+
org.junit.Assert.assertNotNull(RpcContext.getContext().getResponse());
8990
}
9091

9192
public void testAsync() throws TransportException {

motan-transport-netty4/src/main/java/com/weibo/api/motan/transport/netty4/NettyChannelHandler.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.weibo.api.motan.rpc.*;
1212
import com.weibo.api.motan.transport.Channel;
1313
import com.weibo.api.motan.transport.MessageHandler;
14+
import com.weibo.api.motan.util.CollectionUtil;
1415
import com.weibo.api.motan.util.LoggerUtil;
1516
import com.weibo.api.motan.util.MotanFrameworkUtil;
1617
import com.weibo.api.motan.util.NetUtils;
@@ -21,6 +22,7 @@
2122

2223
import java.net.InetSocketAddress;
2324
import java.net.SocketAddress;
25+
import java.util.Map;
2426
import java.util.concurrent.RejectedExecutionException;
2527
import java.util.concurrent.ThreadPoolExecutor;
2628

@@ -167,6 +169,13 @@ private void processResult(final ChannelHandlerContext ctx, final DefaultRespons
167169
response.setSerializeNumber(request.getSerializeNumber());
168170
response.setRpcProtocolVersion(request.getRpcProtocolVersion());
169171
response.setRequestId(request.getRequestId());
172+
// set rpc context attachments to response
173+
Map<String, String> attachments = RpcContext.getContext().getRpcAttachments();
174+
if (!CollectionUtil.isEmpty(attachments)) {
175+
for (Map.Entry<String, String> entry : attachments.entrySet()) {
176+
response.setAttachment(entry.getKey(), entry.getValue());
177+
}
178+
}
170179
response.setProcessTime(System.currentTimeMillis() - processStartTime);
171180
sendResponse(ctx, response, true);
172181
}

motan-transport-netty4/src/main/java/com/weibo/api/motan/transport/netty4/NettyClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ private Response asyncResponse(Response response, boolean async) {
144144
if (async || !(response instanceof ResponseFuture)) {
145145
return response;
146146
}
147+
RpcContext.getContext().setResponse(response);
147148
return new DefaultResponse(response);
148149
}
149150

motan-transport-netty4/src/test/java/com/weibo/api/motan/transport/netty4/NettyClientTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public void testNormal() {
102102

103103
Assert.assertNotNull(result);
104104
Assert.assertEquals("method: " + request.getMethodName() + " requestId: " + request.getRequestId(), result);
105+
Assert.assertNotNull(RpcContext.getContext().getResponse());
105106
} catch (Exception e) {
106107
fail();
107108
}

0 commit comments

Comments
 (0)