use of com.weibo.api.motan.serialize.DeserializableObject in project motan by weibocom.
the class MotanV2Codec method decode.
/**
* decode data
*
* @return
* @throws IOException
*/
@Override
public Object decode(Channel channel, String remoteIp, byte[] data) throws IOException {
MotanV2Header header = MotanV2Header.buildHeader(data);
Map<String, String> metaMap = new HashMap<String, String>();
ByteBuffer buf = ByteBuffer.wrap(data);
int metaSize = buf.getInt(HEADER_SIZE);
int index = HEADER_SIZE + 4;
if (metaSize > 0) {
byte[] meta = new byte[metaSize];
buf.position(index);
buf.get(meta);
metaMap = decodeMeta(meta);
index += metaSize;
}
int bodySize = buf.getInt(index);
index += 4;
Object obj = null;
if (bodySize > 0) {
byte[] body = new byte[bodySize];
buf.position(index);
buf.get(body);
if (header.isGzip()) {
body = ByteUtil.unGzip(body);
}
// 默认自适应序列化
Serialization serialization = getSerializationByNum(header.getSerialize());
obj = new DeserializableObject(serialization, body);
}
if (header.isRequest()) {
if (header.isHeartbeat()) {
Request request = DefaultRpcHeartbeatFactory.getDefaultHeartbeatRequest(header.getRequestId());
request.setRpcProtocolVersion(RpcProtocolVersion.VERSION_2.getVersion());
return request;
} else {
DefaultRequest request = new DefaultRequest();
request.setRequestId(header.getRequestId());
request.setInterfaceName(metaMap.remove(M2_PATH));
request.setMethodName(metaMap.remove(M2_METHOD));
request.setParamtersDesc(metaMap.remove(M2_METHOD_DESC));
request.setAttachments(metaMap);
request.setRpcProtocolVersion(RpcProtocolVersion.VERSION_2.getVersion());
request.setSerializeNumber(header.getSerialize());
if (obj != null) {
request.setArguments(new Object[] { obj });
}
if (metaMap.get(M2_GROUP) != null) {
request.setAttachment(URLParamType.group.getName(), metaMap.get(M2_GROUP));
}
if (StringUtils.isNotBlank(metaMap.get(M2_VERSION))) {
request.setAttachment(URLParamType.version.getName(), metaMap.get(M2_VERSION));
}
if (StringUtils.isNotBlank(metaMap.get(M2_SOURCE))) {
request.setAttachment(URLParamType.application.getName(), metaMap.get(M2_SOURCE));
}
if (StringUtils.isNotBlank(metaMap.get(M2_MODULE))) {
request.setAttachment(URLParamType.module.getName(), metaMap.get(M2_MODULE));
}
return request;
}
} else {
if (header.isHeartbeat()) {
return DefaultRpcHeartbeatFactory.getDefaultHeartbeatResponse(header.getRequestId());
}
DefaultResponse response = new DefaultResponse();
response.setRequestId(header.getRequestId());
response.setProcessTime(MathUtil.parseLong(metaMap.remove(M2_PROCESS_TIME), 0));
response.setAttachments(metaMap);
if (header.getStatus() == MotanV2Header.MessageStatus.NORMAL.getStatus()) {
// 只解析正常消息
response.setValue(obj);
} else {
String errmsg = metaMap.remove(M2_ERROR);
Exception e = ExceptionUtil.fromMessage(errmsg);
if (e == null) {
e = (Exception) new MotanServiceException("default remote exception. remote errmsg:" + errmsg, false);
}
response.setException(e);
}
return response;
}
}
use of com.weibo.api.motan.serialize.DeserializableObject in project motan by weibocom.
the class ProviderMessageRouter method processLazyDeserialize.
private void processLazyDeserialize(Request request, Method method) {
if (method != null && request.getArguments() != null && request.getArguments().length == 1 && request.getArguments()[0] instanceof DeserializableObject && request instanceof DefaultRequest) {
try {
Object[] args = ((DeserializableObject) request.getArguments()[0]).deserializeMulti(method.getParameterTypes());
((DefaultRequest) request).setArguments(args);
} catch (IOException e) {
throw new MotanFrameworkException("deserialize parameters fail: " + request.toString() + ", error:" + e.getMessage());
}
}
}
use of com.weibo.api.motan.serialize.DeserializableObject in project motan by weibocom.
the class AbstractRefererHandler method invokeRequest.
Object invokeRequest(Request request, Class returnType, boolean async) throws Throwable {
RpcContext curContext = RpcContext.getContext();
curContext.putAttribute(MotanConstants.ASYNC_SUFFIX, async);
// set rpc context attachments to request
Map<String, String> attachments = curContext.getRpcAttachments();
if (!attachments.isEmpty()) {
for (Map.Entry<String, String> entry : attachments.entrySet()) {
request.setAttachment(entry.getKey(), entry.getValue());
}
}
// add to attachment if client request id is set
if (StringUtils.isNotBlank(curContext.getClientRequestId())) {
request.setAttachment(URLParamType.requestIdFromClient.getName(), curContext.getClientRequestId());
}
// 那么正常情况下只会使用A,如果A被开关降级,那么就会使用B,B也被降级,那么会使用C
for (Cluster<T> cluster : clusters) {
String protocolSwitcher = MotanConstants.PROTOCOL_SWITCHER_PREFIX + cluster.getUrl().getProtocol();
Switcher switcher = switcherService.getSwitcher(protocolSwitcher);
if (switcher != null && !switcher.isOn()) {
continue;
}
request.setAttachment(URLParamType.version.getName(), cluster.getUrl().getVersion());
request.setAttachment(URLParamType.clientGroup.getName(), cluster.getUrl().getGroup());
// 带上client的application和module
request.setAttachment(URLParamType.application.getName(), cluster.getUrl().getApplication());
request.setAttachment(URLParamType.module.getName(), cluster.getUrl().getModule());
Response response = null;
boolean throwException = Boolean.parseBoolean(cluster.getUrl().getParameter(URLParamType.throwException.getName(), URLParamType.throwException.getValue()));
try {
MotanFrameworkUtil.logEvent(request, MotanConstants.TRACE_INVOKE);
response = cluster.call(request);
if (async) {
if (response instanceof ResponseFuture) {
((ResponseFuture) response).setReturnType(returnType);
return response;
} else {
ResponseFuture responseFuture = new DefaultResponseFuture(request, 0, cluster.getUrl());
if (response.getException() != null) {
responseFuture.onFailure(response);
} else {
responseFuture.onSuccess(response);
}
responseFuture.setReturnType(returnType);
return responseFuture;
}
} else {
Object value = response.getValue();
if (value != null && value instanceof DeserializableObject) {
try {
value = ((DeserializableObject) value).deserialize(returnType);
} catch (IOException e) {
LoggerUtil.error("deserialize response value fail! deserialize type:" + returnType, e);
throw new MotanFrameworkException("deserialize return value fail! deserialize type:" + returnType, e);
}
}
return value;
}
} catch (RuntimeException e) {
if (ExceptionUtil.isBizException(e)) {
Throwable t = e.getCause();
// 只抛出Exception,防止抛出远程的Error
if (t != null && t instanceof Exception) {
throw t;
} else {
String msg = t == null ? "biz exception cause is null. origin error msg : " + e.getMessage() : ("biz exception cause is throwable error:" + t.getClass() + ", errmsg:" + t.getMessage());
throw new MotanServiceException(msg);
}
} else if (!throwException) {
LoggerUtil.warn("RefererInvocationHandler invoke false, so return default value: uri=" + cluster.getUrl().getUri() + " " + MotanFrameworkUtil.toString(request), e);
return getDefaultReturnValue(returnType);
} else {
LoggerUtil.error("RefererInvocationHandler invoke Error: uri=" + cluster.getUrl().getUri() + " " + MotanFrameworkUtil.toString(request), e);
throw e;
}
}
}
throw new MotanServiceException("Referer call Error: cluster not exist, interface=" + interfaceName + " " + MotanFrameworkUtil.toString(request), MotanErrorMsgConstant.SERVICE_UNFOUND, false);
}
Aggregations