forked from apache/dubbo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
支持隐式参数的回传(从服务端传到消费端),apache#889、apache#895
- Loading branch information
hsrong
committed
Dec 22, 2017
1 parent
d917744
commit 8f38201
Showing
3 changed files
with
427 additions
and
316 deletions.
There are no files selected for viewing
257 changes: 138 additions & 119 deletions
257
...o-rpc-default/src/main/java/com/alibaba/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,119 +1,138 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.dubbo.rpc.protocol.dubbo; | ||
|
||
import com.alibaba.dubbo.common.logger.Logger; | ||
import com.alibaba.dubbo.common.logger.LoggerFactory; | ||
import com.alibaba.dubbo.common.serialize.ObjectInput; | ||
import com.alibaba.dubbo.common.utils.Assert; | ||
import com.alibaba.dubbo.common.utils.StringUtils; | ||
import com.alibaba.dubbo.remoting.Channel; | ||
import com.alibaba.dubbo.remoting.Codec; | ||
import com.alibaba.dubbo.remoting.Decodeable; | ||
import com.alibaba.dubbo.remoting.exchange.Response; | ||
import com.alibaba.dubbo.remoting.transport.CodecSupport; | ||
import com.alibaba.dubbo.rpc.Invocation; | ||
import com.alibaba.dubbo.rpc.RpcResult; | ||
import com.alibaba.dubbo.rpc.support.RpcUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.lang.reflect.Type; | ||
|
||
public class DecodeableRpcResult extends RpcResult implements Codec, Decodeable { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(DecodeableRpcResult.class); | ||
|
||
private Channel channel; | ||
|
||
private byte serializationType; | ||
|
||
private InputStream inputStream; | ||
|
||
private Response response; | ||
|
||
private Invocation invocation; | ||
|
||
private volatile boolean hasDecoded; | ||
|
||
public DecodeableRpcResult(Channel channel, Response response, InputStream is, Invocation invocation, byte id) { | ||
Assert.notNull(channel, "channel == null"); | ||
Assert.notNull(response, "response == null"); | ||
Assert.notNull(is, "inputStream == null"); | ||
this.channel = channel; | ||
this.response = response; | ||
this.inputStream = is; | ||
this.invocation = invocation; | ||
this.serializationType = id; | ||
} | ||
|
||
public void encode(Channel channel, OutputStream output, Object message) throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public Object decode(Channel channel, InputStream input) throws IOException { | ||
ObjectInput in = CodecSupport.getSerialization(channel.getUrl(), serializationType) | ||
.deserialize(channel.getUrl(), input); | ||
|
||
byte flag = in.readByte(); | ||
switch (flag) { | ||
case DubboCodec.RESPONSE_NULL_VALUE: | ||
break; | ||
case DubboCodec.RESPONSE_VALUE: | ||
try { | ||
Type[] returnType = RpcUtils.getReturnTypes(invocation); | ||
setValue(returnType == null || returnType.length == 0 ? in.readObject() : | ||
(returnType.length == 1 ? in.readObject((Class<?>) returnType[0]) | ||
: in.readObject((Class<?>) returnType[0], returnType[1]))); | ||
} catch (ClassNotFoundException e) { | ||
throw new IOException(StringUtils.toString("Read response data failed.", e)); | ||
} | ||
break; | ||
case DubboCodec.RESPONSE_WITH_EXCEPTION: | ||
try { | ||
Object obj = in.readObject(); | ||
if (obj instanceof Throwable == false) | ||
throw new IOException("Response data error, expect Throwable, but get " + obj); | ||
setException((Throwable) obj); | ||
} catch (ClassNotFoundException e) { | ||
throw new IOException(StringUtils.toString("Read response data failed.", e)); | ||
} | ||
break; | ||
default: | ||
throw new IOException("Unknown result flag, expect '0' '1' '2', get " + flag); | ||
} | ||
return this; | ||
} | ||
|
||
public void decode() throws Exception { | ||
if (!hasDecoded && channel != null && inputStream != null) { | ||
try { | ||
decode(channel, inputStream); | ||
} catch (Throwable e) { | ||
if (log.isWarnEnabled()) { | ||
log.warn("Decode rpc result failed: " + e.getMessage(), e); | ||
} | ||
response.setStatus(Response.CLIENT_ERROR); | ||
response.setErrorMessage(StringUtils.toString(e)); | ||
} finally { | ||
hasDecoded = true; | ||
} | ||
} | ||
} | ||
|
||
} | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.dubbo.rpc.protocol.dubbo; | ||
|
||
import com.alibaba.dubbo.common.logger.Logger; | ||
import com.alibaba.dubbo.common.logger.LoggerFactory; | ||
import com.alibaba.dubbo.common.serialize.ObjectInput; | ||
import com.alibaba.dubbo.common.utils.Assert; | ||
import com.alibaba.dubbo.common.utils.StringUtils; | ||
import com.alibaba.dubbo.remoting.Channel; | ||
import com.alibaba.dubbo.remoting.Codec; | ||
import com.alibaba.dubbo.remoting.Decodeable; | ||
import com.alibaba.dubbo.remoting.exchange.Response; | ||
import com.alibaba.dubbo.remoting.transport.CodecSupport; | ||
import com.alibaba.dubbo.rpc.Invocation; | ||
import com.alibaba.dubbo.rpc.RpcResult; | ||
import com.alibaba.dubbo.rpc.support.RpcUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.lang.reflect.Type; | ||
import java.util.HashMap; | ||
|
||
public class DecodeableRpcResult extends RpcResult implements Codec, Decodeable { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(DecodeableRpcResult.class); | ||
|
||
private Channel channel; | ||
|
||
private byte serializationType; | ||
|
||
private InputStream inputStream; | ||
|
||
private Response response; | ||
|
||
private Invocation invocation; | ||
|
||
private volatile boolean hasDecoded; | ||
|
||
public DecodeableRpcResult(Channel channel, Response response, InputStream is, Invocation invocation, byte id) { | ||
Assert.notNull(channel, "channel == null"); | ||
Assert.notNull(response, "response == null"); | ||
Assert.notNull(is, "inputStream == null"); | ||
this.channel = channel; | ||
this.response = response; | ||
this.inputStream = is; | ||
this.invocation = invocation; | ||
this.serializationType = id; | ||
} | ||
|
||
public void encode(Channel channel, OutputStream output, Object message) throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public Object decode(Channel channel, InputStream input) throws IOException { | ||
ObjectInput in = CodecSupport.getSerialization(channel.getUrl(), serializationType) | ||
.deserialize(channel.getUrl(), input); | ||
|
||
byte flag = in.readByte(); | ||
switch (flag) { | ||
case DubboCodec.RESPONSE_NULL_VALUE: | ||
break; | ||
case DubboCodec.RESPONSE_VALUE: | ||
try { | ||
Type[] returnType = RpcUtils.getReturnTypes(invocation); | ||
setValue(returnType == null || returnType.length == 0 ? in.readObject() : | ||
(returnType.length == 1 ? in.readObject((Class<?>) returnType[0]) | ||
: in.readObject((Class<?>) returnType[0], returnType[1]))); | ||
} catch (ClassNotFoundException e) { | ||
throw new IOException(StringUtils.toString("Read response data failed.", e)); | ||
} | ||
break; | ||
case DubboCodec.RESPONSE_WITH_EXCEPTION: | ||
try { | ||
Object obj = in.readObject(); | ||
if (obj instanceof Throwable == false) | ||
throw new IOException("Response data error, expect Throwable, but get " + obj); | ||
setException((Throwable) obj); | ||
} catch (ClassNotFoundException e) { | ||
throw new IOException(StringUtils.toString("Read response data failed.", e)); | ||
} | ||
break; | ||
default: | ||
throw new IOException("Unknown result flag, expect '0' '1' '2', get " + flag); | ||
} | ||
byte attachmentFlag = in.readByte(); | ||
switch (attachmentFlag) { | ||
case DubboCodec.RESPONSE_ATTACHMENTS_NO_EXIST: | ||
break; | ||
case DubboCodec.RESPONSE_ATTACHMENTS_EXIST: | ||
try { | ||
HashMap<String, String> attachments = in.readObject(HashMap.class); | ||
if(attachments != null | ||
&& attachments.size() > 0){ | ||
this.getAttachments().putAll(attachments); | ||
} | ||
} catch (ClassNotFoundException e) { | ||
throw new IOException(StringUtils.toString("Read response data failed.", e)); | ||
} | ||
break; | ||
default: | ||
throw new IOException("Unknown result attachments flag, expect '3' '4', get " + attachmentFlag); | ||
} | ||
return this; | ||
} | ||
|
||
public void decode() throws Exception { | ||
if (!hasDecoded && channel != null && inputStream != null) { | ||
try { | ||
decode(channel, inputStream); | ||
} catch (Throwable e) { | ||
if (log.isWarnEnabled()) { | ||
log.warn("Decode rpc result failed: " + e.getMessage(), e); | ||
} | ||
response.setStatus(Response.CLIENT_ERROR); | ||
response.setErrorMessage(StringUtils.toString(e)); | ||
} finally { | ||
hasDecoded = true; | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
8f38201
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Provider向Consumer回传,也是通过Attachment,是吧