Search in sources :

Example 1 with DefaultRequest

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());
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Test(org.junit.Test)

Example 2 with DefaultRequest

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());
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Test(org.junit.Test)

Example 3 with DefaultRequest

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();
    }
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) MessageHandler(com.weibo.api.motan.transport.MessageHandler) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Channel(com.weibo.api.motan.transport.Channel) Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Test(org.junit.Test)

Example 4 with DefaultRequest

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;
    }
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) HashMap(java.util.HashMap) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Request(com.weibo.api.motan.rpc.Request) ByteBuffer(java.nio.ByteBuffer) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Serialization(com.weibo.api.motan.codec.Serialization) DeserializableObject(com.weibo.api.motan.serialize.DeserializableObject) DeserializableObject(com.weibo.api.motan.serialize.DeserializableObject)

Example 5 with DefaultRequest

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);
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest)

Aggregations

DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)45 Test (org.junit.Test)26 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)8 Request (com.weibo.api.motan.rpc.Request)7 Model (com.weibo.api.motan.protocol.example.Model)5 Response (com.weibo.api.motan.rpc.Response)5 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)4 Channel (com.weibo.api.motan.transport.Channel)4 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)3 IHello (com.weibo.api.motan.protocol.example.IHello)3 URL (com.weibo.api.motan.rpc.URL)3 MessageHandler (com.weibo.api.motan.transport.MessageHandler)3 HashMap (java.util.HashMap)3 MockChannel (com.weibo.api.motan.mock.MockChannel)2 MockReferer (com.weibo.api.motan.mock.MockReferer)2 Referer (com.weibo.api.motan.rpc.Referer)2 Listener (io.grpc.ServerCall.Listener)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Codec (com.weibo.api.motan.codec.Codec)1 Serialization (com.weibo.api.motan.codec.Serialization)1