You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "shadowmind (GitHub)" <gi...@apache.org> on 2019/11/26 11:56:41 UTC
[GitHub] [dubbo] shadowmind opened issue #5389: Dubbo 2.6.x
RpcContext.getServerContext().getAttachment(key) is null when consumer use
generic
- [ ] I have searched the [issues](https://github.com/apache/dubbo/issues) of this repository and believe that this is not a duplicate.
- [ ] I have checked the [FAQ](https://github.com/apache/dubbo/blob/master/FAQ.md) of this repository and believe that this is not a duplicate.
### Environment
* Dubbo version: 2.6.x
* Operating System version: MacOS 10.12.6
* Java version: Java 1.8
### Steps to reproduce this issue
1. Build a provider demo, add RpcContext.getServerContext().setAttachment("key","value");
2. Build a consumer demo. Call provider service by generic.
3. RpcContext.getServerContext().getAttachment("key");
What do you expected from the above steps?
Get the value which provider set in the RpcContext.getServerContext() attachment
What actually happens?
Null. Can't get the value which provider set in the RpcContext.getServerContext() attachment
Cause:
GenericFilter return a new RpcResult without setting setAttachments
Code(GenericFilter.java):
`Result result = invoker.invoke(new RpcInvocation(method, args, inv.getAttachments()));
if (result.hasException()
&& !(result.getException() instanceof GenericException)) {
return new RpcResult(new GenericException(result.getException()));
}
if (ProtocolUtils.isJavaGenericSerialization(generic)) {
try {
UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(512);
ExtensionLoader.getExtensionLoader(Serialization.class)
.getExtension(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA)
.serialize(null, os).writeObject(result.getValue());
return new RpcResult(os.toByteArray());
} catch (IOException e) {
throw new RpcException("Serialize result failed.", e);
}
} else if (ProtocolUtils.isBeanGenericSerialization(generic)) {
return new RpcResult(JavaBeanSerializeUtil.serialize(result.getValue(), JavaBeanAccessor.METHOD));
} else {
return new RpcResult(PojoUtils.generalize(result.getValue()));
}`
Fix:
`RpcResult rpcResult;
if (ProtocolUtils.isJavaGenericSerialization(generic)) {
try {
UnsafeByteArrayOutputStream os = new UnsafeByteArrayOutputStream(512);
ExtensionLoader.getExtensionLoader(Serialization.class)
.getExtension(Constants.GENERIC_SERIALIZATION_NATIVE_JAVA)
.serialize(null, os).writeObject(result.getValue());
rpcResult = new RpcResult(os.toByteArray());
} catch (IOException e) {
throw new RpcException("Serialize result failed.", e);
}
} else if (ProtocolUtils.isBeanGenericSerialization(generic)) {
rpcResult = new RpcResult(JavaBeanSerializeUtil.serialize(result.getValue(), JavaBeanAccessor.METHOD));
} else {
rpcResult = new RpcResult(PojoUtils.generalize(result.getValue()));
}
rpcResult.setAttachments(result.getAttachments());
return rpcResult;`
If there is an exception, please attach the exception trace:
```
Just put your stack trace here!
```
[ Full content available at: https://github.com/apache/dubbo/issues/5389 ]
This message was relayed via gitbox.apache.org for notifications@dubbo.apache.org
[GitHub] [dubbo] chickenlj commented on issue #5389: Dubbo 2.6.x
RpcContext.getServerContext().getAttachment(key) is null when consumer use
generic
Posted by "chickenlj (GitHub)" <gi...@apache.org>.
Merged.
[ Full content available at: https://github.com/apache/dubbo/issues/5389 ]
This message was relayed via gitbox.apache.org for notifications@dubbo.apache.org