use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.
the class NettyResponseFutureTest method testException.
@Test
public void testException() {
DefaultRequest request = new DefaultRequest();
NettyResponseFuture response = new NettyResponseFuture(request, 100, client);
Exception exception = new Exception("hello");
DefaultResponse defaultResponse = new DefaultResponse();
defaultResponse.setException(exception);
response.onFailure(defaultResponse);
try {
response.getValue();
Assert.assertTrue(false);
} catch (Exception e) {
Assert.assertTrue(true);
}
Assert.assertTrue(response.isDone());
}
use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.
the class NettyResponseFutureTest method testCancel.
@Test
public void testCancel() {
DefaultRequest request = new DefaultRequest();
NettyResponseFuture response = new NettyResponseFuture(request, 10, client);
response.cancel();
try {
response.getValue();
Assert.assertTrue(false);
} catch (Exception e) {
Assert.assertTrue(true);
}
Assert.assertTrue(response.isCancelled());
}
use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.
the class UnSerializableClass method testNettyRequestDecodeException.
/**
* @throws Exception
*/
@Test
public void testNettyRequestDecodeException() throws Exception {
NettyServer nettyServer;
nettyServer = new NettyServer(url, new MessageHandler() {
@Override
public Object handle(Channel channel, Object message) {
Request request = (Request) message;
DefaultResponse response = new DefaultResponse();
response.setRequestId(request.getRequestId());
response.setValue("success");
return response;
}
});
nettyServer.open();
NettyClient nettyClient = new NettyClient(url);
nettyClient.open();
DefaultRequest request = new DefaultRequest();
request.setRequestId(RequestIdGenerator.getRequestId());
request.setInterfaceName(url.getPath());
request.setMethodName("hello");
request.setParamtersDesc("void");
try {
nettyClient.request(request);
Assert.assertTrue(false);
} catch (Exception e) {
Assert.assertTrue(e.getMessage().contains("framework decode error"));
} finally {
nettyClient.close();
nettyServer.close();
}
}
use of com.weibo.api.motan.rpc.DefaultRequest 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.rpc.DefaultRequest in project motan by weibocom.
the class RefererCommonHandler method call.
public Object call(String methodName, Object[] arguments, Class returnType, Map<String, String> attachments, boolean async) throws Throwable {
DefaultRequest request = new DefaultRequest();
request.setRequestId(RequestIdGenerator.getRequestId());
request.setInterfaceName(interfaceName);
request.setMethodName(methodName);
request.setArguments(arguments);
request.setAttachments(attachments);
return invokeRequest(request, returnType, async);
}
Aggregations